Operating System Lab 03 Shell Script I
Operating System Lab 03 Shell Script I
Operating System Lab 03 Shell Script I
Example 1:
Create a file called test.sh containing the following lines (use any editor like vi):
clear
cd /root
cal
To execute a shell script:
sh your-script-name (e.g.: sh test.sh)
After writing shell script, we may set execute permission for the script as follows using
chmod command:
$ chmod +x your-script-name
$ chmod 755 your-script-name
Note: This will set read write execute (7) permission for owner, for group and
other permission is read and execute only (5).
NOTE: In the last syntax ./ means current directory, But only . (dot) means
execute given command file in current shell without starting the new copy of
shell, The syntax for . (dot) command is: .command-name
Example 2: Write a shell script that will print "Knowledge is Power" on screen.
#
# My first shell script
#
clear
echo "Knowledge is Power"
#End script
Page 1 of 5
Sumanta Das
Dept. of CSE, NSEC [email protected]
Using comment-text
Any text followed by # is considered as comment. Comment gives more information
about script, logical explanation about shell script but not executed.
Note: For shell script file the extension (.sh) should be used to easily identify the
shell script files. The extension is not mandatory.
Example 3:
Script to print user information who currently login, current date & time
Ans:
#
# Script to print user information who currently login , current date & time
#
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0
#End script
At the end of the script, the statement exit 0 is used to sop the script with exit status.
Shell Variables
Page 2 of 5
Sumanta Das
Dept. of CSE, NSEC [email protected]
Arithmetic Operations
Use to perform arithmetic operations use expr command.
Examples:
$ expr 1 + 3
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 6 + 3`
expr 20 %3 # Remainder division. Read as 20 mod 3 and remainder is 2.
expr 10 \* 3 # Multiplication operation. Use \* and not * since it is a wild card.
Page 3 of 5
Sumanta Das
Dept. of CSE, NSEC [email protected]
Note: the expr command uses ` (back quote) sign not the (single quote i.e. ') sign. Back
quote is generally found on the key under tilde (~) on PC keyboard. There are three types
of quotes
" (Double Quotes) - Anything enclose in double quotes removed meaning of that
characters (except \ and $).
' (Single quotes) - Enclosed in single quotes remains unchanged.
` (Back quote) - To execute command
Example:
$ echo "Today is date" # Can't print message with today's date.
$ echo "Today is `date`". # It will print: today's date as, Today is Tue Jan...
Example 4:
#
#Script to read your name from key-board
#
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"
To output Linux-commands result (output of command or shell script) to file. Note that if
file already exist, it will be overwritten without any warning else new file is created.
e.g. ls > myfiles
Now if 'myfiles' file exist in your current directory it will be overwritten without any type
of warning.
Page 4 of 5
Sumanta Das
Dept. of CSE, NSEC [email protected]
In above example sort ($ sort < sname > sorted_names) command takes input from
sname file and output of sort command (i.e. sorted names) is redirected to sorted_names
file.
Wild card
Meaning Examples
/Shorthand
$ ls * will show all files
will show all files whose first name
$ ls a*
Matches any string is starting with letter 'a'
* or group of will show all files having extension
characters. $ ls *.c
.c
will show all files having extension
$ ls ut*.c
.c but file name must begin with 'ut'.
will show all files whose names are
$ ls ?
1 character long
Matches any single
? will show all files whose names are
character.
$ ls fo? 3 character long and file name begin
with fo
Matches any one of
will show all files beginning with
[...] the enclosed $ ls [abc]*
letters a,b,c
characters
A pair of characters
Will show all files name beginning
[..-..] separated by a minus $ ls /bin/[a-c]*
with letter a,b or c like
sign denotes a range.
------o------
Page 5 of 5