Bourne Shell in Unix Questions

Shellscript3

Question:

Write a shell script to print numbers as 5 4 3 2 1 using while loop.

Sample output:

5

4

3

21

Code:

scriptprog3.sh

i=5
while test $i != 0
do
echo "$i
"
i=$(( $i-1))
done

ShellScript4

Question:

Write a shell script to print the given number in reverse order.  Get input from user and display the result. In case of negative number is given as input, display “Not a positive number” as output.

Sample Input 1:

123

Sample Output 1:

Reverse number is 321

Sample Input 1:

-123

Sample Output 1:
Not a positive number

Code:

scriptprog4.sh

 read n
sd=0
rev=0

if [ $n -lt 0 ]
    then echo "Not a positive number"
    exit
fi

while [[ $n -gt 0 ]]
do
    sd=$(( $n%10 ))
    rev=$(( $rev*10 + $sd ))
    n=$(( $n/10))
done
echo "Reverse number is $rev"

Shellscript5

Question:

Write a shell script to print sum of digits of the given number. Get input from user and display the result. In case of negative number is given as input, display “Not a positive number” as output.

Sample Input 1:

123

Sample Output 1:

Sum of digit for given number is 6

Sample Input 2:

-123

Sample Output 2:

Not a positive number

Code:

scriptprog5.sh

read a
s=0
if [ $a -lt 0 ]
then
echo "Not a positive number"
else
while [[ $a -gt 0 ]]
do
    k=$(( $a%10 ))
    a=$(( $a/10 ))
    s=$(( $k + $s))
done
echo "Sum of digit for given number is $s"
fi 

Script to Count

Question:

Write a shell script which displays the total number of words, characters, lines in a file. Assume the file name is given as command line argument.

Sample Output:

Number of characters in test.txt is 20

Number of words in test.txt is 3

Number of lines in test.txt is 3

Code:

script1.sh

file="$1"
w=`cat $file | wc -w`
c=`cat $file | wc -c`
l=`grep -c "." $file`
echo "Number of characters in $file is $c"
echo "Number of words in $file is $w"
echo "Number of lines in $file is $l"

Script to Check Permission

Question:

Write a shell script to test whether a given

  •  file is directory or not
  •  file has read permission or not
  •  file has write permission or not
  •  file has execute permission or not

Assume file name is given as command line argument.

Sample Output:

Write : yes

Read : yes

Execute : no

Directory : no

Code:

script2.sh

for f in ${*}
do
    [ -w $f ] && W="Write : yes" || W="Write : no"
    [ -x $f ] && X="Execute : yes" || X="Execute : no"
    [ -r $f ] && R="Read : yes " || R="Read : no"
    [ -d $f ] && D="Directory : yes" || D="Directory : no"
    echo "$W"
    echo "$R"
    echo "$X"
    echo "$D"
done

Script to Change Permission

Question:

Write a shell script to change the read and write permission of the file to read,write and execute permission for all.

If file exists display as,

“File Exists!”

else display as,

“File does not Exist”

If file has read and write permission display as,

“File Permission has been changed”

Assume file name is given as command line argument.

Sample Output1:File Exists!
File Permission has been changed

Sample Output2:File does not Exist

Code:

Main.sh

for f in ${*}
do
if [ -e $f ]; then
    echo "File Exists!"
    if [ -w $f ] && [ -r $f ]; then
        chmod a=rwx $f && echo "File Permission has been changed"
        fi
    else
        echo "File does not Exist" && exit
    fi
    done

Recommended:

  1. All programs of Introduction to Unix
  2. All programs of File System
  3. All Programs of Filters
  4. All Programs of Bourne Shell

Leave a Comment

Your email address will not be published. Required fields are marked *

x