2017-06-03 21:43:46 +00:00
|
|
|
# To implement a for loop:
|
2019-12-05 01:31:57 +00:00
|
|
|
for WORD in LIST
|
|
|
|
do
|
|
|
|
COMMANDS
|
|
|
|
done
|
|
|
|
# For example:
|
|
|
|
for CurDay in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
|
|
|
|
do
|
2020-03-16 19:51:55 +00:00
|
|
|
printf "%s\n" "$CurDay"
|
2017-06-03 21:43:46 +00:00
|
|
|
done
|
|
|
|
|
2019-12-05 01:31:57 +00:00
|
|
|
# To implement a case statement:
|
|
|
|
case $1 in
|
|
|
|
0)
|
2020-03-16 19:51:55 +00:00
|
|
|
echo "Found a '0'." ;;
|
2019-12-05 01:31:57 +00:00
|
|
|
1)
|
2020-03-16 19:51:55 +00:00
|
|
|
echo "Found a '1'." ;;
|
2019-12-05 01:31:57 +00:00
|
|
|
2)
|
2020-03-16 19:51:55 +00:00
|
|
|
echo "Found a '2'." ;;
|
2019-12-05 01:31:57 +00:00
|
|
|
3*)
|
2020-03-16 19:51:55 +00:00
|
|
|
echo "Something beginning with '3' found." ;;
|
|
|
|
'')
|
|
|
|
echo "Nothing (null) found." ;;
|
|
|
|
*)
|
|
|
|
echo "Anything else found." ;;
|
2017-06-03 21:43:46 +00:00
|
|
|
esac
|
|
|
|
|
2019-12-05 01:31:57 +00:00
|
|
|
# Turn on built-in Bash debugging output:
|
2017-06-03 21:43:46 +00:00
|
|
|
set -x
|
2019-12-05 01:31:57 +00:00
|
|
|
# Turn the above off again:
|
2017-06-03 21:43:46 +00:00
|
|
|
set +x
|
|
|
|
|
|
|
|
# Retrieve N-th piped command exit status
|
2019-11-26 23:34:52 +00:00
|
|
|
printf 'foo' | grep -F 'foo' | sed 's/foo/bar/'
|
2017-06-03 21:43:46 +00:00
|
|
|
echo ${PIPESTATUS[0]} # replace 0 with N
|
|
|
|
|
|
|
|
# Lock file:
|
|
|
|
( set -o noclobber; echo > my.lock ) || echo 'Failed to create lock file'
|
|
|
|
|
2019-12-05 01:31:57 +00:00
|
|
|
# Fork bomb. Do not run this! Has the potential to wreak havoc. It repeatedly
|
2020-10-27 00:10:11 +00:00
|
|
|
# and quickly spawns a lot of processes until the system eventually locks up.
|
2017-06-03 21:43:46 +00:00
|
|
|
:(){ :|:& };:
|
2019-12-05 01:31:57 +00:00
|
|
|
# An alternative, easier-to-understand version without the obfuscation:
|
|
|
|
func(){ func | func & }; func
|
2017-06-03 21:43:46 +00:00
|
|
|
|
2019-12-05 01:31:57 +00:00
|
|
|
# Unix Roulette, courtesy of Bigown's answer in the joke thread.
|
|
|
|
#
|
|
|
|
# DANGER! Don't execute!
|
|
|
|
#
|
2020-10-27 00:10:11 +00:00
|
|
|
# Luckily, most modern setups have `--preserve-root` on by default, so this
|
|
|
|
# will be null and void, but even so, not even remotely worth the risk!
|
2017-06-03 21:43:46 +00:00
|
|
|
[ $[ $RANDOM % 6 ] == 0 ] && rm -rf /* || echo Click #Roulette
|
2017-12-03 01:53:55 +00:00
|
|
|
|
2019-12-05 01:31:57 +00:00
|
|
|
# A for loop one-liner.
|
|
|
|
for CurIter in {1..4}; do echo "$CurIter"; done
|
|
|
|
# Alternative, slightly-cleaner syntax:
|
|
|
|
for CurIter in {1..4}; { echo "$CurIter"; }
|
2017-12-03 01:53:55 +00:00
|
|
|
|
2019-12-05 01:31:57 +00:00
|
|
|
# Test for a variable being equal to (`-eq`) an integer (`0`).
|
|
|
|
if [ $var -eq 0 ]; then
|
|
|
|
printf "Variable '\$var' is equal to '0'.\n"
|
2019-06-24 17:41:48 +00:00
|
|
|
fi
|
|
|
|
|
2019-12-05 01:31:57 +00:00
|
|
|
# Test for a `PATH` executable existing as a file, but note that aliases and
|
|
|
|
# functions will also output and result in a `0` exit status.
|
2017-12-03 13:02:43 +00:00
|
|
|
command -v ${program} >/dev/null 2>&1 || error "${program} not installed"
|
2020-10-27 00:10:11 +00:00
|
|
|
# However, that is a solution commonly found in a script using the Bourne
|
|
|
|
# shell, so in this case, an alternative, Bash-like, and more accurate version
|
|
|
|
# could be:
|
2019-12-05 01:31:57 +00:00
|
|
|
if ! type -fP bash > /dev/null 2>&1; then
|
2020-03-16 19:51:55 +00:00
|
|
|
printf "ERROR: Dependency 'bash' not met." >&2
|
|
|
|
exit 1
|
2019-12-05 01:31:57 +00:00
|
|
|
fi
|
2017-12-03 01:53:55 +00:00
|
|
|
|
2020-10-27 00:10:11 +00:00
|
|
|
# Send both STDOUT and STDERR from COMMAND to FILE.
|
2019-12-05 01:31:57 +00:00
|
|
|
COMMAND > FILE 2>&1
|
|
|
|
# Send STDOUT and STDERR from COMMAND to `/dev/null`, where data goes to die.
|
|
|
|
COMMAND > /dev/null 2>&1
|
|
|
|
# Pipe the STDOUT and STDERR from COMMAND_1 to COMMAND_2.
|
|
|
|
COMMAND_1 |& COMMAND_2
|
2018-08-28 14:28:57 +00:00
|
|
|
|
2020-10-27 00:10:11 +00:00
|
|
|
# Verbosely convert whitespaces (` `) to underscores (`_`) in file names.
|
2018-08-28 14:28:57 +00:00
|
|
|
for name in *\ *; do mv -vn "$name" "${name// /_}"; done
|