Scripting Basics Flashcards

1
Q

Write out the basic structure of a bash script

A
#!/bin/sh  #this points to the intrpreter to use
echo "Hello World!"
exit 0 #The return value for the script
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do we make a script an executable

A

chmod +x name-of-script.sh

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

How do run a script

A

./name-of-script.sh

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

What type of file extension does a script need to be saved as

A

.sh

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

What is the shell used to run scripts

A

Bourne

If you want to use a different shell (e.g. zsh, bash), you need to update the script to know to use that environment.

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

Can you run a script that doesn’t have the opening #!/bin/sh

A

Yes. You can run it using the sh command.

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

What’s the comment character for Bourne scripting

A

#

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

Declare a shell variable

A

v1=”Hello World”

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

What type of value are shell variables

A

Strings.

Shell scripting does not support other type of variables

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

How do you use a variable once it’s declared

A

$variableName e.g. $v1

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

How do you determine the length of a string

A

${variableName} e.g. ${#v1}

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

What does a null value mean in shell scripting

A

An empty string

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

Explain the following variable modifier:

v2=${v1:-foo}

A

If v1 is not empty, use v1; else use the text ‘foo’

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

Explain the following variable modifier:

v2=${v1:=bar}

A

If v1 is not empty, use v1; else assign ‘bar’ to v1 and use it

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

Explain the following variable modifier:

v2=${v1:?}

A

If v1 is not empty, use v1; else exit with an error message

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

How would you store the output of a command into a variable?

A

You would create a vriable and then surround the command in backticks

num=echo $v1 | wc -w

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

How can you perform arithmatic since shell variables are stored as strings

A

Use the expr command

v2=3 #3 is stored as a string valueexp
expr $v2 + 1 #returns 4

Or you can use the following shorthand:

v3=$((v3 + 1)) #

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

What operators do we have to chain commands together

A

&& #If the first command runs, run the next one also
command1 && command2 # if command1 succeeds, run command2 also

|| #If the first command fails, run the next one
command1 || command3 # if command1 fails, run command3

19
Q

What’s the syntax for grouping commands together

A

Wrap in curly braces. Seperate commands with a newline or semicolon. If a semi colon is used, ensure the is a space afterwards.

[ ‘id -u’ -eq 0 ] || { echo “root only!”; exit 1; }

20
Q

Wildcard characters

A

? A single character

* Multiple Characters

21
Q

Escape character

A

\

22
Q

Home path characyer

A

~

23
Q

Is there a difference between single quoutes and double quotes

A

Yes. Single quotes indicate literal assignment, Double quotes allows for enhanced features (e.g. escaped whitespaces, variable name exansion).

The backslash escape character works with double quotes, not single quotes.

When in doubt, go with double quotes.

24
Q

How can you assign the output of a command to a variable

A

Use backticks.

e.g. dirlist=ls -alt

25
Q

Write a shell script function named say

A
#!/bin/sh
say()
{    
v1="Veni  Vidi  Vici"
echo $v1
}
say
exit  0
26
Q

What are positional parameters

A

Used with functions to pass in additonal information.

The position they are assinged indicates what parameter is assigned.

Positional parameters are read only.

27
Q

How can you break out of a function

A

Use the return keyword and exit code. e.g.

return 0

If another code apart from 0 is used, the function has failed

28
Q

Special variable for process id of current shell process

A

$$

29
Q

Special varaiable for return value of last command

A

$?

30
Q

Special varaiable for name used to invoke script

A

$0

31
Q

Special variable for number of arguments

A

$#

32
Q

Special variable for consolidaed list of argumetns

A

$ *

33
Q

Special variable for iterable list of all arguments

A

$@

34
Q

Basic structure for a conditional statement containing an if, else if and else condition

A

if condition1; then

elif condition2; then

else

fi

35
Q

Basic structure for an if then statement

A

if [ -f /etc/passwd ]; then
echo “/etc/passwd exists”
fi

36
Q

Basic structure for a case statement

A

case “$var” in
[aA][bB][cC]*)

;; 123|xyz)

;;  *)

;; esac
37
Q

When reading variables, is there a differnece if you quote the values e.g. “$v1” vs $v1

A

Yes. If the unquoted variable contains embedded white space between the values, it will not print out the value as a string.

38
Q

Write a basic while loop that counts from 0 to 9

A
n=0
while [ $n -lt 10 ]; do
    echo $n
    n=`expr $n + 1`
done
39
Q

Write a basic for loop to check if files exit

A

for f in /etc/fstab /etc/passwd; do
[ -f $f ] && “$f exists” || “$f does not exist”
done

40
Q

How can you exit a loop

A

Use the break command

41
Q

What does the shift command do

A

It decrements the number of positional parameters in a function/loop

42
Q

How would you set up a script to use a different interptreer

A

Modify the opening instruction to the interpreter you want

#!/usr/bin/env  
# Example: #!/usr/bin/env zsh
43
Q

What is an alias

A

A set of commands assigned to a variable

e.g. alias dir=”ls -l”

44
Q

How do you exercise commands located in another script within the current shell

A

use the . command

. otherscript
# Execute statements in otherscript within the current shell