SHELL PROGRAMS

 

How to run a Shell Script

        Edit and save your program using editor

        Add execute permission by chmod command

        Run your program using the name of your program

./program-name

Important Hints

      No space before and after the assignment operator               Ex. sum=0

      Single quote ignores all special characters. Dollar sign, Back quote and Back slash are not ignored inside Double quote. Back quote is used as command substitution. Back slash is used to remove the special meaning of a character.

      Arithmetic expression can be written as follows : i=$((i+1) or i=$(expr

$i + 1)

      Command line arguments are referred inside the program as $1, $2, ..and so on

      $* represents all arguments, $# specifies the number of arguments

Syntax for if statement

if [ condition ] then

...

elif [ condition ] then

else fi...

...


Syntax for case structure

case value in pat1) ...

pat2) ...

*) ...

esac

Syntax for for-loop

statement;;

Statement;;

Statement;


    for var in list-of-values

do


            Syntax for printf statement

printf “string and format” arg1 arg2 … …

      Break and continue statements functions similar to C programming

      Relational operators are         –lt, -le, -gt, -ge, -eq,-ne

      Ex. (i>= 10) is written as [ $i -ge 10 ]

      Logical operators (and, or, not) are                           -o, -a, !

      Ex. (a>b) && (a>c) is written as [ $a –gt $b –a $a –gt $c ]

      Two strings can be compared using = operator

 

Q1. Given the following values

num=10,         x=*,     y=`date`          a="Hello, 'he said'"

 

Execute and write the output of the following commands

 

Command

Output

echo num

 

echo $num

 

echo $x

 

echo ‘$x’

 

echo “$x”

 

echo $y

 

echo $(date)

 

echo $a

 

echo \$num

 

echo \$$num

 

 

Q2. Find the output of the following shell scripts

$ vi ex51

echo Enter value for n

read n

sum=0

i=1

while [ $i –le $n ]

do

sum=$((sum+i))

i=$((i+2))

done

echo Sum is $sum

 

Output :


Q3. Write a program to check whether the file has execute permission or not. If not, add thepermission.

$ vi ex52

Q4. Write a shell script to print a greeting as specified below.

If hour is greater than or equal to 0 (midnight) and less than or equal to 11 (up to 11:59:59), "Good morning" is displayed.

If hour is greater than or equal to 12 (noon) and less than or equal to 17 (up to 5:59:59 p.m.), "Good afternoon" is displayed.

If neither of the preceding two conditions is satisfied, "Good evening" is displayed.

$ vi ex53

hour=$(date | cut -c12-13)

if [ "$hour" -ge 0 -a "$hour" -le 11 ]

then                                 

Q5. Write a shell script to list only the name of sub directories in the present working directory

$ vi ex54


Q6. Write a program to check all the files in the present working directory for a pattern (passed through command line) and display the name of the file followed by a message stating that the pattern is available or not available.

$ vi ex55

Post a Comment

Previous Post Next Post