Topic 8 - Unix Shell Programming Flashcards
What is a shell script?
1) A computer program designed to be run by the Unix shell
2) a text file that can be read and understood by humans and by machines
What does a shell script contain?
1) Unix commands
2) Flow Control command
How are commands executed by the shell?
1) In order as they appear in the file or
2) in an order determined by the flow control statements
Different shells have different ______________
Different shells have different control structure syntaxes
How is a shell script created?
Like all high-level language programs, programmers create shell scripts with a text editor.
What is the main difference between shell scripts and high-level language programs?
Shell scripts do not have to be converted to machine language by a compiler - unix shell acts as an interpreter when reading script files.
What are the main uses of shell scripts?
1) To avoid repetition
2) To automate difficult tasks
There are two methods one can use to inform Unix which shell will be used to run the script. Describe the first method:
Calling the shell followed by the script file name - no need to set the permission of the script to executable.
There are two methods one can use to inform Unix which shell will be used to run the script. Describe the second method:
Invoking the shell as the first working line in the script file (using #!) - permission of the script must be set to executable.
With the second method of running a script, what happens when #1 is not present at beginning of first line?
a default shell will be used to run the script.
What happens if both methods are presented?
The first method has precedence
How can you assign the output of a command to a variable?
Using backquotes - example: #!/bin/sh files=`ls` echo $files - #!/bin/sh value=`expo 23456 + 6789` echo $value
What is the syntax for for loops?
for variable in value1 value 2... ; do ----command_set done; example: for i in 1 2 3; do ----exectueline done;
How do you debug your program?
use the -x when invoking the shell
or -xv
how are conditional statements done in bourne shell script?
Instead of evaluating a boolean variable to true or false, in born shell script, the only thing ou can test is whether a command is “successful” or “not successful”
What are the exit status codes for commands?
0 = successful Non-zero = unsuccessful
What is another way to check the value of the exit status of a program immediately after running the program?
echo $?
Syntax for if-statement?
if decision_command;
then
—-command_set;
fi
Syntax for if-else statement?
if decision_command; then ----command_set; else ----command_set; fi
[NOT A QUESTION] read this example of Nested If:
if decision_command_1; then ----command_set; else ----if decision_command_2; ----then -------command_set2; ----else --------command_set_3; ----fi fi
Syntax for else-if-statement?
if decision_command_1; then ----command_set; elif decision_command_2; ---then -------command_set_2 fi
What is the command to do nothing? You can place this command inside an if-then statement.
”:” aka the colon.
What command can check file type/size, compare strings and compare integer values?
“test”
As a result of check/comparison done by “test”, what is returned?
An exit status code where:
0 = successful
non-zero = unsuccessful
What do each of these test commands with each of their options do? test -f file test -L file test -d file test -x file test -s file test -z string test siring1 = string2 test string1 != string2
test -f file –> does file exist and is not a directory or a link?
test -L file –> does file exist and is a link?
test -d file –> does file exist and is a directory?
test -x file –> does file exist and the executable flag is on?
test -s file –> does file exist and its size is bigger than 0 bytes?
test -z string –> does the length of string equal 0?
test string1 = string2 –> does str1 equal str2?
test string1 != –> does string1 not equal string2?
What do each of these options mean?
- eq,
- ne,
- lt,
- le,
- gt,
- ge
- eq, = true if n1 and n2 are equal
- ne, = true if n1 and n2 are not equal
- lt, = true if n1 is less than n2
- le, = true if n1 is less than or equal to n2
- gt, = true if n1 is greater than n2
- ge = true if n1 is greater than or equal to n2
What is the shorthand notation for the “test” command?
[ ] where the brackets are surrounded by spaces
While-do-done syntax:
While [ ];
do
—command-execute;
done
What is the case-Statement used for?
It supports multi-way branding, based on the value of a single string
What does $0 mean?
name of the running command
What does $* mean?
All the arguments starting from 1$
What does $# mean?
Number of arguments, $0 is not included in the count
What does the shift command do?
Shift all arguments to the left. Results in losing the first argument ($1)