Shell Programming Flashcards

1
Q

Sequential Logic

A

to execute commands in the order in which they appear

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

Decision Logic

A

to execute commands only if a certain condition is satisfied

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

Looping Logic

A

to repeat a series of commands for given number of times

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

Case Logic

A

to replace “if then/else if/else” statements when making numerous comparisons

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

Positional Parameters

A

special variables used when shell scrit or shell function is called with argument parameters

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

$#

A

number of input parameters

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

$0

A

Name of the script

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

$1, $2, $3, …. , $9

A

first, second, third, and 9th argument parameter

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

$@

A

List of input parameters

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

$*

A

List of input parameters as space separated string

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

shift

A

shifts the positional parameters by one towards the beginning and drops $1 from the list. After a shift, $2 becomes $1 and so on (note if more than 9 arguments, they cannoted be directly accessed by $1 to $9 . Must use shift command

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

Example of Positional Variables

A
cat myinputs.sh
    #!/bin/sh
    echo Total number of inputs: $#
    echo First input: $1
    echo Second input: $2
$ chmod u+x myinputs.sh
$ ./myinputs.sh ONE TWO BUCKLE MY SHOE
Total number of inputs: 5
First input: ONE
Second input: TWO
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Decision Logic: Conditional

A
if test-command
then
    execute command
elif test-command
then
    execute this
    and execute this command
else
    execute default command
fi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a TEST command?

A

testis a built in bash commands. that returns true (0) ir false (non-zero) for a given set of arguments

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

Arguments structure for TEST

A

Argument structure is:
test item1 comparator item2

or

test option item1

• Instead of calling test, can use square brackets:
[ item1 comparator item2 ]
[ option item1 ]

you must have spaces around the brackets

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

test Options for File Inquiry

A

[ -d filename ] Test if filename is a directory
[ -f filename ] Test if filename is not a directory
[ -s filename ] Test if filename has non zero length
[ -r filename ] Test if filename is readable
[ -w filename ] Test if filename is writable
[ -x filename ] Test if filename is executable
[ -o filename ] Test if filename is owned by the user
[ -e filename ] Test if filename exists
[ -z filename ] Test if filename has zero length
All these conditions return exit status code ($?) of true (0) if satisfied and false (1) otherwise

17
Q

[ -d filename ]

A

Test if filename is a directory

18
Q

[ -f filename ]

A

Test if filename is not a directory

19
Q

[ -s filename ]

A

Test if filename has non zero length

20
Q

[ -r filename ]

A

Test if filename is readable

21
Q

[ -w filename ]

A

Test if filename is writable

22
Q

[ -x filename ]

A

Test if filename is executable

23
Q

syntax for Combining Tests

A

&& represents AND
|| represents OR

Syntax:
if cond1 && cond2 || cond3 …
An alternative form is to use a compound statement in test using
–a and –o keywords. For example:
if [ cond1 –a cond2 –o cond3 … ]
where cond1, cond2, cond3 are either commands returning a
value or test conditions of the form:
test arg1 op arg2 or [ arg1 op arg2 ]
24
Q

Looping Logic

A

A loop is a block of code that is repeated a number of times. Either:
– A pre-determined number of times determined by a list of items
in the loop count ( for loops), or
– Until a particular condition is satisfied ( while and until
loops)

To provide flexibility to the loop constructs there are also
two statements for control:
– continue : skips to the next item in a for loop
– break : exits out of a loop

25
Q

for Each Loops

A
for arg in list_args
do
     command1
     command2
     command3
done

where the value of variable arg is set to the values provided in
list_args one at a time and the block of statements is
executed. This is repeated until the list is exhausted.

26
Q

Iterating through for loops

A

Common programming practice to iterate through index

for (int index = 0; index <= 10; index++)...

In bash script, we can use sequence expression

{istart..iend[..incr]} where [..incr] is an
optional step size

for index in {1..10}; do…

27
Q

In terms of output to terminal:
$ ls -l > flist.txt; grep -e “-rwxrw—-“ flist.txt
is equivalent to
$ ls -l | grep -e “-rwxrw—-“

A

Instead of a program writing data to a file on disk and the next reading it, keep information in a buffer and pass it along

Difference in resulting file system and speed!
1. First command creates a file (flist.txt) in the local directory
2. Writing to/reading from a file is slow compared to accessing
memory

28
Q

man ls > ls.txt; wc < ls.txt

Standard IO redirect to (disk)

A
  1. ‘man ls’ is sent to stdout buffer
  2. then the buffer put the command to ‘ls.txt’ which is on the DISK
  3. then the txt file (DISK) is put into the stdin buffer
  4. The ‘wc’ command is then executed on the stdin buffer
  5. finally outputs to the stdout bufffer
29
Q

man ls | wc

piping

A
  1. man ls is put into stdout buffer memory
  2. stdout buffer moves to stdin buffer
  3. ‘wc’ executes on the information coming from stdin buffer
  4. wc takes its output to the stdout buffer and is done.

Disk is never accessed, all data is accessed through buffer

30
Q

grep -e

A

-e option is to exclude

31
Q

chmod can be set with :

A

incremental permission change (+) or (-)
chmod ug+x myscript.sh
chmod 0-rwx myscript.sh

setting permission with “=”

perform multiple setting with “,”

32
Q

$ grep -i “juliet” shakespeare.txt > names.log
$ grep -i “romeo” shakespeare.txt&raquo_space; names.log
$ sort < names.log > names_sort.log
$ uniq < names_sort.log > names_uniq.log
$ wc –l < names_uniq.log

A

grep -i : case insensitive option, “juliet” and create names.log and second line just appends to the names.log for “romeo”

sort will put all the output in order by line

uniq will delete duplicate any lines where romeo and juliet are in the same line and leaving only 1 instance isntead of 2

uniq will delete any two lines that are the same next to each other.

wc -l : prints out to

33
Q

&laquo_space;HERE or <

A

will direct input (terminal or script) to stdin

34
Q

2> &1

A

redirect stderr to stdout

& means “same place as”

35
Q
Examples:
string1='15L Fina Review!!'
string2="Good luck on finals!!"
var2='$string1'
var3="$string2"

echo var2
echo $var2
echo $var3

A

Output:

var2
$string1
Good luck on finals!!

line1: without the quotes it literally echos
line2:

36
Q

Example:

var1=7
var2=expr $var1 + 7
var3=expr $var1+7
var4=”expr $var1 + 7”

echo $var2
echo $var3
echo $var4
var3=

A

Output:

12
5+7
expr 5 + 7

line1: runs the expression and command in actually adding it. You do need spaces for EXPR cmd
line2: doesn’t uses spaces so it just prints out the string
line3: it doesn’t know to treat expr as a command since its using “ “ instead of ` `

37
Q

Example:

var1=7
var3=$[$var1+8]
var5=$(($var1 + 7))

echo $var3
echo $var5

A

Output:

15
14

bracketts are for older syntax, but (( )) is more associated with expr

when brackets don’t need spaces
when (( )) NEED spaces since its interpreted like ` `

38
Q

echo is sueful when trying to debug scripts

echo [options] stringtoprint

using “ “ is weakly quotes

using ‘ ‘ is strongly quoted

In weakly quoted strings, references to variables are replaced y the value of those variables before the output

A

common options

  • e : expand \ special characters
  • n : do not output a new-line at the end

”” : will dereference the variable and give the value

’ ‘ : will be taken literally nothing is treated as special

` ` : enclosed string is treated as a command and shell attempts to execute it, if successful the primary output from command replaces the string