Scripting Flashcards

1
Q

What is the first line of a bash shell script?

A

!/bin/bash

Tells shell what program to interpret the script with

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

How do you exit with a specific exit code in a shell script?

A

exit 0
exit $?
*numbers 0-255, 0 means no errors, $? evaluate last line as exit code

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

What is the command to load variable into the shell from a shell script?

A

source scriptname.sh
. scriptname.sh

*. does the same thing

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

In bash, what is the stored variable containing the number of arguments the script was run with?

A

$#

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

In bash, what is the stored variable that contains the scripts own filename?

A

$0

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

In bash, what is the stored variable containing script arguments?

A

$1..$n

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

How do you access stored script only variables when in quotes?

A

${}

i.e $# > echo “${#}”

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

How do you access stored variables when in quotes?

A

$()

i.e date > echo “$(date)”

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

How do you write a function in a bash script?

A
function multiply() {
     echo 5 * $1
}
*call function
multiply 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the $PATH variable?

A

Colon separated listing of directories that commands are found

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

How do you modify the $PATH variable?

A

append: PATH=$PATH:/directory
prepend: PATH=/directory:$PATH

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

How do you record a terminal session?

A

script

ctrl + d / exit to commit to file

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

How do you record a terminal session in real time?

A

script –timing=

scriptreplay -s -t

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

How do you record the output of a command non-interactively?

A

script -c ‘ls /’ scriptfilename

*useful for scripting as is non-interactive

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

What is the command to create aliases?

A

alias lr=’ls -R’
unalias lr
*unalias removes an alias binding

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

How do you create persistent aliases across shell sessions?

A

Edit file ~/.bashrc
Append alias commands
* ~/.bashrc is evaluated at shell launch