W1: Linux - Bash Language Flashcards
- Keywords: bash
GNU Bourne-Again SHell
sh-compatible language interpreter, executes commands read from an input .sh filew
- Keywords: Global Variables
Global variables can be used by all Bash scripts on your system
- Keywords: Local Variables
Local Variables can only be used within the script (or shell) in whech they’re defined
- Keywords: printenv
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.
- Keywords: Index/associative arrays
array: stores multiple data.
with index and the value of each array element accessed by its corresponding index value.
- Keywords: for, while, until loops
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
- Keywords: break,
break 1
terminates current loop, passes program control to the command that follows the terminated loop.
- Keywords: continue
continue 1
skips the remaining commands in the current loop iteration, passes program control to the next loop iteration.
- Keywords: select
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
- Keywords: PS3
PS1: primary prompt string
PS2: secondary prompt string
PS3: this parameter’s value is used as prompt for the select command
PS4:
- Keywords: unset
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
- Keywords: if statement
!/bin/bash
# Basic if statement if [ 1000 > 100 ] then echo Hey that\'s a large number. pwd fi date
- Keywords: case statement
!/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
- Keywords: function
!/bin/bash
BashFunction () {
echo Hello, my linux friends
}
BashFunction
- Keywords: /dev/stdin
standard input stream file. what’s input there is what’s input to the console,
what’s input to stdout is console output
- What is the meaning of #!/bin/bash
“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.
- How do you declare local/global vars?
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.
- How do you make multiline comments?
: ‘
this will not get executed
‘
- How do you access associative array fields?
!/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
- What does the following command do?
echo $(elements[-1]}
supposedly prints the last element in the array
- How do you iterate through an array’s elements in a for loop?
arr=( “apple” “banana” “cherry” )
for item in “${arr[@]}”
do
echo $item
done
- 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[*]}
!/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
- What is the meaning of the following switches within the square bracket of an ‘if’ statement? -d -w -x -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
- How do you check if a variable is set?
-v variable
-z${variable}
- How would you return a result of a function?
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.
- Is it possible to write a function with the name cp?
apparently yes
- How would you read a password variable from STDIN, keeping it hidden?
!/bin/bash
secretInput () {
echo “Please enter your password”;
read -s password;
#pass = $(</dev/stdin);
echo break; echo $password; }
secretInput
- How many times will the following loop run?
counter=1
while [ $counter -le 10 ]
do
. . .
done
ad infinitum
- How many times will the following loop run?
counter=1
until [ $counter -le 10 ]
do
. . .
done
it will not.
as long as the test case is true, it won’t start
- When would you choose “while loop” and “until loop”?
since until won’t run until it is false, you use until
Exercise 1.
Write a function in a bash script file, that returns the number of lines in that file.
!/bin/bash
function countMe () {
wc -l < countMe.sh
}
lineCount=$(countMe)
echo $lineCount
#beep boop bop
#line 9
- Please assign the value of the command ls /usr | wc -l to the variable ls_output
!/bin/bash
function assignMe () {
ls /usr | wc -l
}
ls_output=$(assignMe)
echo $ls_output
!/bin/bash
- 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
!/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