bash scripting Flashcards
Line at beginning of shell script file
!/bin/bash
print statement
echo “hello”
get user input (name)
read PERSON
echo “Hello, $PERSON”
command line arguments
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.
variables
name = “ricky”
echo “Hello, $name”
Initialize array with values
NAME[0]=”ricky”
NAME[1]=”matt”
NAME[2]=”joe”
access array values
echo “First Index: ${NAME[0]}”
access all the items in an array
${array_name[*]}
How to perform arithmetic ops
val=expr 2 + 2
echo “Total value : $val”
if statement
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
while loops
a=0
while [ “$a” -lt 10 ] # this is loop1
do
b=”$a”
done
for loop
NUMS=”1 2 3 4 5 6 7”
for NUM in $NUMS
do
# this stuff
done
shell substitutions
what does command substitution do?
used to assign the output of a command to a variable
command sub example
USERS=who | wc -l
echo “Logged in user are $USERS”