首页 > 代码库 > Bash Excercises

Bash Excercises

1. cat <<EOF

#!/bin/bash
function printHelp {
cat<<EOF

Run the Dash vector tests.
Usage:
./run_dash_vector_test.sh <vector_env> <dash_version>

The vector environment should be the value 1 or 2, to indicate which of the
static Dash vector environments should be used.

The Dash version should be a valid git revision, e.g. ‘master‘ or
‘2.3352-HOTFIX‘.

EOF
}

if [[ "$#" -ne 2 ]]
then
printHelp
exit -1
fi

2. let i++
reference:
[1] http://askubuntu.com/questions/385528/how-to-increment-a-variable-in-bash

#!/bin/bash
let i=1
for SCRIPTLET in "${SCRIPTLETS[@]}"
do
echo " ${i}) ${SCRIPTLET}"
let i++
done

The same as:

#!/bin/bash
i=0
while [ "$i" -lt 10 ]
do
<command block>
i=`expr $i + 1`
done

or even

#!/bin/bash
i=0
while [ "$i" -lt 10 ]
do
<command block>
(( i = i + 1 )) # or (( i+=1 ))
done

 3. getopts

#!/bin/bash
while getopts ":d:e:g:v:" opt
do
  case $opt in
    d) DASH_REVISION="$OPTARG" ;;
    e) EMAIL="$OPTARG" ;;
    g) GIT_DIR="$OPTARG" ;;
    v) VECTOR_ENV="$OPTARG" ;;
    \?) echo "Invalid option: -$OPTARG" >&2; exit -1 ;;
  esac
done

Bash Excercises