码迷,mamicode.com
首页 > 其他好文 > 详细

Bash Excercises

时间:2016-12-21 20:30:11      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:sage   sed   ref   int   one   getopt   .com   i++   static   

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

标签:sage   sed   ref   int   one   getopt   .com   i++   static   

原文地址:http://www.cnblogs.com/codingforum/p/6208620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!