12 Compound commands Flashcards

1
Q

Lists

A

A list is a sequence of one or more pipelines separated by “;”, “&”, “&&” or “||”, and optionally terminated by one of “;”, “&” or end-of-line. The return value of a list is that of the last command executed in it.

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

Execute list in a subshell to contain state changes

A

( ⟨list⟩ ) executes list in a subshell (e.g., to contain state changes)

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

Group a list to override operator priorities

A

{ ⟨list⟩ ; } groups a list (to override operator priorities)

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

For loop

A

for ⟨variable ⟩ in ⟨words ⟩ ; do ⟨list ⟩ ; done

for f in *.txt ; do cp $f $f.bak ; done

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

Conditionals

A

if ⟨list⟩ ; then ⟨list⟩ ; elif ⟨list⟩

then ⟨list⟩ ; else ⟨list⟩ ; fi

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

While

A

while ⟨list⟩ ; do ⟨list⟩ ; done until ⟨list⟩ ; do ⟨list⟩ ; done

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

Ignore next linefeed

A

single backslash at end of line

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

case

A

case ⟨word ⟩ in ⟨pattern⟩|⟨pattern⟩|. . . )
⟨list⟩ ;; …
esac

Matches expanded ⟨word⟩ against each ⟨pattern⟩ in turn (same matching rules as pathname expansion) and executes the corresponding ⟨list⟩ when first match is
found

case "$command" in
      start)
app_server &
        processid=$! ;;
      stop)
        kill $processid ;;
      *)
        echo 'unknown command' ;;
    esac
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Boolean tests – inverse logic

A
[  ] 
e.g. 
-e  = file exists
-d ... = exists + directory 
...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

String comparisons lexico

A

⟨string1⟩ == ⟨string2⟩

⟨string1⟩ != ⟨string2⟩

⟨string1⟩ < ⟨string2⟩

⟨string1⟩ > ⟨string2⟩

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

Aliases allow a string to be substituted for the first word of a command:

A

$ alias dir=’ls -la’

$ dir

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

Shell functions

A

Shell functions are defined with “⟨name⟩ () { ⟨list⟩ ; }”. In the function body, the command-line arguments are available as $1, $2, $3, etc. The variable $* contains all arguments and $# their number.
[unalias dir]
$ dir () { ls -la $* ; }

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

Function variable access

A

Outside the body of a function definition, the variables $*, $#, $1, $2, $3, . . . can be used to access the command-line arguments passed to a shell script.

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