Shell Scripts
Create shell scripts with the .sh
file extension.
Bash will run this script in a subprocess.
For example if you run a change directory command in a script, it will not change the directory in the terminal you are running the script from.
Hashbang #!
This says interpret this file with the following program... bash
#! /bin/bash
This also allows it to run without a file extension on it.
Use which
to find what interpreter is being used for a file. (e.g. which node
)
Then put this path at the top with the hashbang, then you can write a script with that interpreter.
Path
You can add to your path by appending to it in bachrc
like so to the bottom of the file.
export PATH=~/<directory or file to add>:$PATH
Then run source ~/.bashrc
to update the path.
Variables
You can make variables in shell scripts and it's convention to use all caps for them.
DESTINATION=~/bin
FILE_PREFIX=file
cd ${DESTINATION}
touch ${FILE_PREFIX}{1..10}.txt
Arguments
You can pass arguments to a shell script.
$0
is the name of the script. e.g.bash or zsh
$1
is the first argument.
$2
is the second argument.
$#
is the number of arguments.
$@
is all the arguments.
$*
is all the arguments as a single string.
Inputs
You can use read
to get input from the user.
read -p "enter a file prefix: " FILE_PREFIX
Conditionals
if
You can use if
then
else
fi
to create conditionals.
if [-z $DESTINATION ]; then
echo "no path provided, defaulting to ~/temp"
DESTINATION=temp
fi
Flags in an if statement are syntactic sugar, they are essentially using the built in test command.
Common if statement flags:
-z
checks if the variable is empty.-n
checks if the variable is not empty.-e
checks if the file exists.-d
checks if the directory exists.-f
checks if the file exists and is a regular file.-x
checks if the file exists and is executable.-eq
checks if two numbers are equal.-le
checks if the first number is less than or equal to the second number.-ge
checks if the first number is greater than or equal to the second number.-w
checks if the file exists and is writable.
More complex conditionals
if [ $1 -gt 10 ]; then
echo "greater than 10"
elif [ $1 -lt 10 ]; then
echo "less than 10"
else
echo "equal to 10"
fi
Switch cases:
case $1 in
"smile")
echo ":-)"
;;
"sad")
echo ":-("
;;
"laugh")
echo ":-D"
;;
*)
echo ":-|"
;;
esac
Loops and Arrays
Arrays
ARRAY=(one two three)
echo ${ARRAY[0]}
echo ${ARRAY[1]}
echo ${ARRAY[2]}
echo ${ARRAY[*]}
While
i=0
while [ $i -lt 10 ]; do
echo $i
i=$((i+1))
done
Until
i=0
until [ $i -ge 10 ]; do
echo $i
i=$((i+1))
done
For
for i in {1..10}; do
echo $i
done
While Loop Random Number example:
#!/bin/bash
NUM_TO_GUESS=$(( $RANDOM % 10 + 1 ))
GUESSED_NUM=0
echo "guess a number between 1 and 10"
while [ $NUM_TO_GUESS -ne $GUESSED_NUM ]
do
read -p "your guess: " GUESSED_NUM
done
echo "you got it!"
NOTE: In the above program we don't want a dollar sign for GUESSED_NUM in the loop because we don't want to evaluate it as a variable (0).
We want to compare it to the number to guess.
Random Takeaways
Random numbers in bash: Below generates a random number between 1 and 10.
NUMBER=$(( $RANDOM % 10 + 1 ))