java要多思考下

成长^_^

   ::  :: 新随笔 ::  ::  :: 管理 ::
  33 随笔 :: 0 文章 :: 19 评论 :: 0 Trackbacks
取脚本运行参数: $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数据批量操作脚本
#!/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

调用形式:
./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

 
posted on 2013-02-22 17:50 java要多思考下 阅读(1780) 评论(0)  编辑  收藏 所属分类: 系统运维

只有注册用户登录后才能发表评论。


网站导航: