Bash CSH Perl KSH

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

ITU UNIX Operating System

Assignment #1
Practice creating and using Basic Scripts
1. A shell is !th a c!mmand line interpreter and a pr!gramming
language. "#plain the di$$erence et%een the $!ll!%ing shells&
bash csh
perl ksh
In y!ur h!me direct!ry' ma(e a in sudirect!ry )i$ it d!esn*t e#ist+
mkdir bin
,! int! that sudirect!ry cd bin
-. In y!ur in sudirect!ry' create the $!ll!%ing $ile named commands
using "macs !r .i )!r any te#t edit!r+
# a simple shell script
cal
date
who
/. Sa.e the $ile' 0ea.e the edit!r' try t! run the script commands
1hat happens2
3. 4a(e the script e#ecutale chmod ug+x commands
5. N!% try t! run the script commands
6. 4a(e sure the ash shell is al%ays e#ecuted %hen y!u run this
script
,! ac( int! the "dit!r and add this statement t! the t!p !$ the $ile
#!/bin/bash
POSITIONA0 PA7A4"T"7S
1hen y!u call a shell script %ith arguments' y!u can access them inside
the script %ith the special .ariales
$1, $2, etc.. $* re$ers t! all !$ the arguments
The .ariale $0 re$ers t! the name !$ the calling pr!gram !r script.
8. 9reate the $!ll!%ing script called echoargs
#!/bin/bash
# illustrate the use o! positional parameters
echo $0 $1 $2 $" $# $$ $% $& $' $(
:. 4a(e the script e#ecutale and try it !ut %ith this c!mmand&
echoargs )o be or not to be
;. 9hange the script s! it l!!(s li(e this&
#!/bin/bash
# illustrate the use o! positional parameters, user de!ined
# *ariables and the read command
echo +what is ,our name-+
read name
echo .well, $name, ,ou t,ped $# arguments/.
echo .$*.
1<. Sa.e the $ile' e#it and try !ut the script
echoargs )o be or not to be
T=" S"T 9O44AN> ta(es the !utput !$ a c!mmand and st!res it in the
p!siti!nal parameters
11. Try !ut the date c!mmand date
Numer the di$$erent pieces !$ the !utput 1' -' / etc.
1-. Set the p!siti!nal parameters t! the !utput !$ date
set $0date1
1/. 0!!( at the .alue !$ the p!siti!nal parameters n!%&
echo $1 $2 $" $# $$ $%
13. 4a(e a ne% script $ile called setdate %ith the $!ll!%ing lines&
#!/bin/bash
# demonstrate the set command in a script
set $0date1
echo .)ime/ $# $$.
echo .2a,/ $1.
echo .2ate/ $" $2 $%.
15. Sa.e the $ile' ma(e it e#ecutale' run the $ile
APP0I9ATION&
The %c )?%!rd c!unt?+ $ilters and c!unts the %!rds' lines' and characters
in a $ile. @!r e#ample' try running %c !n the setdate $ile y!u Aust made&

$ wc setdate
% 2" 12# setdate
)B& %hat p!siti!nal parameter is the $ilename in the !utput2+
The !utput tells us there are 6 lines' -/ %!rds and 1-3 characters. T!
impr!.e !n %c' y!u are g!ing t! ma(e a ne% script called m,wc' %hich
%ill pr!.ide a m!di$ied !utput laeling each p!rti!n s! it is m!re
readale. The !utput !$ y!ur script m,wc' used in the $!ll!%ing
c!mmand
m,wc setdate sh!uld read
setdate count3 lines/ % words/ 2" characters/ 12#
T! d! this' create the script m,wc using the $!ll!%ing plan&
1+ Select the ash shell.
-+ 7un %c !n the C1 p!siti!nal parameter and capture the !utput using
set
/+ Print the $ilename )C3+
3+ Print the numer !$ lines )C1+' numer !$ %!rds )C-+ and numer !$
characters )C/+ using appr!priate laels.
5+ Use y!ur script setdate as a m!del $!r this pr!lem.
6+ Paste y!ur $inished script el!%.

You might also like