INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Similar documents
Shell programming. Introduction to Operating Systems

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

Practice 5 Batch & Shell Programming

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

CENG 334 Computer Networks. Laboratory I Linux Tutorial

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Basic Linux (Bash) Commands

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

LING 408/508: Computational Techniques for Linguists. Lecture 5

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Computer Systems and Architecture

Answers to Even-numbered Exercises

Bash scripting basics

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

Lab #12: Shell Scripting

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Part 1: Basic Commands/U3li3es

Assignment 3, Due October 4

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Conditional Control Structures. Dr.T.Logeswari

Exploring UNIX: Session 3

DAVE LIDDAMENT INTRODUCTION TO BASH

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

Linux Shell Script. J. K. Mandal

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

CSC 2500: Unix Lab Fall 2016

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Computer Systems and Architecture

LOG ON TO LINUX AND LOG OFF

5/8/2012. Encryption-based Protection. Protection based on Access Permission (Contd) File Security, Setting and Using Permissions Chapter 9

Shell Programming (bash)

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Open up a terminal, make sure you are in your home directory, and run the command.

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Shell Control Structures

CS214 Advanced UNIX Lecture 4

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

COMP 4/6262: Programming UNIX

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Name: Tej. D. Shah Subject:CC-304 Linux Uni. Practical programme College :L.J. College Of Computer Application. Questions:

Introduction to the UNIX command line

Useful Unix Commands Cheat Sheet

Mills HPC Tutorial Series. Linux Basics II

Shell Programming (ch 10)

IBM AIX Basic Operations V5.

Shell Control Structures

Lab 4: Shell scripting

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 1

UNIX Programming Laboratory. Subject Code: 10MCA17

Shell Scripting. Index Of Contents

CS Unix Tools & Scripting

The Unix Shell & Shell Scripts

Introduction to Shell Scripting

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

UNIX shell scripting

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Shell Programming (Part 2)

PROGRAMMAZIONE I A.A. 2015/2016

Bash Shell Programming Helps

Introduction to Linux for BlueBEAR. January

Week 6 Lesson 1: Control-Flow Statements (Continued)

More Linux Shell Scripts

A shell can be used in one of two ways:

Exercise sheet 1 To be corrected in tutorials in the week from 23/10/2017 to 27/10/2017

CS1101: Lecture 9 The Shell as a Programming Language

Linux shell & shell scripting - II

Introduction to UNIX Command Line

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

Original Script. Display commands to manually creating an account. #!/bin/bash

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

SASGSUB for Job Workflow and SAS Log Files Piyush Singh, Prasoon Sangwan TATA Consultancy Services Ltd. Indianapolis, IN

CENG393 Computer Networks Labwork 1

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Програмиранев UNIX среда

Title:[ Variables Comparison Operators If Else Statements ]

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Lecture 02 The Shell and Shell Scripting

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

Introduction: What is Unix?

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

Assignment clarifications

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Introduction of Linux

What is Bash Shell Scripting?

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

CS 2505 Computer Organization I Test 1

There are some string operators that can be used in the test statement to perform string comparison.

Bash shell programming Part II Control statements

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

CS 2505 Computer Organization I Test 1

SOFTWARE ARCHITECTURE 3. SHELL

Shell Start-up and Configuration Files

Transcription:

1. Write a shell script to check whether the name passed as rst argument is the name of a le or directory. Ans: #!/bin/bash if [ -f $1 ] echo "$1 is a le" echo "$1 is not a le" 2. Write a shell script to print the "long list" of all the le names passed at command line. Ans: #!/bin/bash for i in $* ls -l $i 3. Write a shell script to enter a le/directory name at command line. Change its permissions to user - rwx group - rwothers r-- Ans: #!/bin/bash if [ -f $1 ] chmod 764 $1 ls -l $1 elif [ -d $1 ] chmod 764 $1 ls -ld $1 echo "Not a le nor a directory" 4. Write a shell script to create a le and a directory. The name given as rst argument should be used for creating the le and the second argument should be used as the name of directory. touch $1 mkdir $2 1 P a g e

5. Write shell script to create a le inside a directory. The le name should be passed as rst argument and the directory name/path should be passed as second argument. mkdir $2 cd $2 touch $1 6. Write a shell script which checks the total number of arguments passed. If the argument count is greater than 5 display the message "Too many arguments". if [ $# -gt 5 ] echo "Too many arguments" echo "Less arguments" 7. Write a shell script which prints the count of total number of les and directories in any directory (name should be passed at command line) f1=0 d1=0 cd $1 for i in * if [ -f $i ] f1=$(( $f1 + 1 )) elif [ -d $i ] d1=$(( $d1 + 1 )) echo "Number of les: $f1" echo "Number of directories: $d1" 2 P a g e

8. WASS to check if the le names passed at command line by the user exits or not. If yes copy the contents of rst le into another le. if [ -f $1 ] if [ -f $2 ] cat $1 >> $2 echo "Successfully copied" echo "$2 es not exit" echo "$1 es not exist" 9. WASS to ask the user for a name. If the name starts with "f/f" create a le. If the name starts with "d/d" create a directory. For any other entry show a relevant message. Ans: #/bin/bash echo "Enter a name" read name ch=${name:0:1} if [ $ch == "f" ] [ $ch == "F" ] touch $name echo "A le whose name is $name is created" elif [ $ch == "d" ] [ $ch == "D" ] mkdir $name echo "A directory whose is $name is created" echo "You have entered $name" 3 P a g e

10. WASS that reads from command line. tf the argument passed starts with "F/f" a le should be created. if argument starts with "D/d" it should create a directory. Ans: #/bin/bash ch=${$1:0:1} if [ $ch == "f" ] [ $ch == "F" ] touch $name echo "A le whose name is $name is created" elif [ $ch == "d" ] [ $ch == "D" ] mkdir $name echo "A directory whose is $name is created" echo "You have entered $name" 11. WASS that reads from command line. If the number of arguments passed are: 4 - print a long list 5 - simply display the names if [ $# -eq 4 ] for i in $* ls -l $i elif [ $# -eq 5 ] for i in $* echo $i 12. WASS to count the number of sub-directories in a directory. nd. -type d wc -l 4 P a g e

13. WASS to count the number of les having read permission on them in a directory. count=0 for i in * if [[ -f $i ]] && [[ -r $i ]] count=$(( $count + 1 )) echo "Number of les having read permission : $count" 14. WASS to read ve le names from the user. Check if the user has entered the le name "HELLO". Show all the names while searching and Stop the script as soon this le name is encountered Ans : #!/bin/bash for((i=0; i<5; i++)) read NAME[$i] echo "Entries are" for((i=0; i<5; i++)) if [ `nd. -type f -name ${NAME[$i]}` ] if [ ${NAME[$i]} = "HELLO" ] echo ${NAME[$i]} break echo ${NAME[$i]} echo "${NAME[$i]} is not a le" 5 P a g e

15. WASS that reads from command line. Check whether the rst 3 argument names are all of existing les or not. for i in $1 $2 $3 if [ `nd. -type f -name $i` ] echo "$i exists" echo "$i es not exists" 6 P a g e