shell basics Flashcards

Learning to use bash and python scripting

1
Q

How do you start a script?

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

how do you tell your interpreter which scripting language you wanna use?

A

After the Shabang (#!) you type it in.

i.e. you can be like /bin/bash to tell it you are using bash

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

how to make a comment?

A

then write after the hash symbol

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

what does echo do?

A

echo writes arguments to standard output (1).

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

how do you execute a script via the terminal?

A

./file_name

so period then the backslash followed by file_name

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

how do you make a file to an executable if it is not already?

A
chmod 755 file_name
defines who can do what with the file. 
7 means read/write/execute. 
6 means read/write. 
5 means read/execute. 
4 means read only.
3 means write/execute.
2 means write only.
1 means execute only.
0 means cant do anything.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what do variables start with?

A

letter or underscore and by convention their lowercase.

Except if they are environment variables then all caps.

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

how would you create a variable for a name.

A

variable_name=”Saidname”

no spaces

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

how do you declare a constant?

A

declare -r NUM1=5

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

how do you display the output of an algorithm? lets say 2*10 as example

A
echo $(( 2*10))
or
num2=2
num10=10
echo $(( num2*num10))

works with numbers or variables

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

how can you use floating point numbers in your shell script?

A

you can call python in your code and assign the output to a variable to be displayed or used later

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

how do you call python in a bash script?

A

python -c “here use python”

i.e. num9= $(python -c “print 3.1+4.5”)
echo $num9

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

what does cat do?

A

prints a file or any string you pass to it.

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

how to create a function in a script?

A
function_name(){
   //stuff inside the function
  return
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to call a function previously declared?

A

simply call the function_name

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

how do you use the attributes (arguments) passed to a function?

A

use $1 or $2 or $3 ect.

it depends on how many attributes are sent to the function. but the numbers correlate with the

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

how do you “return” a value after function is used.

A

you pass back whatever value you wish to return by saying echo $variable_name

18
Q

how do you assign a variable the returning value of a function call?

A

variable=$(function arg1 arg2 ect)

then you can echo the variable to display it.

19
Q

how to prompt a user for input?

A

read -p “question or prompt” store_variable

the -p says we want the input of whatever is said AFTER the prompt

20
Q

how to say greater then or equal to in the script?

21
Q

how to say equal to in the script?

22
Q

how to say not equal to in the script?

23
Q

how to say greater than in the script

24
Q

how to say less than or equal to in the script

25
how to say less than in the script
-lt
26
how to say less than or greater than using < or >?
you can use < or > if they are within double parentheses (())
27
how to make a file in a directory
-d sample_dir
28
how to delete a file
rm sample_file
29
how to delete a directory
rmdir sample_dir
30
how to check if a file exists?
if [ -e "$file" ]; then echo "$file exists" fi
31
check if a valid expression
lets say we want to validate the date entered by user. then you can say: check="^[0-9]{8}$" variable check means that user should enter 8 numbers from 0-9. the ^ means its the beginning of string and the $ means its the end of string
32
what is the IFS?
input field separator. you can decide what you want the special symbol that separates your arguments to be (for example the ',' or ' ' or '.')
33
how would you get rid of all white spaces in a string?
variable = ${variable//[[:blank:]]//} | this replaces all blanks with nothing.
34
in a string how would you replace 'the' with 'a'?
variable="the string to test" | echo "${variable}//the/a}"
35
how to get the length of a string?
str="string of something" | length=${#str}
36
how do you grab everything after, lets say the letter 'a' in as string?
str="a good question to ask" | mod=${str#*a}
37
how to end an if statement?
fi
38
how to end a while loop?
done
39
how to end a case statement?
esac
40
how to end an until loop?
done
41
how to output things inside a file?
while read arg1 arg2 arg3; do printf "Arg1: ${arg1}\nArg2: ${arg2}\nArg3: ${arg3}\n" done < sample.txt ** < pipes the text file to this line