CS2211a Lab No. 4 Introduction to UNIX Solved

30.00 $ 15.00 $

Category:

Description

The objective of this lab is:

o to practice Unix Shell Programming

If you would like to leave, and at least 30 minuteshave passed, raise your hand and wait for the TA.

Show the TA what you did. If, and only if, you did a reasonable effort during the lab, he/she will give you the lab mark.

====================================================================================

  1. Consider the following shell script Q1

#!/bin/sh x=$2

echo  $1      $x $* echo `$1      $x $*` echo ”$1      $x $*” echo ’$1      $x $*’ echo $# shift

echo  $1      $x $* echo `$1      $x $*` echo ”$1      $x $*” echo ’$1      $x $*’

echo $#

Understand and trace the script using –x and –xv options when calling it with the following arguments (try to expect the output before you execute the command):

  1. Q1 pwd echo
  2. Q1 echo pwd
  3. #!/bin/sh X=$1 echo $X shift while [ $# -gt 0 ]; do   echoConsider the shell script Q2: $1   if [ $1 -gt $X ]; then     X=$1     echo ”   << $X”   fi   shift done echo ”—–”;  echo $X

Understand and trace the script using –x and –xv options when calling it with the following arguments (try to expect the output before you execute the command):

Q2 11 4 19 7 31 49 1 6

 

Copyright © 2015 Mahmoud El-Sakka.

  1. Change your current shell to Bourne shell (by executing sh command). Delete any file or directory called ~/abc,  what is the printable output of the following Unix commands:

cd; mkdir abc;   ABC=abc;   echo “ls -a $ABC”;   echo `ls -a \$ABC | wc -w`

  1. Explain what happens when you execute the following shell script

#!/bin/sh # echo hour=`date +%H` if [ ”$hour” -lt 12 ] then

echo ”GOOD MORNING” elif [ ”$hour” -lt 18 ]   then

echo ”GOOD AFTERNOON”   else

echo ”GOOD EVENING” fi echo

 

If you decided not to use “elif”, what you should change in the program to keep it works the same way.

  1. Consider you executed the following command:

(echo a b c; echo 1 2 3) > data_file

Also consider that you have a shell script called script.sh as listed below:

#!/bin/sh while read a b  do

echo $a $a $b $b $c $c   echo $a $a $b $b $c $c done

Trace and explain the output of the following command: script.sh < data_file | tr ’a-z’ ’A-Z’

  1. Explain what happens when you execute the following commands: pwd rm –r new_dir mkdir new_dir cd new_dir

 

cat <<+ > new_file

#!/bin/sh

 

echo ”I am inside new_file”

echo ”Current directory is `pwd`”

+

chmod u+x new_file

/bin/ls –l `/bin/ls` cd .. rm -r new_dir pwd

  1. Write a Bourne shell script that takes a seconds as a positive integer argument (less than 86400) representing the number of seconds since midnight and returns the equivalent time in hours (0-23), minutes (0-59), and seconds (0-59), respectively.