Syntax: Bash

Bash is context-sensitive - a script could mean different things depending on what is defined in the environment. Thus, to present Bash code clearly, you will need to use inline markup, such as the last example on this page.

#!/bin/bash
# Counting to 11 in 10 different ways.

n=1; echo -n "$n "

let "n = $n + 1"   # let "n = n + 1"  also works.
echo -n "$n "


: $((n = $n + 1))
#  ":" necessary because otherwise Bash attempts
#+ to interpret "$((n = $n + 1))" as a command.
echo -n "$n "

(( n = n + 1 ))
#  A simpler alternative to the method above.
#  Thanks, David Lombard, for pointing this out.
echo -n "$n "

n=$(($n + 1))
echo -n "$n "

: $[ n = $n + 1 ]
#  ":" necessary because otherwise Bash attempts
#+ to interpret "$[ n = $n + 1 ]" as a command.
#  Works even if "n" was initialized as a string.
echo -n "$n "

n=$[ $n + 1 ]
#  Works even if "n" was initialized as a string.
#* Avoid this type of construct, since it is obsolete and nonportable.
#  Thanks, Stephane Chazelas.
echo -n "$n "

# Now for C-style increment operators.
# Thanks, Frank Wang, for pointing this out.

let "n++"          # let "++n"  also works.
echo -n "$n "

(( n++ ))          # (( ++n )  also works.
echo -n "$n "

: $(( n++ ))       # : $(( ++n )) also works.
echo -n "$n "

: $[ n++ ]         # : $[ ++n ]] also works
echo -n "$n "

echo

exit 0

#!/bin/bash

svc_cluster_ip=10.0.0.10
svc_priv_dsa=~/.ssh/id_dsa

if [ ! -f $svc_priv_dsa ] ; then
	echo " ${0##*/} is missing the SSH DSA private key"
	echo "                         needed to access the SAN Volume controller."
	echo " Please specify the correct path!"
fi

if [ -z $1 ] ; then
	echo
	echo " ${0##*/} [ WWPN ]"
	echo
	echo "  WWPN    - WWPN, or part of WWPN that is being searched."
	echo "            Should be entered in continous format (ie. no dash (-) or colon (:))!"
	echo
	exit 1;
fi

WWPN=$1

list_wwpn_by_host() {
	local _host=$1

	if [ ! -z $_host ] ; then
		ssh -i $svc_priv_dsa admin@$svc_cluster_ip svcinfo lshost \
		-delim : $_host | grep ^WWPN | sed "s,WWPN:,,"
	fi
}

#set -x

HOST_LIST="$( ssh -i $svc_priv_dsa admin@$svc_cluster_ip svcinfo lshost -delim : -nohdr | cut -d: -f2 )"

for HOST in $HOST_LIST; do
	echo -n "$HOST: "
	echo `list_wwpn_by_host $HOST`
done | grep $WWPN

#set +x

This next example uses inline HTML to markup and highlight rd-resolve-test bob.

samuel@ayako:~$ which git
/usr/bin/git
samuel@ayako:~$ ls -lah
getconf     getent      getopt      getopts     gettext     gettext.sh  gettextize  
samuel@ayako:~# sudo rd-resolve-test bob
/home/samuel

Here is the HTML of the above <pre>: