You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BASH shell scripts are essentially a series of bash commands that are stored in a file that can be executed by running the script.
hello-world.sh
#!/bin/bash# This is a commentecho"Hello world"
#!/bin/bash - the first line says the script should be run using bash
#! = shebang
# = pound/hash - used for comments
There are a few ways to execute the script:
bash hello-world.sh
Hello world
# bash is the executable program# To start the script without "bash" command, the script must be executable
chmod +x hello-world.sh
./hello-world.sh
Hello world
executing-commands-example.sh
#!/bin/bash# ************************************# Executing commands example# Execute whoami command
user=$(whoami)# Execute hostname command
hostname=$(hostname)# Execute print working directory (pwd) command
directory=$(pwd)# Display informationecho"User=[$user] Host=[$hostname] Working dir=[$directory]"# Display contents of directoryecho"Contents:"
ls
Conditionals allow the script to take different actions depending on some sort of state, referred to as if-then rules.
if-examples.sh
if statements.
The expression inside the brackets [[ ... ]] is evaluated and used to determine if the conditional code is executed.
If the expression evaluates to TRUE, the block (the code inside the IF statement) is executed.
If the expression evaluates to FALSE, BASH skips over all of the conditional code and starts execute after the "fi" keyword.
else statements
Code inside the else block is executed if the test returns FALSE.
elif statements (else-if)
If the first test in the if statement fails, the elif statement will be evaluated.
elif statements will be tested from top down.
the code block associated with the first TRUE evaluated test will be executed and the rest of the conditional will be skipped.
#!/bin/bash# ************************************# If examples# Test to see if /etc is a directoryif [[ -d /etc/ ]];thenecho /etc/ is indeed a directory
fi# Check to see if a file existsif [[ -e sample.txt ]];thenecho The file sample.txt exists
elseecho The file sample.txt does NOT exist
fi# Check a variable's value
TEST_VAR="test"if [[ $TEST_VAR=="test" ]];thenecho TEST_VAR has a value of "test"elif [[ $TEST_VAR=="again" ]];thenecho TEST_VAR has a value of "again"elseecho TEST_VAR has an unknown value
fi
bash if-examples.sh
/etc/ is indeed a directory
The file sample.txt does NOT exist
TEST_VAR has a value of test
comparison-examples.sh
Examples of performing numerical and string comparisons.
#!/bin/bash# ************************************# Numerical comparison examples# Create some variables
x=1
echo x=["$x"]
y=2
echo y=["$y"]
z=2
echo z=["$z"]
# Perform some comparisons# Numeric: Not equalsif [[ "$x"-ne"$y" ]];thenecho ["$x"] ne ["$y"]
fi# Numeric: Equalsif [[ "$y"-eq"$z" ]];thenecho ["$y"] eq ["$z"]
fi# Numeric: Greater thanif [[ "$y"-gt"$x" ]];thenecho ["$y"] gt ["$x"]
fi# Numeric: Greater than or equal toif [[ "$y"-ge"$z" ]];thenecho ["$y"] ge ["$z"]
fi# Numeric: Less thanif [[ "$x"-lt"$y" ]];thenecho ["$x"] lt ["$y"]
fi# Numeric: Less than or equal toif [[ "$y"-le"$z" ]];thenecho ["$y"] le ["$z"]
fi# ************************************# String comparison examples# Create some variables
a="A"echo a=["$a"]
b="B"echo b=["$b"]
anotherA="A"echo anotherA=["$anotherA"]
# Perform some comparisons# String: Equalsif [[ "$a"=="$anotherA" ]];thenecho ["$a"] "==" ["$anotherA"]
fi# String: Not equalsif [[ "$a"!="$b" ]];thenecho ["$a"] "!=" ["$b"]
fi# String: Less thanif [[ "$a"<"$b" ]];thenecho ["$a"] "<" ["$b"]
fi# String: Greater thanif [[ "$b">"$a" ]];thenecho ["$b"] ">" ["$a"]
fi
A case statement is essentially an if statement with multiple elif statements.
it uses a command line argument - $1
#!/bin/bash# ************************************# Case example# Switch off of the first command line argumentcase$1in
[1-3])
message="Argument is between 1 and 3 inclusive"
;;
[4-6])
message="Argument is between 4 and 6 inclusive"
;;
[7-9])
message="Argument is between 7 and 9 inclusive"
;;
1[0-9])
message="Argument is between 10 and 19 inclusive"
;;
*)
message="I don't understand the argument or it is missing"
;;
esac# Print out a message describing the resultecho$message
bash case-example.sh 6
Argument is between 4 and 6 inclusive
bash case-example.sh 22
I don't understand the argument or it is missing
Loops
Loops allow to write code once and execute it multiple times.
Definite loops
know the number of loops (times it is executed) before the loop ever starts.
most common: for loop
Indefinite loops
unknown number of loops until the end (it depends on the user input for example).
most common: while loop
Infinite loops
loops that never end (whether by design or accident), != indefinite loops
loop-examples.sh
#!/bin/bash# ************************************# For loop examplesecho -----------------------------------
echo For loops
# Iterate through the numbers 1 through 5 and print them outecho Print out a hard-coded sequence
foriin 1 2 3 4 5;doecho Index=[$i]
done# Same as above, but generate the sequenceecho Print out a generated sequence
foriin {1..5};doecho Index=[$i]
done# Same as above, but use a more conventional format# NOTE: Double parenthesis are used since we are doing arithmeticecho Print out a generated sequence using the 3-expression format
for(( i=1; i<=5; i++))doecho Index=[$i]
done# Print out the last line of each shell script in the current directoryecho Print out the last line of each shell script
forFILEin*.sh
doecho =====================================
echo File=[$FILE]
tail -n 1 $FILEdoneecho''# ************************************# While loop exampleecho -----------------------------------
echo While loop
# Countdown to blastoffecho Executing a while loop to countdown to blastoff
counter=5
while [[ $counter-gt 0 ]];doecho Countdown [$counter]
counter=$(($counter-1))doneecho Blastoff
bash loop-examples.sh
-----------------------------------
For loops
Print out a hard-coded sequence
Index=[1]
Index=[2]
Index=[3]
Index=[4]
Index=[5]
Print out a generated sequence
Index=[1]
Index=[2]
Index=[3]
Index=[4]
Index=[5]
Print out a generated sequence using the 3-expression format
Index=[1]
Index=[2]
Index=[3]
Index=[4]
Index=[5]
Print out the last line of each shell script
=====================================
File=[case-example.sh]
echo$message
=====================================
File=[command-line-arguments-example.sh]
fi
=====================================
File=[comparison-examples.sh]
fi
=====================================
File=[executing-commands-example.sh]
ls
=====================================
File=[generate-passord.sh]
echo$password
=====================================
File=[generate-password.sh]
=====================================
File=[hello-world.sh]
echo"Hello world"
=====================================
File=[if-examples.sh]
fi
=====================================
File=[loop-examples.sh]
echo Blastoff
=====================================
File=[process-information.sh]
echo Child process exit status $?
-----------------------------------
While loop
Executing a while loop to countdown to blastoff
Countdown [5]
Countdown [4]
Countdown [3]
Countdown [2]
Countdown [1]
Blastoff
Examples
command-line-arguments-example.sh
Variables usage - $#, $*
#!/bin/bash# ******************************************************************************# Processing command line arguments# What is the name of the executed script?echo Name of script [$0]
# How many were provided?echo Command line argument count [$#]
# Iterate through each argumentforargin$@;doecho Argument [$arg]
done# Display all the arguments as a stringecho All arguments [$*]
# Use parenthesis for arguments with numbers 10 or largerif [ "${12}"!="" ];thenecho Argument 12 is [${12}]
echo Argument 12 is NOT [$12]
fi
bash command-line-arguments-example.sh one Two 3 4th 999
Name of script [command-line-arguments-example.sh]
Command line argument count [5]
Argument [one]
Argument [Two]
Argument [3]
Argument [4th]
Argument [999]
All arguments [one Two 3 4th 999]
generate-password.sh
This script generates a password by combining a specified number of words.
Each word is capitalized and separated by a separator character provided as a command line argument.
#!/bin/bash# Grab the command line arguments
passwd_word_count=$1
separator=$2# Start with a blank password
password=''# Get the total number of words in the word list
total_word_count=`wc -l wordlist.txt | awk '{print $1;}'`# Build the password using the specified number of wordsfor(( i=1; i<=$passwd_word_count; i++))do# Generate a random number using OpenSSL to be cryptographically secure
rand_num_hex=`openssl rand -hex 4`
rand_num_dec=$((16#$rand_num_hex))# Use the random number as an index into the word list
word_index=$(($rand_num_dec%$total_word_count))
random_word=`awk -v idx="$word_index"'{if (NR==idx) print $1}' wordlist.txt`# Capitalize the word
random_word_upper=`echo ${random_word^}`# Insert a separator if this isn't the first word in the passwordif [[ ${#password}-gt 0 ]];then
password=$password$separator$random_word_upperelse
password=$random_word_upperfidone# Display the passwordecho$password