Study questions covering Unix Chapter 8 Flashcards
Unix Midterm part 3 of 3
Is it possible to execute a shell Bourne script if you are not given execute access permission? If yes, how?
Yes it is possible. You just need to execute the command
sh shell_script_name
Is it possible to execute a shell csh script if you are not given execute access permission? If yes, how?
Yes it is possible.
tcsh shell_script_name
What is the command that used to make a file an executable file?
chmod u+x schell_script_name
How do you debug a shell script?
To debug your program use -x or -xv when you invoke the shell.
ex. sh -x shell_script
In Bourne-style shells, what is the difference between the while and until loops?
while–do–done loops repeat statements as long as the while
condition is met
until–do–done loops repeat statements till the until condition is met
In Bourne-style shells, what is the command that used to read input from the standard input? Give an example
The read command reads one line of input from the standard input and assigns its
words to variables given as arguments.
read X
bob
echo $X
bob
In Bourne-style shells, what is the Unix command that adds two integer numbers? Give an example.
expr 5 + 2
In Bourne-style shells, what is the Unix command that multiplies two integer numbers? Give an example.
expr 5 * 6
need the \ because * is reserved for something else in UNIX
In Bourne-style shells, how do you compare two numeric values? Give an example.
[5 -lt 6]
- eq
- ne
- lt
- gt
- le
- ge
In Bourne-style shells, how do you compare two strings? Give an example.
test -z string
does string length = 0
test string = string2
test string != string2
Consider script.sh that contains the following script
#!/bin/sh
echo $0
Explain the output when you execute script.sh ABC
output would be ./script.sh because echo $0 echoes the filename of the current script
Write a Bourne shell script named LL that lists your current directory in a long format.
a. Execute LL using the sh command (i.e., sh ./LL)
b. How do you execute LL again without using sh command
a) sh LL
b) chmod u+x LL; LL
Write a Bourne script file that performs the following:
• Clearing the screen
• Showing the current date and time
• Showing the current number of users on the system
!/bin/sh
clear
date
w
In Bourne shell, if x=10, what you will get if you execute echo $x$x; echo x$x$
1010
x10$
What is the output of the following shell script? #!/bin/sh x=5 echo expr $x + 10 echo ”expr $x + 10” echo ’expr $x + 10’ echo `expr $x + 10`
expr 5 + 10
expr 5 + 10
expr $x + 10
15
Explain what happens when you execute the following commands: new_command=ls echo $new_command echo ”$new_command” echo ’$new_command’ echo `$new_command`
ls
ls
$new_command
#odd_prn# #what_is_ph.c# LL LL~ Lab3_script_outputs Working-Area a.out abc bup bup~ dir1 dir2 error error_file_1 error_file_2 folder hello hello.c hello.c~ hello2 lab5_1.c lab5_1.c~ lab5_2.c lab5_2.c~ lastarg letter.txt letter2.txt man numbas~ numbersfile nums nums~ odd_prn old_nums output_and_error_file_1 output_and_error_file_2 output_file_1 output_file_2 postMidnight postMidnight~ prn_triangle prn_triangle~ prog-A prog-B prog-C prog1 public_html script.sh simple.c test test.c test.c~ test1 test2 test~ vm what_is_ph.c
If you have a shell script called new_file as listed below:
#!/bin/sh # echo $0 echo $1 echo $# echo $*
shift
echo $0
echo $1
echo $#
echo $*
Explain what happens when you execute the following commands:
new_file a b c
new_file a 3 a b c new_file b 2 b c
Write a Bourne shell script called fi1e_checker that reads a filename from the standard input and produces the
properties of that file (e.g., exists, readable, executable).
#!/bin/sh file= read file if test -x "$file" ; then echo "$file" has execute permission. fi if [ -r $file ] ; then echo "$file" has read permission. fi if [ -s $file ] ; then echo "$file" exists. fi
Write a Bourne shell script called executable that lists the names of all executable files in the current directory.
grep –> search a file for a pattern
ls -al | grep “*”
Write a Bourne shell script called s that displays the name of your login shell.
#!/bin/sh echo $SHELL
Write a Bourne script file that sums the numbers passed to it as arguments on the command line and displays the
results. Use a for loop in your program. If this program is called SUM, and you execute
SUM 10 20 30
the program should display the following:
10 + 20 + 30 = 60
#!/bin/sh sum=0 counter=1 for i in $* do sum=`expr $sum + $i` if [ $counter -lt $# ] then echo -n ""$i" + " else echo ""$i" = "$sum"" fi counter=`expr $counter + 1` done
Write a Bourne script file that sums the numbers passed to it as arguments on the command line and displays the
results. Use a while loop in your program. If this program is called SUM, and you execute
SUM 10 20 30
the program should display the following:
10 + 20 + 30 = 60
#!/bin/sh sum=0 while [ $# -gt 0 ] do sum=`expr $sum + $1` if [ $# -gt 1 ] then echo -n ""$1" + " else echo ""$1" = "$sum"" fi shift done
Write a Bourne script file that sums the numbers passed to it as arguments on the command line and displays the
results. Use a until loop in your program. If this program is called SUM, and you execute
SUM 10 20 30
the program should display the following:
10 + 20 + 30 = 60
#!/bin/sh sum=0 until [ $# -eq 0 ] do sum=`expr $sum + $1` if [ $# -gt 1 ] then echo -n ""$1" + " else echo ""$1" = "$sum"" fi shift done
!/bin/sh
Explain what happens when you execute the following shell script
#
echo ”Please enter a name: ”
read name
if who | grep -s $name > /dev/null then echo $name is logged else echo no such user $name fi
grep -s suppresses error messages about nonexistant or unreadable files
when this script is executed, the user is prompted for a name
who lists all the users currently logged in and that output is pipelined to grep which searches the list for a pattern, in this case the $name of the person.
if the person is found, the person is said to be logged
otherwise, no such user name is echoed
Trace and explain the following shell script.
#!/bin/sh
#
echo
echo ”Are you OK? ”
echo -n ”Input Y for yes and N for no: ”
read answer
if test ”$answer” = Y then echo ”Glad to hear ! ” elif test ”$answer” = N then echo ”Go home! ” else echo ”Your answer should be Y or N” fi echo
Is this script case insensitive to your input?
If not, then how do you modify it to make it case insensitive?
#!/bin/sh # echo "Are you okay? " echo -n "Input Y for yes and N for no: " read answer
if [ $answer = N -o $answer = y ] then echo "Good" elif [ $answer = N -o $answer = n ] then echo "Boo" else echo "Y or N" fi
It is case sensitive.
Above changes makes it case insensitive.
Prompts user for input then checks answer and outputs accordingly.
Explain what happens when you execute the following commands:
pwd
mkdir new_dir
cd new_dir
cat
print working directory
make new dir
change to new dir
create new file and keep writing input to new file until "+" make new file executable list all files in new dir go up one directory remove new directory entry print working directory
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
checks current hour.
if
Consider that 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 c
do
echo $a $a $b $b $c $c
echo $a $a $b $b $c $c
done | tr a-z A-Z
Trace and explain the output of the following command:
script.sh
A A B B C C
A A B B C C
1 1 2 2 3 3
1 1 2 2 3 3
while there is line to be read, each word assigned to variable separated by space
echo is self explanatory, it echoes whatever the input read was.
the output for the echo is pipelined into translate program which translates lower case to upper case letters. this explains why a b c became A B C in the output
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 | tr a-z A-Z
Trace and explain the output of the following command:
script.sh
A A B C B C
A A B C B C
1 1 2 3 2 3
1 1 2 3 2 3
same as before:
while there is line to be read, each word assigned to variable separated by space
echo is self explanatory, it echoes whatever the input read was.
the output for the echo is pipelined into translate program which translates lower case to upper case letters. this explains why a b c became A B C in the output
the difference here is that there is no c
so echo $c echoes nothing. and instead of
$a = a $b = b $c = c
$a = 1 $b = 2 $c =3
now it is
$a = a $b = b c $a = 1 $b = 2 3
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
do
echo $a $a $b $b $c $c
echo $a $a $b $b $c $c
done | tr a-z A-Z
Trace and explain the output of the following command:
script.sh
A B C A B C
A B C A B C
1 2 3 1 2 3
1 2 3 1 2 3
same as before:
while there is line to be read, each word assigned to variable separated by space
echo is self explanatory, it echoes whatever the input read was.
the output for the echo is pipelined into translate program which translates lower case to upper case letters. this explains why a b c became A B C in the output
difference is $b and $c echo nothing.
$a = a b c $a = 1 2 3 now instead of
$a = a $b = b $c = c $a = 1 $b = 2 $c =3
Trace and explain the following shell script #!/bin/sh mkdir new_dir cd new_dir pwd > new_file ln new_file new_file_2 rm new_file cat new_file_2 cd ../ rm -r new_dir
make new directory change to new directory print working directory saved to new file make link to new file remove new file concatenate contents of link to new file change to home directory recursively remove new directory and contents
Consider following Unix commands
cat
No. I got this instead of what I typed: #!/bin/sh echo -tcsh This is because $0 returns the filename of the current script and tcsh was the filename at the time of the write up of the script
to get what you typed, you should fix script_2.sh to this:
#!/bin/sh echo \$0
Write a Bourne shell script that displays all command line arguments, even if they are more than 9 arguments.
Hint: use shift command.
#!/bin/sh while [ $# -gt 0 ] ; do echo -n "$1 " shift done echo
Write a Bourne shell script that accepts from the command line three integer numbers and sort them from the largest
to the smallest.
#!/bin/sh echo $1 > list23 echo $2 >> list23 echo $3 >> list23 sort -n list23 rm list23
Write a Bourne shell script that interactively reads from the user three integer numbers and sort them from the largest
to the smallest.
#!/bin/sh read a b c echo $a > list23 echo $b >> list23 echo $c >> list23 sort -n list23 rm list23
Write a Bourne shell script that accepts two directory names, dir1 and dir2, and deletes the files in dir2 that are
identical to their namesakes in dir1.
#!/bin/sh for file in $1 do for file2 in $2 do diff file file2 > difference if test -s $difference then rm file2 fi done done
Write a Bourne shell script called median that takes one argument (input-filename) and gives the median
number of the numbers in the provided file. Create a file called input-filename. Write the following numbers in
file, one number in each line, (3, 6, 9, 11, 3, 4, -8, -10, 0, 16, 5). Test your scrip using the data
file you have created. Hint: you may want to utilize sort and wc commands in your code.
#!/bin/sh num=`wc -l $1` median=0 lineCount=0 echo $num | tr -d "[A-Z] [a-z]" > abe4 num=`cat abe4` if [ `expr $num % 2` -eq 0 ] then median=`expr $num / 2` else median=`expr $num / 2 + 1` fi sort -n $1 > abe4 while read line do lineCount=`expr $lineCount + 1` if [ $lineCount -eq $median ] then echo $line fi done
include
Write a Bourne shell script that processes every file with name ended by .c in the current directory by searching
inside it for the keywords printf or fprintf. If found, the script should add the statement
at the beginning of the file, if, and only if, the file does not already have it, regardless of the number of spaces
between #include and
-
Write a Bourne shell script that causes the following output (below) to be displayed. The number of column should
be taken as input. For example, if the input to the program is 6, the program should produce the following output:
0 1 2 3 4 5 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5
#!/bin/sh col=0 row=0 while [ $row -lt $1 ] do col=$row while [ $col -lt $1 ] do echo -n "$col " col=`expr $col + 1` done echo row=`expr $row + 1` done
Write a Bourne shell script that causes the following output (below) to be displayed. The number of column should
be taken as input. For example, if the input to the program is 6, the program should produce the following output:
5 4 5 3 4 5 2 3 4 5 1 2 3 4 5 0 1 2 3 4 5
#!/bin/sh col=`expr $1 - 1` row=`expr $1 - 1` while [ $row -lt $1 -a $row -ge 0 ] do col=$row while [ $col -lt $1 -a $col -ge 0 ] do echo -n "$col " col=`expr $col + 1` done echo row=`expr $row - 1` done
Write a Bourne shell script that causes the following output (below) to be displayed. The number of column should be taken as input. For example, if the input to the program is 6, the program should produce the following output: * *** ***** ******* ********* ***********
#!/bin/sh row=1 col=`expr 2 \* $row - 1` maxCol=`expr 2 \* $1 - 2` space=`expr $maxCol / 2` co=1
while [ $row -le $1 ] do #print spaces while [ $space -gt 0 ] do echo -n " " space=`expr $space - 1` done
#print col while [ $co -le $col ] do echo -n "*" co=`expr $co + 1` done
echo
co=1 row=`expr $row + 1` col=`expr $col + 2` maxCol=`expr $maxCol - 2` space=`expr $maxCol / 2` done
Write a Bourne shell script that computes the factorial of a positive integer. The number should be taken as input
from the standard input.
#!/bin/sh result=1 read fac while [ $fac -gt 1 ] do result=`expr $result \* $fac` fac=`expr $fac - 1` done echo $result
Write a Bourne shell script that computes the factorial of a positive integer. The number should be taken as an inline
parameter.
#!/bin/sh result=1 fac=$1 while [ $fac -gt 1 ] do result=`expr $result \* $fac` fac=`expr $fac - 1` done echo $result
Write a Bourne shell script that takes a positive integer n as an argument and tell if n is prime, or not.
#!/bin/sh numFac=0 x=1 y=`expr $1 / 2`
while [ $x -le $y ] do if [ `expr $1 % $x` -eq 0 ] then numFac=`expr $numFac + 1` fi x=`expr $x + 1` done
if [ $numFac -eq 1 ] echo prime else echo not prime fi
Write a Bourne shell script that takes a positive integer as input and returns the leading (the most significant) digit.
For example, the leading digit of 234567 is 2.
!/bin/sh
echo Enter an interger value
read int
echo $int | cut -c 1
Write a Bourne shell script that takes two parameters, n and k and returns the k th digit (from the right) in n (a positive integer). For example, if the parameters are 829 and 1, it returns 9. If the parameters are 829 and 2, it returns 2. If the parameters are 829 and 3, it returns 8. If k is greater than the number of digits in n, have the script return 0
#!/bin/sh echo Enter an integer value n read int echo Enter an integer k read int2
len=expr length $int
if [ $int2 -gt $len ] then echo 0 exit 1 fi
digit=expr $len + 1 - $int2
echo $int | cut -c $digit
Write a Bourne shell script that takes three parameters, month, day, and year, and returns the day of the year (an
integer between 1 and 366).
#!/bin/sh month=`expr $1 - 1` day=$2 year=$3 x=0 case $month in 1) x=31 ;; 2) x=`expr 31 + 28` ;; 3) x=`expr 31 + 28 + 31` ;; 4) x=`expr 31 + 28 + 31 + 30` ;; 5) x=`expr 31 + 28 + 31 + 30 + 31` ;; 6) x=`expr 31 + 28 + 31 + 30 + 31 + 30` ;; 7) x=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31` ;; 8) x=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31` ;; 9) x=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30` ;; 10) x=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31` ;; 11) x=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30` ;; esac
x=expr $x + $day
echo $x
Write a Bourne shell script that prints the prime numbers below n, where n is an input parameter.
#!/bin/sh numFac=0 x=1 a=1 y=`expr $x / 2` maxX=$1 nmP=0
while [ $a -lt $maxX ]
do
x=1
y=expr $a / 2
while [ $x -le $y ] do if [ `expr $a % $x` -eq 0 ] then numFac=`expr $numFac + 1` fi x=`expr $x + 1` done
if [ $numFac -eq 1 ] then nmP=`expr $nmP + 1` fi numFac=0 a=`expr $a + 1` done