You are on page 1of 7

Assignment 6

NAME: Shubham Kashyap

REG NO: 20204197

GROUP: C1

1. Write a shell script that print the sum of three numbers which collects
arguments from command line and hold these arguments in positional
variable. The program must print how many arguments has been
collected and print these values

CODE:

fo()
echo "the total arguments are:- $#
ech

echo "Enter the thee numbers :-


read num1 num2 num
echo "Entered numbers are:- $num1 $num2 $num3
ans=`expr $num1 + $num2 + $num3
echo "The sum is : $ans
fo num1 num2 num

OUTPUT:-
}

"

"

"

"

2. Write a shell script that nd the average of numbers that takes input
from command line arguments

CODE:

echo "Enter three numbers:


read a b
sum=`expr $a + $b + $c
avg=`expr $sum / 3
echo "Sum = $sum
echo "Average = $avg

OUTPUT:-

3. Write a shell script that run the following command “ls -l” and “cat /
home/a.txt” and print there respective exit status.
c

"

fi
"

4. Write a shell script that run the following command “who | grep user” (made of
two separate commands who and grep) and print the exit status of above combined
command . You need to identify this exit status for which command.

//Exit status of who command is


// Exit status of user is

5. Write a shell script that takes input “ls” “-l” (seperately) from command line. Print
the following

1. PID of shell executing the script

2. PID of last background proces

3. Print last argument

4. Print exit status of above comman


:

6. Write a shell script to change permission as an executable of given les


(more than two) as command line argument

CODE:

le="$1

if [ -e $ le ] then echo "File Exists!

elif [ ! -e $ le ] then echo "File does not Exist" && exi

if [-w $ le ] && [ -r $ le ] then chmod 777 $ le && echo "File Permission has
been changed
fi
fi

fi

fi
fi
-

"

fi
"

fi
"

fi
t

fi
7. Write a shell script to take a string as input and print the output of
following operatio
e.g. String=ABCDEEW1231sdfsadfas23
1. echo ${string:5
2. echo ${string:7:0
3. echo ${string:7:-2
4. echo ${string: -7:-2

CODE:
echo "Enter the string:-
read strin
echo ${string:5
echo ${string:7:0
echo ${string:7:-2
echo ${string:-7:-2

OUTPUT:

8. Write a shell script that take input a string of 10 character (e.g. array=(a b c d A 4 5
3 2 w)) from command line. Print the value of ${array[@]:7}. And this can be done
shift operation. Extend the program using shift operation for same output

CODE:

echo -n "Enter the Total numbers :


read
echo "Enter numbers:
i=
0

"

"

"

while [ $i -lt $n
d

read a[$i
i=`expr $i + 1
don

echo ${a[@]:7
echo "print the same output with shift operation:-
set -- "${a[@]:7}
while (( $# )
d
echo "$1
shif
don

OUTPUT:-
o

"

"

"

9. Write a shell script that takes an unspeci ed number of command line


arguments (up to nine) of integers and nd their sum

CODE:

sum=

for i in $@
do sum=$((sum+i))
echo $sum
don

echo $su

exit
0

fi
fi
.

You might also like