shell命令的相关事例操作
案例1:变量、函数、判断、比较
test.sh
- #!/bin/bash
- echo ======================== this is a bash demo========================
- printf "%s is %d years old \n" zhengjinwei 23
- declare count=10
- declare fruit=apple
- declare strNormal="we have $count ${fruit}'s "
- declare strSpecial="we have $count ${fruit} "
- function printStr()
- {
- if [ $count -gt 1 ] ; then
- echo "$strNormal"
- else
- echo "$strSpecial"
- fi
- }
- printStr
- echo $(uname)
- #########################################
- declare num=1000
- function printNum()
- {
- local num=10
- let num+=10
- echo $num
- }
- echo "the global num is : $num "
- echo -e "call printNum Function :"
- printNum
- o "==================this is if else fi ....test demo====================="
- if [ -f "test.sh" ] ; then
- echo "named test.sh is a file.."
- else
- echo "named test.sh is not a file.."
- fi
- function checkVariableHaveNum()
- {
- if [ -n "$1" ] ; then
- echo "this variable have value"
- else
- echo "this variable have not value"
- fi
- }
- declare g_num=10
- checkVariableHaveNum $g_num
- function checkVariableEqual()
- {
- if [ "$1" -eq "$2" ] ; then
- printf "%d == %d " $1 $2
- elif [ "$1" -ne "$2" ] ; then
- printf "%d != %d " $1 $2
- elif [ "$1" -ge "$2" ] ; then
- printf "%d >= %d " $1 $2
- elif [ "$1" -gt "$2" ] ; then
- printf "%d > %d" $1 $2
- elif [ "$1" -lt "$2" ] ; then
- printf "%d < %d" $1 $2
- else
- printf "%d <= %d" $1 $2
- fi
- }
- checkVariableEqual 2 4
- checkVariableEqual 4 2
- checkVariableEqual 2 2
-
案例2:字符串操作(取子串)
- echo "-------------------------------------"
- ########this is string opt################################
- #get string's length
- declare strName="zhengjinwei"
- declare strNameLength=${#strName}
- printf "the length of %s is %d \n" $strName $strNameLength
- function getChildString()
- {
- local StrResourse=$1
- local nParamCount=$2
- local nStartPos=$3
- local nEndPos=$4
- if [[ $nParamCount -lt 3 ]] ; then
- echo "param less than 3 is err"
- elif [[ $nParamCount -gt 4 ]] ; then
- echo "param more than 4 is err"
- else
- if [[ $nParamCount -eq 3 ]] ; then
- let nEndPos=${#StrResourse}
- else
- let nEndPos=$4
- fi
- fi
- local strResult=${StrResourse:$nStartPos:$nEndPos}
- printf "the child string in %s from position %d to %d is %s: \n\n\n" $StrResourse $nStartPos $nEndPos $strResult
- }
- getChildString "zhengjinwei" 4 2 6
- getChildString "zhengjinwei" 3 2
-
案例3:迭代、数组
- ###########################################################
- <span style="font-size:24px;"># this is array test demo
- declare array_int=(1 2 3 4 5 6 7)
- printf "the int value in array_int which index is %d is %d\n\n" 0 ${array_int["0"]}
- echo "=============for loop=============================="
- declare array_int_len=${#array_int[*]}
- for (( i=0 ;i<"$array_int_len";i++ ))
- do
- echo -e ${array_int[$i]}
- done
- echo ""
- echo "============for iterator loop================================"
- for value in ${array_int[@]}
- do
- echo -e $value
- done
- echo ""
- echo "==========while loop=================================="
- declare i=0
- while (( $i<$array_int_len ))
- do
- echo -e ${array_int[$i]}
- let i=i+1
- done
- ########################################################</span>
案例4:时间操作
- function getNowtime()
- {
- local _date=$(date +%Y-%m-%d:%H-%M-%S:%A)
- echo $_date
- }
- function getCostTime()
- {
- local _start=$(date +%S)
- sleep 2
- local _end=$(date +%S)
- local _cost=$(( _end - _start ))
- echo $_cost
- }
- function getDiffTimeBetweenInSeconds()
- {
- local _start=$(date +%s -d '2010-09-01 17:00:00')
- local _end=$(date +%s -d '2011-09-01 18:40:40')
- tep=$(($_end-$_start))
- tep2=$(( 3600*24 ))
- result=$(( $tep/$tep2 ))
- extra=$[ $tep%$tep2 ]
- declare _hours=0
- declare _seconds=0
- if [ $extra -ge 3600 ] ; then
- _hours=$(( $extra/3600 ))
- _seconds=$(( $extra%3600))
- else
- _hours=0
- _seconds=$extra
- fi
- printf "%d days ---%d hours ----%d seconds \n\n" $result $_hours $_seconds
- }
- getNowtime
- getCostTime
- getDiffTimeBetweenInSeconds


浙公网安备 33010602011771号