Created
August 16, 2019 09:34
-
-
Save janstuemmel/5bb3e15c8b5c0ad2bf39ff715daefa42 to your computer and use it in GitHub Desktop.
Demo shell script for blog post: https://janstuemmel.de/2019/08/15/shell-scripting.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# USAGE: execute with e.g.: BAZ=hello ./script.sh 5 1 | |
# | |
## Variables | |
# | |
FOO=foo | |
${BAR:-bar} # default value "bar" for BAR, can be set from outside the script | |
echo $FOO $BAR # prints: foo bar | |
echo ${FOO}baz # prints foobaz | |
echo ${FOO} # prints string length of "foo" | |
${BAZ:?variable BAZ missing} # exits script if variable BAZ is missing | |
echo $BAZ | |
# | |
## Parameters | |
# | |
echo Scriptname: $0 | |
echo Number of args: $# | |
echo All args: $@ | |
echo All args expanded: "$@" | |
echo First arg: $1 | |
echo Second arg: $2 | |
echo Process number: $$ | |
echo Last command exit code: $? | |
sh -stfu 2> /dev/null # silent stderr | |
echo Last command exit code: $? | |
# | |
## Conditionals | |
# | |
if [ $1 -le 2 ] | |
then echo "$1 <= 2" | |
elif [ $1 -le 4 ] | |
then echo "$1 <= 4" | |
else | |
echo "i can't count to $1" | |
fi | |
# numbers | |
if [ 1 -eq "1" ]; then echo "1 == 1"; fi # equal | |
if [ 1 -ne 2 ]; then echo "1 != 2"; fi # not equal | |
if [ 2 -gt 1 ]; then echo "2 > 1"; fi # greater then | |
if [ 2 -ge 2 ]; then echo "2 >= 2"; fi # greater equal | |
if [ 1 -lt 2 ]; then echo "1 < 2"; fi # lower then | |
if [ 1 -le 1 ]; then echo "1 <= 2"; fi # lower equal | |
# strings | |
if [ "foo" = "foo" ]; then echo '"foo" == "foo"'; fi | |
if [ "foo" != "bar" ]; then echo '"foo" != "bar"'; fi | |
if [ -z "" ]; then echo '"" is empty'; fi | |
if [ -n "foo" ]; then echo '"foo" is not empty'; fi | |
# always use "$VAR" in quotes if you checking string variables | |
if [ "$1" = "5" ]; then echo "$1 == 5"; else echo "$1 != 5"; fi | |
# check folders and files | |
if [ -f "$0" ]; then echo "me exists"; fi | |
mkdir foo | |
if [ -d "./foo" ]; then echo "folder exists"; fi | |
rm -r foo | |
if [ ! -d "./foo" ]; then echo "folder removed"; fi | |
# | |
## Loops | |
# | |
# while loop with break condition | |
i=0 | |
while [ $i -le 10 ] | |
do | |
echo $i | |
i=$(( $i + 1 )) | |
done | |
# infinite loop | |
while [ : ] | |
do | |
echo 1 | |
break # we don't want you to run infinite :) | |
done | |
# for loop with list [one, two, three] | |
for i in one two three | |
do | |
echo $i | |
done | |
# for loop with list from command (all files/dirs in $PWD that end with .sh) | |
for i in $(ls *.sh) | |
do | |
echo $i | |
done | |
# loop through all file names in $PWD that end with .sh | |
for i in *.sh | |
do | |
echo $i | |
done | |
# loop through all command line arguments | |
for i in $@ | |
do | |
echo $i | |
done | |
# same as above, but shorter | |
for i | |
do | |
echo $i | |
done | |
# | |
## Functions | |
# | |
hello() { | |
echo "hello $@" | |
} | |
hello world ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment