W1: Linux - Bash Language Flashcards

1
Q
  1. Keywords: bash
A

GNU Bourne-Again SHell
sh-compatible language interpreter, executes commands read from an input .sh filew

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Keywords: Global Variables
A

Global variables can be used by all Bash scripts on your system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Keywords: Local Variables
A

Local Variables can only be used within the script (or shell) in whech they’re defined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Keywords: printenv
A

prints environment variables, which are the values kept in the machine that are in use by programs running in terminals/subshells.

ALSO prints previously specified environment variables in the shell.

aka attributes with a title and a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Keywords: Index/associative arrays
A

array: stores multiple data.
with index and the value of each array element accessed by its corresponding index value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Keywords: for, while, until loops
A

N=10
for i in $(seq 1 $N); do
echo “$i-th run”
done

i=0
while [ $i -lt 100 ]; do
i=expr $i + 1
echo $i
done

i=0
until [ $i -ge 100 ]; do
i=expr $i + 1
echo $i
done

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Keywords: break,
A

break 1

terminates current loop, passes program control to the command that follows the terminated loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Keywords: continue
A

continue 1

skips the remaining commands in the current loop iteration, passes program control to the next loop iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Keywords: select
A

https://linuxhint.com/bash_select_command/

used in scripts for menu creation
#!/bin/bash
echo “Which Operating System do you like?”

Operating system names are used here as a data source
select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP
do

case $os in
# Two case values are declared here for matching
“Ubuntu”|”LinuxMint”)
echo “I also use $os.”
;;

Three case values are declared here for matching
“Windows8” | “Windows7” | “WindowsXP”)
echo “Why don’t you try Linux?”
;;

Matching with invalid data
*)
echo “Invalid entry.”
break
;;
esac

done

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Keywords: PS3
A

PS1: primary prompt string
PS2: secondary prompt string
PS3: this parameter’s value is used as prompt for the select command
PS4:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Keywords: unset
A

unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks

https://unix.stackexchange.com/questions/382391/what-does-unset-do

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Keywords: if statement
A

!/bin/bash

# Basic if statement
if [ 1000 > 100 ]
then
echo Hey that\'s a large number.
pwd
fi
date
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. Keywords: case statement
A

!/bin/bash

simplifies complex conditions with multiple different choices. easier to read and maintain than nested if.

echo “Which color do you like best?”
echo “1 - Blue”
echo “2 - Red”
echo “3 - Yellow”
echo “4 - Green”
echo “5 - Orange”
read color;
case $color in
1) echo “Blue is a primary color.”;;
2) echo “Red is a primary color.”;;
3) echo “Yellow is a primary color.”;;
4) echo “Green is a secondary color.”;;
5) echo “Orange is a secondary color.”;;
*) echo “This color is not available. Please choose a different one.”;;
esac

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

!/bin/bash

BashFunction () {
echo Hello, my linux friends
}

BashFunction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. Keywords: /dev/stdin
A

standard input stream file. what’s input there is what’s input to the console,

what’s input to stdout is console output

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. What is the meaning of #!/bin/bash
A

“shabang”.

inscructs the program loader to use the /bin/sh program instead of any other, passing the oath of the script as the first argument.

17
Q
  1. How do you declare local/global vars?
A

global variables are declared outside of the function block. local variables are declared inside the function block

other source claims: all variables in bash are global by default,
set a variable as local x=$1 or sush to be a local variable.

18
Q
  1. How do you make multiline comments?
A

: ‘
this will not get executed

19
Q
  1. How do you access associative array fields?
A

!/bin/bash

BashFunction () {
declare -A assArray1
assArray1[fruit]=Mango
assArray1[bird]=Cockatail
assArray1[flower]=Rose
assArray1[animal]=Tiger

declare -A assArray2=( [HDD]=Samsung [Monitor]=Dell [Keyboard]=A4Tech )

echo ${assArray1[bird]}
echo ${assArray2[HDD]} }

BashFunction

20
Q
  1. What does the following command do?

echo $(elements[-1]}

A

supposedly prints the last element in the array

21
Q
  1. How do you iterate through an array’s elements in a for loop?
A

arr=( “apple” “banana” “cherry” )

for item in “${arr[@]}”
do
echo $item
done

22
Q
  1. Given the following arrays:
    my_array=(3 4 5 6)
    or
    my_array=(3,4,5,6)
    What will the following commands return?

echo ${#my_array[@]}
echo ${#my_array[*]}

A

!/bin/bash

provides the length of the element,

elements need to be seperated by spaces. the second ‘array’ simply consists of a single element ‘3,4,5,6’

testSeven () {
my_array=(3 4 5 6)
echo ${#my_array[@]}
echo ${#my_array[]}
echo stops here
my_array=(3,4,5,6)
echo ${#my_array[@]}
echo ${#my_array[
]}
}
testSeven

4
4

1
1

23
Q
  1. What is the meaning of the following switches within the square bracket of an ‘if’ statement? -d -w -x -a
A

https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
-d : true if file exists and is a directory
-w : true if file exists and is writable
-x : true if file exists and is executable

24
Q
  1. How do you check if a variable is set?
A

-v variable
-z${variable}

25
Q
  1. How would you return a result of a function?
A

bash functions don’t return a value when called.bash function return value is the status of the last statement executed in the function.

easily just set a global variable to the ‘return’ value. technically all variables in bash are global, play with that as you will.

26
Q
  1. Is it possible to write a function with the name cp?
A

apparently yes

27
Q
  1. How would you read a password variable from STDIN, keeping it hidden?
A

!/bin/bash

secretInput () {
echo “Please enter your password”;
read -s password;
#pass = $(</dev/stdin);

echo break;
echo $password; }

secretInput

28
Q
  1. How many times will the following loop run?

counter=1
while [ $counter -le 10 ]
do
. . .
done

A

ad infinitum

29
Q
  1. How many times will the following loop run?

counter=1
until [ $counter -le 10 ]
do
. . .
done

A

it will not.

as long as the test case is true, it won’t start

30
Q
  1. When would you choose “while loop” and “until loop”?
A

since until won’t run until it is false, you use until

31
Q

Exercise 1.
Write a function in a bash script file, that returns the number of lines in that file.

A

!/bin/bash

function countMe () {
wc -l < countMe.sh
}
lineCount=$(countMe)
echo $lineCount
#beep boop bop
#line 9

32
Q
  1. Please assign the value of the command ls /usr | wc -l to the variable ls_output
A

!/bin/bash

function assignMe () {
ls /usr | wc -l
}
ls_output=$(assignMe)
echo $ls_output

33
Q

!/bin/bash

  1. Fix the following script: The script should prompt the user to choose a mentor name,
    the script then prints the mentor’s email.
    If the user chooses ‘Exit’ then the program exits.

declare -A mentors

mentors=([“Naftali”] ????? [“Exit”])
mentors_emails=(“naftali@infinitylabs.co.il” “tal@infinitylabs.co.il” “ilya@infinitylabs.co.il”)

PS3=”Please enter your choice: “

select name in ?????
do
echo ${??????}
done

A

!/bin/bash

declare -A mentors=( [Naftali]=”naftali@infinitylabs.co.il” [Tal]=”tal@infinitylabs.co.il” [Ilya]=”ilya@infinitylabs.co.il” [Exit]=”Exit” )
PS3=”Please enter your choice: “

select name in ${!mentors[@]}
do
if [ $name == Exit ]; then
exit 0
fi

echo ${mentors[$name]} done