#!/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