mirror of
https://github.com/chubin/cheat.sheets
synced 2024-11-03 15:40:17 +00:00
58 lines
1.3 KiB
Plaintext
58 lines
1.3 KiB
Plaintext
# To implement a for loop:
|
|
for file in *;
|
|
do
|
|
echo $file found;
|
|
done
|
|
|
|
# To implement a case command:
|
|
case "$1"
|
|
in
|
|
0) echo "zero found";;
|
|
1) echo "one found";;
|
|
2) echo "two found";;
|
|
3*) echo "something beginning with 3 found";;
|
|
esac
|
|
|
|
# Turn on debugging:
|
|
set -x
|
|
|
|
# Turn off debugging:
|
|
set +x
|
|
|
|
# Retrieve N-th piped command exit status
|
|
printf 'foo' | fgrep 'foo' | sed 's/foo/bar/'
|
|
echo ${PIPESTATUS[0]} # replace 0 with N
|
|
|
|
# Lock file:
|
|
( set -o noclobber; echo > my.lock ) || echo 'Failed to create lock file'
|
|
|
|
# Fork bomb
|
|
:(){ :|:& };:
|
|
|
|
# Unix Roulette
|
|
# (Courtesy of Bigown's answer in the joke thread)
|
|
# DANGER! Don't execute!
|
|
[ $[ $RANDOM % 6 ] == 0 ] && rm -rf /* || echo Click #Roulette
|
|
|
|
# for loop in one line
|
|
for i in $(seq 1 4); do echo $i; done
|
|
|
|
# Check to see if a variable equals a specified integer
|
|
if [ "$var" -eq "0" ]; then
|
|
echo "var equals zero";
|
|
fi
|
|
|
|
# Test if a program exists in the path
|
|
# There are false positives: aliases and functions
|
|
command -v ${program} >/dev/null 2>&1 || error "${program} not installed"
|
|
|
|
# Redirection
|
|
# Please note that 2>&1 goes after
|
|
my_command > command-stdout-stderr.txt 2>&1
|
|
my_command > /dev/null 2>&1
|
|
# Redirect stdout and stderr of cmd1 to cmd2
|
|
cmd1 |& cmd2
|
|
|
|
# Convert spaces to underscores in filenames
|
|
for name in *\ *; do mv -vn "$name" "${name// /_}"; done
|