An explained introduction of bash scripting is on:
In order to execute/run with shell enterpreter, the first line in the script should be:
#!/bin/bash
This line is called Shebang.
If you want to comment a line, you could use '#' sign, and for a block:
: << COMMENT
comment line 1
comment line 2
comment line 3
...
comment line n
COMMENT
This command allows us to create custom bash commands, where to use it in permanent way, we should write it on the shell profile file.
Example:
alias conn="echo 'Total connections on port 80 and 443:' ; netstat -plant | grep '80\|443' | grep -v LISTEN | wc -l"
Where the output is:
Total connections on port 80 and 443: 12
It is important you can not have spaces after and before the '=' sign.
name="Anna"
So, if you want to reference it, you should write:
$name
or ${name}
For user input we use read
command where you define the name of the variable where the input will be stored.
Example:
echo "What is your name?"
read name
echo "Hi there $name"
Another way to get the input without using echo
command is using the -p
read
's parameter:
read -p "What is your name? " name
echo "Hi there $name"
To initialize an array by assigning values, they should be separated by space and enclosed in '()'.
my_array=("value 1" "value 2" "value 3" "value 4")
Then, if you want to acces to a value, you should set the index (0-n).
Example:
echo ${my_array[2]}
Should print value 3
.
Example 2:
echo ${my_array[@]}
Should print all values.
Note: For arrays you should use curly brackets ({}).
Functions in programming, allow us to reuse lines of code and/or segment our application.
To create a function in bash, we use this structure:
function function_name() {
your_commands
}
We also can omit function
key word.
To send parameters, we just reference it.
Example:
server_name=$(hostname)
function memory_check() {
echo ""
echo "Memory usage on ${server_name} is: "
free -h
echo ""
}
if [[ condition ]] ; then
commands
else
commands
fi
case $some_variable in
pattern_1)
commands
;;
pattern_2 | pattern_3)
commands
;;
*)
default commands
;;
esac
We can use continue
and break
commands, which permits stop the loop or start the next iteration.
continue
: stops the current iteration and start the next one.break
: ends the current loop. Example 1:
for i in {1..5}
do
if [[ $i == 2 ]]
then
echo "skipping number 2"
continue
fi
echo "i is equal to $i"
done
The output will be:
i is equal to 1
skipping number 2
i is equal to 3
i is equal to 4
i is equal to 5
Example 2:
num=1
while [[ $num –lt 10 ]]
do
if [[ $num –eq 5 ]]
then
break
fi
((num++))
done
echo "Loop completed"
The output will be:
for var in ${list}
do
your_c ommands
done
Example 1:
users="devdojo bobby tony"
for user in ${users}
do
echo "${user}"
done
Example 2:
for num in {1..10}
do
echo ${num}
done
while [[ your_condition ]]
do
your_commands
done
until [[ your_condition ]]
do
your_commands
done