Bash Script Flashcards

1
Q

What is set command?

A

set returns all environment variables + extras like functions etc which env would not return

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

What is $#?

A

Number of all parameters

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

What is $0, $1

A

Program name, first argument

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

What is $*

A

All arguments

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

How to print parameter 12?

A

${12}

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

What does this do?
var=”one two three four five”
set – $var

A

sets the input parameters as one two three four five

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
for i
do
echo $i
done
A

when range is neglected the for loop loops through the input parameters so $i is $1, $2 etc

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

Difference between echo $varname and echo “$varname”?

A

Same if there are no multiple white spaces. In case there are, $varname will print only 1 space while “$varname” will print all the spaces.

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

hello=”K L M”
1. Output: echo “$hello”
2. Output: echo $hello
3. Output: echo ‘$hello’

A
  1. K L M
  2. K L M
  3. $hello
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

IFS=’!’ read a b
echo a is $a
echo b is $b

  1. input = kostas mylonas
  2. input = kostas!mylonas
A
  1. a is kostas mylonas
    b is

2.
a is kostas
b is mylonas

Default IFS is whitespace

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

str=”kostas”
echo ${str^^}

A

KOSTAS

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

str=”KOSTAS”
echo ${str,,}

A

kostas

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

str=”KOStas”
echo ${str,}

A

kOStas

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

str=”kOstAs”
echo ${str^}

A

KOstAs

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

anArray=(‘el1’ ‘el2’ ‘el3’)
1. echo ${anArray[@]}
2. echo ${!anArray[@]}
3. echo ${#anArray[@]}

A
  1. whole array: el1 el2 el3
  2. array indices: 0 1 2
  3. size of array: 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

a=2
let b = a+2
Will it work? If not what should be changed to make it work

A

No, space is not allowed. If you want to use space then include evreything in “” like
let “b = a+2”

17
Q

How to properly use expr command?

A

Always put it inside ` ` and spaces are mandatory!
e.g.
a =expr 3 + 5
a =expr $k + 3

18
Q

string=ABigString
pos=4
length=6
z=expr substr $string $pos $length
echo $z

Output?

19
Q

What is
“echo $?”

A

Prints the exit status of the last executed command

20
Q

set – echo "p1 p2 p3 p4 p5 p6"

for j in ${*:2:3}
do
echo -n $j
done
echo

A

p2p3p4

“Start from 2nd param and use up untill 3 params”

20
Q

set – echo "p1 p2 p3 p4 p5 p6"

for j in ${*:2}
do
echo -n $j
done
echo

A

p2p3p4p5p6

“Start from 2nd param and go till the end”

21
Q

!export my_new_env_var=”Harry”

A

create env var only for current session. If you want it globally then you have to go through bashrc

22
Q

How to multiply using expr?

A

a=expr 5 \* 3
# 15 – with expr in multiplication use *

23
Q

z=expr substr $string $pos $length

A

from string start from pos and take up to length chars
ABigString pos = 4 len = 6 will give
gStrin

24
A=0.04 B=0.03 let "comp=`echo $A-$B\>0 | bc`"
True
25
a=1000 b=$(echo "scale=3; $a/100" | bc)
10.000
26
multiline comments in bash?
#! /bin/bash : ' This is the segment of multi-line comments Through this script, you will learn How to do multi-line commenting '
27
How to call a bash script from python
import subprocess subprocess.call(['sh', './exitStatus.sh'])
28
if [ "$age" -gt 18 ] && [ "$age" -lt 40 ] if [[ "$age" -gt 18 && "$age" -lt 40 ]] if [ "$age" -gt 18 -a "$age" -lt 40 ] Are these three the same?
Yes
29
How to right if (count > 9.8) in bash?
if (( $(bc <<<"$count > 9.8" ))) first write as normal and then append $(bc <<< and fix parenthesis
30
How to check if string $a is alphabetically smaller than $b?
if [ $a /< $b ]
31
How to check if string a is not null and how if its null?
if [ -n "a" ] if [ -z "a" ]
32
if [ -e file ]
check if file exists
33
if [ -r file ]
check if user has read permissions on file
34
for j in ${*:3}
from second parameter to the end
35
for j in ${*:3:3}
from 3d parameter and use 3 parameters 3d 4th 5th