Q3 1-37 Flashcards
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 commandsh 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 metuntil–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 Xbobecho $Xbob
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 * 6need 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 stringdoes string length = 0test string = string2test 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 LLb) 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/shcleardatew
In Bourne shell, if x=10, what you will get if you execute echo $x$x; echo x$x$
1010x10$
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 + 10expr 5 + 10expr $x + 1015
Explain what happens when you execute the following commands: new_command=ls echo $new_command echo ”$new_command” echo ’$new_command’ echo $new_command
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
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_filea3a b cnew_fileb2b 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/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
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 patternls -al | grep “*”
Write a Bourne shell script called s that displays the name of your login shell.
!/bin/shecho $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/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 + 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/shsum=0while [ $# -gt 0 ]do sum=expr $sum + $1
if [ $# -gt 1 ] then echo -n ““$1” + “ else echo ““$1” = “$sum”” fi shiftdone
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/shsum=0until [ $# -eq 0 ]do sum=expr $sum + $1
if [ $# -gt 1 ] then echo -n ““$1” + “ else echo ““$1” = “$sum”” fi shiftdone
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
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