| Home | Notes | Github |
|---|
Written: 05-Mar-2022
Here’s someone else’s cheatsheet
#!/bin/bashfor LoopTo loop over files in a directory:
for f in $FILES
do
# Do something here
echo "Reading filename: $f"
doneTo loop over the lines of a file use (useful if there are spaces in a list of filenames):
while IFS= read -r line
do
# Doing something on each line
echo "Text read from file: $line"
done < my_filename.txtif Statementif [ expression ]
then
Statement(s) for true
else
Statement(s) for false
fiCheck two numbers are equal
[ "$a" -eq "$b" ]Check two strings are equal
[ "$str1" = "$str2" ]IFS=$'\n'Or liberally apply quotes.
$# = Number of Arguments$0 = 0th Argument a.k.a. the function name.$1 = 1st Argument### User Input
read -r -p "Are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
do_something
;;
*)
do_something_else
;;
esac[ -d "/path/to/dir" ] && echo "Directory /path/to/dir exists."
## OR ##
[ ! -d "/path/to/dir" ] && echo "Directory /path/to/dir DOES NOT exists."For files use -f instead!