Bash Scripting Flashcards

1
Q

What do you put at the beginning of bash scripts?

A

!/bin/bash

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

Where should you place a script if you want it to be executable from anywhere?

A

/home/user/bin

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

What do you have to do to the script myscript before you can run it?

A

chmod u+x myscript

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

myvar = $1

A

Will assign the value of the first argument to myvar

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

$#

A

Refers to the total number of arguments

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

How to do a numerical comparison with an if statement in a bash script

A

if (( $var == 0 ))

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

What does an if block end with?

A

fi

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

if test -f file

A

Tests if $file is a file

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

if [ -f $file ]

A

Tests if $file is a file

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

if [ -z “$string” ]

A

Checks if $string is empty

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

if statement flag for greater than

A

-gt

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

if statement flag for less than

A

-lt

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

if statement flag for greater than or equal

A

-ge

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

if statement flag for less than or equal

A

-le

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

if statement flag for equal

A

-eq

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

if statement flag for not equal

A

-ne

17
Q

if statement flag for testing if directory

A

-d

18
Q

if statement flag for testing if symlink

A

-l

19
Q

What does putting a command between ` ` do in a bash script?

A

Executes the command in a new shell then immediately closes the shell

20
Q

Which brackets must you use in order to use regular expressions and && and stuff?

A

[[ ]]

21
Q

if statement flag for &&

A

-a

22
Q

if statement flag for ||

A

-o

23
Q

for statement syntax

A

for a in b
do
blahblah
done

24
Q

while statement syntax

A

while [some condition];
do
blahblah;
done

25
Q

case statement syntax

A
case $var in
1) first
do this thing;;
2) second
do this other thing;;
*)
this is the default
esac
26
Q

read -n2 var

A

will read in 2 characters and put them in var

27
Q

read -t

A

If input is not given within a certain time frame the script will move on

28
Q

if [ -x $file ]

A

If $file exists and is executable

29
Q

seq 10

A

Will print 1 through 10 in sequential order

30
Q

seq 1 5 20

A

Will print 1-20 in increments of 5