Topic 8 - Unix Shell Programming Flashcards

1
Q

What is a shell script?

A

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

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

What does a shell script contain?

A

1) Unix commands

2) Flow Control command

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

How are commands executed by the shell?

A

1) In order as they appear in the file or

2) in an order determined by the flow control statements

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

Different shells have different ______________

A

Different shells have different control structure syntaxes

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

How is a shell script created?

A

Like all high-level language programs, programmers create shell scripts with a text editor.

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

What is the main difference between shell scripts and high-level language programs?

A

Shell scripts do not have to be converted to machine language by a compiler - unix shell acts as an interpreter when reading script files.

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

What are the main uses of shell scripts?

A

1) To avoid repetition

2) To automate difficult tasks

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

There are two methods one can use to inform Unix which shell will be used to run the script. Describe the first method:

A

Calling the shell followed by the script file name - no need to set the permission of the script to executable.

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

There are two methods one can use to inform Unix which shell will be used to run the script. Describe the second method:

A

Invoking the shell as the first working line in the script file (using #!) - permission of the script must be set to executable.

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

With the second method of running a script, what happens when #1 is not present at beginning of first line?

A

a default shell will be used to run the script.

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

What happens if both methods are presented?

A

The first method has precedence

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

How can you assign the output of a command to a variable?

A
Using backquotes - example:
#!/bin/sh
files=`ls`
echo $files
-
#!/bin/sh
value=`expo 23456 + 6789`
echo $value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the syntax for for loops?

A
for variable in value1 value 2... ;
do
----command_set
done;
example:
for i in 1 2 3;
do
----exectueline
done;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you debug your program?

A

use the -x when invoking the shell

or -xv

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

how are conditional statements done in bourne shell script?

A

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”

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

What are the exit status codes for commands?

A
0 = successful
Non-zero = unsuccessful
17
Q

What is another way to check the value of the exit status of a program immediately after running the program?

A

echo $?

18
Q

Syntax for if-statement?

A

if decision_command;
then
—-command_set;
fi

19
Q

Syntax for if-else statement?

A
if decision_command;
then
----command_set;
else
----command_set;
fi
20
Q

[NOT A QUESTION] read this example of Nested If:

A
if decision_command_1;
then
----command_set;
else
----if decision_command_2;
----then
-------command_set2;
----else
--------command_set_3;
----fi
fi
21
Q

Syntax for else-if-statement?

A
if decision_command_1;
then
----command_set;
elif decision_command_2;
---then
-------command_set_2
fi
22
Q

What is the command to do nothing? You can place this command inside an if-then statement.

A

”:” aka the colon.

23
Q

What command can check file type/size, compare strings and compare integer values?

A

“test”

24
Q

As a result of check/comparison done by “test”, what is returned?

A

An exit status code where:
0 = successful
non-zero = unsuccessful

25
Q
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
A

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?

26
Q

What do each of these options mean?

  • eq,
  • ne,
  • lt,
  • le,
  • gt,
  • ge
A
  • 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
27
Q

What is the shorthand notation for the “test” command?

A

[ ] where the brackets are surrounded by spaces

28
Q

While-do-done syntax:

A

While [ ];
do
—command-execute;
done

29
Q

What is the case-Statement used for?

A

It supports multi-way branding, based on the value of a single string

30
Q

What does $0 mean?

A

name of the running command

31
Q

What does $* mean?

A

All the arguments starting from 1$

32
Q

What does $# mean?

A

Number of arguments, $0 is not included in the count

33
Q

What does the shift command do?

A

Shift all arguments to the left. Results in losing the first argument ($1)