Shell Programming Flashcards

1
Q

Shell Programming Topics

A
  • Variables and Environment
  • Parameters and Quoting
  • Grouping
  • Exit Values
  • Testing
  • Repeating
  • Miscellaneous
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Shell Programming:

Variables and

Environment

A
  • Shell supports local variables
    • local to a script
  • Assigning:
    • name = value
  • Use:
    • Reference using ‘$’
    • echo $name
    • Be careful of blanks(unassigned variables)
  • You can export a variable to make it available to any other scripts invoked by the original script:
    • %export MODE = “smart”
    • %script2
    • This exports allows script2 to make use of the MODE variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Variables Inherited

from the Environment

A

File Descriptors:

  • stdin
  • stdout
  • stderr

Locations:

  • current working directory

Variables:

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

Shell Scripts:

Parameters

A

Parameters are passed to a script like command line arguments:

$myScript arg1 arg2 “some string”

The parameters are referenced in the script as numerical variables:

$@ - set of parameters

$1 - first argument

$2 - second argument, etc

Dollar signs trigger parameter substitution

Quotes or backslashes suppress parameter substitution

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

Shell Scripts:

Command Substitution

A

Dollar signs with parenthesis run a command and substitute the stdout

form: $( command )

example:

“ab$(echo hello world)yz”

Will be seen as:

“abhello worldyz”

Text can also use older backquote system

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

Shell Scripts:

Grouping:

Combine Commands on

one line

A

Simple: separate with a semicolon

Example:

thing = “blah blah”; echo $thing

This assigns the variable thing,

then echos it

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

Shell Scripts:

Grouping:

Establish a New Context

A

Use parenthesis to establish a new context.

example:

(cd /home/sarah; pwd; echo “done”)

This changes the working directory, but only within the new context.

Commands only affect the current context.

Only inner context changed working directory.

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

Shell Scripts:

Exit

A

Use command:

exit <status></status>

Status returns 0 on success (True)

nonzero on failure (False)

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

Shell Scripts:

Comments

A

Use the #

example:

echo $myVar #stupid comment

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

Shell Scripts:

Testing:

If Statements

A

do stuff

Nothing around condition, must end with “fi”,

explicitly state “then” and “else”

Form:

if condition

then

else

fi

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

Shell Scripts:

Testing:

Bracket Program

A

The Bracket Program provides some Testing Functions

  • Surround test with brackets
  • [-d file] -> true if file exists and is directory
  • [-f file] -> true if file exists and is not a directory
  • [string1 = string2] -> Strings are identical
  • [string1 != string2] -> Strings are different
  • [file1 -nt file2] -> file1 is newer than file2
  • other flags:
    • -e
    • -r
    • -w
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Shell Scripts:

Repeating:

For Loop

A

Form:

for x in <set></set>

do

….

done

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

Shell Scripts:

Arithmetic:

Evaluate an expression

A

Use the “let” command prior to doing math:

let n=1

let n=($n + 2)*3; echo $n

let ++n ; echo $n

Output:

9

10

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

Shell Scripts:

Including a file

A

Use the “.” command

This includes a file into a script as if it were typed there.

.external_script

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

Shell Scripts:

While-Do Loop Form

A

perform actions

while [condition]

do

done

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

Shell Scripts:

Double Parentheses Construct

(( ))

A

Similar to the let command,

the (( … )) construct permits arithmetic expansion and evaluation.

In its simplest form, a=$(( 5 + 3 )) would set a to 5 + 3, or 8.

Also a mechanism for allowing C-style manipulation of variables in Bash, for example, (( var++ ))

17
Q

Accessing the Clipboard:

Copy and Paste

(Mac OS only)

A

Use the utilities:

pbcopy

pbpaste

Copy the result of pwd command to clipboard as

$ pwd | pbcopy

Use the content in the clipboard as

$ cd $(pbpaste)

18
Q
A