取腳本運行參數: $0 $1 $2 ...
取腳本運行參數個數: $#
取腳本運行的所有參數:$*
判斷字符串相等:if [[ "$1" = "-cmd" ]]; then command=$2 fi ([后有空格,]前也有空格)
變量命名:string1="/usr/local/redis/bin/redis-cli"
取變量值:$string1
命令調用:`export abc="abc"`
字符串命令調用:result=`eval $string`
將命令調用值賦給數組:results=(`eval $string`)
取數組長度:${#results[@]}
取數組值:${results[i]}
取運行參數數組值:${!i}
調用外部腳本:. xxxx.sh
附:redis數據批量操作腳本
取腳本運行參數個數: $#
取腳本運行的所有參數:$*
判斷字符串相等:if [[ "$1" = "-cmd" ]]; then command=$2 fi ([后有空格,]前也有空格)
變量命名:string1="/usr/local/redis/bin/redis-cli"
取變量值:$string1
命令調用:`export abc="abc"`
字符串命令調用:result=`eval $string`
將命令調用值賦給數組:results=(`eval $string`)
取數組長度:${#results[@]}
取數組值:${results[i]}
取運行參數數組值:${!i}
調用外部腳本:. xxxx.sh
附:redis數據批量操作腳本
#!/bin/bash
command="keys '*'"
action="noaction"
redis_cli="/usr/local/redis/bin/redis-cli"
if [[ "$1" = "-cmd" ]]; then
command=$2
fi
if [[ "$3" = "-act" ]]; then
action=$4
fi
#collect the command result to an array named results
echo "=====> do command: $command"
exec_command="$redis_cli $command"
results=(`eval $exec_command`)
if [[ ${#results[@]} = 0 ]];then
exit 1
fi
for (( i = 0; i < ${#results[@]}; i++ )); do
case $action in
del)
doaction="$redis_cli del ${results[i]}"
;;
ttl)
doaction="$redis_cli ttl ${results[i]}"
;;
expire)
doaction="$redis_cli expire ${results[i]} 60"
;;
*)
doaction="echo \"${results[i]}\""
;;
esac
actionResult=`eval $doaction`
echo "=====> do action: $doaction ==> result: $actionResult"
done
調用形式:
command="keys '*'"
action="noaction"
redis_cli="/usr/local/redis/bin/redis-cli"
if [[ "$1" = "-cmd" ]]; then
command=$2
fi
if [[ "$3" = "-act" ]]; then
action=$4
fi
#collect the command result to an array named results
echo "=====> do command: $command"
exec_command="$redis_cli $command"
results=(`eval $exec_command`)
if [[ ${#results[@]} = 0 ]];then
exit 1
fi
for (( i = 0; i < ${#results[@]}; i++ )); do
case $action in
del)
doaction="$redis_cli del ${results[i]}"
;;
ttl)
doaction="$redis_cli ttl ${results[i]}"
;;
expire)
doaction="$redis_cli expire ${results[i]} 60"
;;
*)
doaction="echo \"${results[i]}\""
;;
esac
actionResult=`eval $doaction`
echo "=====> do action: $doaction ==> result: $actionResult"
done
調用形式:
./xxxx.sh
./xxxx.sh -cmd 'keys aa*'
./xxxx.sh -cmd 'keys aa*' -act del./xxxx.sh -cmd 'keys aa*' -act expire
./xxxx.sh -cmd 'keys aa*' -act ttl