Shell Scripting Flashcards

1
Q

What should the first line be in a bash script?

A
#!/bin/bash
*the shebang as the first line in the script tells bash what scripting language is being used
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you run a script without specifying a path, and calling like a native command?

A

Move script to one of the directories contained within the $PATH env variable
i.e /home//bin

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

How do you evaluate (run) a command in quotation marks?

A

$()

i. e echo “$(ls)”
* `` - legacy: i.e ls will substitute value of ls

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

How do you make a script executable?

A

chmod +x scriptname

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

How do you write an if statement in bash?

A

if [ ]; then
>command<
fi

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

How do you write an if else statement in bash?

A
if [  ]; then
    >command<
else
    >command<
fi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you write a for loop in bash?

A

for i in {1..10}
do
echo “$i”
done

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
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
9
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
10
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
11
Q

What is an alternative way to write a variable within quotes?

A
${}
i.e var=tree
echo "${var}s"
*useful for string seperation
"$vars" won't work
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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
13
Q

How do you run a command sequentially after another?

A

;

i.e pwd; ls

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

How do you run a command sequentially only if the first was successful?

A

&&

i.e ls -lh file && cat file

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

How do you run a command sequentially only if the first was unsuccessful?

A

||

i.e ls nonexistant || touch nonexistant

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