#!/bin/sh # Yes, you can execute this whole script for demonstration. You can # also give it an argument - which defaults to 15. # Of course you also remember comments, the here document, and the : # command, right? :-) Okay, so I effectively (but not literally) # comment out the part between the pairs of __EOT__ without having to # individually comment out most or all the lines in the section. >>/dev/null 2>&1 : << \__EOT__ # if syntax: if list; then list; [ elif list; then list ] ... [ else list; ] fi # here the square bracket ([]) characters aren't meant literally, # but rather indicate the enclosed portion is optional # the ... indicates the preceding (optional) portions can be # repeated zero or more times # we need whitespace (one or more spaces, tabs, or newlines) # between if and list, newline or ; between list and then, newline # or ; after subsequent lists, and whitespace between each elif or # else and the list that immediately follows it. __EOT__ # if $1 is not set, we set it to 15 # "${1-15}" gives us "$1" if $1 is set, "15" otherwise # we then use set to set that as our first (and now only) positional parameter set -- "${1-15}" # as side effect, we unset an other positional parameters # you can execute this script with arguments, to change $1 from the default # value of 15 # Typical fairly readable use in scripts: # (the test logic is reversed to more defensively handle relatively arbitrary # argument passed to us) if [ 15 -eq "$1" ]; then echo "equals 15" elif [ 15 -lt "$1" ]; then echo "greater than 15" elif [ 15 -gt "$1" ]; then echo "less than 15" else 1>&2 echo "I'm confused, you gave me: $1" fi # somewhat less readable version with minimum whitespace and no semicolons: if [ 15 -eq "$1" ] then echo "equals 15" elif [ 15 -lt "$1" ] then echo "greater than 15" elif [ 15 -gt "$1" ] then echo "less than 15" else 1>&2 echo "I'm confused, you gave me: $1" fi # without newlines: if [ 15 -eq "$1" ]; then echo "equals 15"; elif [ 15 -lt "$1" ]; then echo "greater than 15"; elif [ 15 -gt "$1" ]; then echo "less than 15"; else 1>&2 echo "I'm confused, you gave me: $1"; fi # somewhat less readable version with minimum whitespace and no newlines # within the command: if [ 15 -eq "$1" ];then echo "equals 15";elif [ 15 -lt "$1" ];then echo "greater than 15";elif [ 15 -gt "$1" ];then echo "less than 15";else 1>&2 echo "I'm confused, you gave me: $1";fi