Study Questions #3 - Topic 8 Flashcards

2
Q

Is it possible to execute a shell Bourne script if you are not given execute access permission? If yes, how?

A

Yes it is possible. You just need to execute the commandsh shell_script_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is it possible to execute a shell csh script if you are not given execute access permission? If yes, how?

A

Yes it is possible. tcsh shell_script_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the command that used to make a file an executable file?

A

chmod u+x schell_script_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you debug a shell script?

A

To debug your program use -x or -xv when you invoke the shell. ex. sh -x shell_script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In Bourne-style shells, what is the difference between the while and until loops?

A

while–do–done loops repeat statements as long as the while condition is metuntil–do–done loops repeat statements till the until condition is met

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In Bourne-style shells, what is the command that used to read input from the standard input? Give an example

A

The read command reads one line of input from the standard input and assigns its words to variables given as arguments.read Xbobecho $Xbob

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In Bourne-style shells, what is the Unix command that adds two integer numbers? Give an example.

A

expr 5 + 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In Bourne-style shells, what is the Unix command that multiplies two integer numbers? Give an example.

A

expr 5 * 6need the \ because * is reserved for something else in UNIX

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In Bourne-style shells, how do you compare two numeric values? Give an example.

A

[5 -lt 6]-eq-ne-lt-gt-le-ge

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In Bourne-style shells, how do you compare two strings? Give an example.

A

test -z stringdoes string length = 0test string = string2test string != string2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Consider script.sh that contains the following script #!/bin/sh echo $0 Explain the output when you execute script.sh ABC

A

output would be ./script.sh because echo $0 echoes the filename of the current script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

a) sh LLb) chmod u+x LL; LL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

!/bin/shcleardatew

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In Bourne shell, if x=10, what you will get if you execute echo $x$x; echo x$x$

A

1010x10$

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

expr 5 + 10expr 5 + 10expr $x + 1015

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Explain what happens when you execute the following commands: new_command=ls echo $new_command echo ”$new_command” echo ’$new_command’ echo $new_command

A

lsls$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

18
Q

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

A

new_filea3a b cnew_fileb2b c

19
Q

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).

A

!/bin/shfile=read fileif test -x “$file” ; thenecho “$file” has execute permission.fiif [-r $file] ; thenecho “$file” has read permission.fiif [-s $file] ; thenecho “$file” exists.fi

20
Q

Write a Bourne shell script called executable that lists the names of all executable files in the current directory.

A

grep –> search a file for a patternls -al | grep “*”

21
Q

Write a Bourne shell script called s that displays the name of your login shell.

A

!/bin/shecho $SHELL

22
Q

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

A

!/bin/shsum=0counter=1for i in $*do sum=expr $sum + $i if [$counter -lt $#] then echo -n ““$i” + “ else echo ““$i” = “$sum”” fi counter=expr $counter + 1done

23
Q

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

A

!/bin/shsum=0while [$# -gt 0]do sum=expr $sum + $1 if [$# -gt 1] then echo -n ““$1” + “ else echo ““$1” = “$sum”” fi shiftdone

24
Q

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

A

!/bin/shsum=0until [$# -eq 0]do sum=expr $sum + $1 if [$# -gt 1] then echo -n ““$1” + “ else echo ““$1” = “$sum”” fi shiftdone

25
Q

Explain what happens when you execute the following shell script #!/bin/sh # 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

A

grep -s suppresses error messages about nonexistant or unreadable fileswhen this script is executed, the user is prompted for a namewho 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 loggedotherwise, no such user name is echoed

26
Q

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?

A

!/bin/sh#echo “Are you okay? “echo -n “Input Y for yes and N for no: “read answerif [$answer = N -o $answer = y]thenecho “Good”elif [$answer = N -o $answer = n]then echo “Boo”else echo “Y or N”fiIt is case sensitive.Above changes makes it case insensitive.Prompts user for input then checks answer and outputs accordingly.

27
Q

Explain what happens when you execute the following commands: pwd 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

A

print working directorymake new dirchange to new dircreate new file and keep writing input to new file until “+”make new file executablelist all files in new dirgo up one directoryremove new directory entryprint working directory

28
Q

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

A

checks current hour.if

29
Q

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 < data_file

A

A A B B C CA A B B C C1 1 2 2 3 31 1 2 2 3 3while there is line to be read, each word assigned to variable separated by spaceecho 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

30
Q

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 < data_file

A

A A B C B CA A B C B C1 1 2 3 2 31 1 2 3 2 3same as before:while there is line to be read, each word assigned to variable separated by spaceecho 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 outputthe difference here is that there is no cso echo $c echoes nothing. and instead of $a = a $b = b $c = c $a = 1 $b = 2 $c =3now it is$a = a $b = b c$a = 1 $b = 2 3

31
Q

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 < data_file

A

A B C A B CA B C A B C1 2 3 1 2 31 2 3 1 2 3same as before:while there is line to be read, each word assigned to variable separated by spaceecho 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 outputdifference 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

32
Q

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

A

make new directorychange to new directoryprint working directory saved to new filemake link to new fileremove new fileconcatenate contents of link to new filechange to home directoryrecursively remove new directory and contents

33
Q

Consider following Unix commands cat < script_2.sh #!/bin/sh echo $0 + cat script_2.sh Did you find the content of script_2.sh as you typed? Explain why. How do you fix the above write up so script_2.sh to contain what you typed.

A

No. I got this instead of what I typed:#!/bin/shecho -tcshThis is because $0 returns the filename of the current script and tcsh was the filename at the time of the write up of the scriptto get what you typed, you should fix script_2.sh to this:#!/bin/shecho $0

34
Q

Write a Bourne shell script that displays all command line arguments, even if they are more than 9 arguments. Hint: use shift command.

A

!/bin/shwhile [$# -gt 0] ; do echo -n “$1 “ shiftdoneecho

35
Q

Write a Bourne shell script that accepts from the command line three integer numbers and sort them from the largest to the smallest.

A

!/bin/shecho $1 > list23echo $2 >> list23echo $3 >> list23sort -n list23rm list23

36
Q

Write a Bourne shell script that interactively reads from the user three integer numbers and sort them from the largest to the smallest.

A

!/bin/shread a b cecho $a > list23echo $b >> list23echo $c >> list23sort -n list23rm list23

37
Q

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.

A

!/bin/shfor file in $1do for file2 in $2 do diff file file2 > difference if test -s $difference then rm file2 fi donedone

38
Q

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.

A

!/bin/shnum=wc -l $1median=0lineCount=0echo $num | tr -d “[A-Z] [a-z]” > abe4num=cat abe4if [expr $num % 2 -eq 0]then median=expr $num / 2else median=expr $num / 2 + 1fisort -n $1 > abe4while read linedo lineCount=expr $lineCount + 1 if [$lineCount -eq $median] then echo $line fidone < abe4rm abe4