bash scripting Flashcards

1
Q

Line at beginning of shell script file

A

!/bin/bash

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

print statement

A

echo “hello”

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

get user input (name)

A

read PERSON
echo “Hello, $PERSON”

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

command line arguments

A

The command-line arguments $1, $2, $3, …$9 are positional parameters, with $0 pointing to the actual command, program, shell script, or function and $1, $2, $3, …$9 as the arguments to the command.

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

variables

A

name = “ricky”
echo “Hello, $name”

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

Initialize array with values

A

NAME[0]=”ricky”
NAME[1]=”matt”
NAME[2]=”joe”

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

access array values

A

echo “First Index: ${NAME[0]}”

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

access all the items in an array

A

${array_name[*]}

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

How to perform arithmetic ops

A

val=expr 2 + 2
echo “Total value : $val”

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

if statement

A

if [[ $NUM -gt 5 ]]
then
echo “greater than 5”
elif [[ $NUM -eq 5 ]]
then
echo “the number is 5”
else
echo “the number is less than 5”
fi

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

while loops

A

a=0
while [ “$a” -lt 10 ] # this is loop1
do
b=”$a”
done

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

for loop

A

NUMS=”1 2 3 4 5 6 7”

for NUM in $NUMS
do
# this stuff
done

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

shell substitutions

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

what does command substitution do?

A

used to assign the output of a command to a variable

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

command sub example

A

USERS=who | wc -l
echo “Logged in user are $USERS”

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

character escape

A

backslash \

17
Q

what do single quotes do?

A

all special chars between them lose their special meaning

18
Q

what do backtics do?

A

Anything in between back quotes would be treated as a command and would be executed.

19
Q

output redirection

A

echo “Hello!” > file.txt
cat file.txt
Hello!

20
Q

output redirection with append

A

> > instead of >

21
Q

input redirection

A

wc -l < users
#displays how many users there are

22
Q

discard output

A

command > /dev/null

23
Q

functions

A

Hello () {
echo “Hello”
}

invoke function with ./prog_name.sh

24
Q

pass parameters to function

A

./func.sh ricky matt joe

25
Q

function that takes args

A

echo “The first two args were ${1} and ${2}”