ssh 与 for 循环 - 括号问题

ssh 与 for 循环 - 括号问题

我有以下脚本:

while [ "$1" != "" ]
do
case "$1" in
    -h) HOST="$2"; shift 2;;
esac
done

if test -z "$HOST"
then
    [...]
else
    TODAY=$(date +'%Y-%m-%d') 
    configs=("/etc/inittab" "/etc/default/cron" "/etc/default/login" "/etc/default/su" "/etc/group" "/etc/inet/inetd.conf" "/etc/mnttab" "/etc/netmasks" "/etc/nsswitch.conf" "/etc/protocols" "/etc/services" "/etc/syslog.conf" "/var/sadm/install/contents" "/etc/user_attr" "/etc/passwd")  
    ssh $HOST 
    for (i=0; i<${#configs[*]}; i++) 
    do 
        digest -v -a md5 ${configs[$i]} | awk '{print $2,$4}' | sed 's/) (/\n/;s/[()]//g'; 
    done > /tmp/md5_config_$TODAY.txt
fi

如果我使用主机名 (scriptname.sh -h hostname) 执行脚本,则会收到错误

line 97: syntax error near unexpected token `('
line 97: `for (i=0;i<${#configs[*]};i++;); '

有办法解决吗?我想使用数组“configs”的文件名在远程主机上执行“digest”命令

编辑:实际状态

else
    TODAY=$(date +'%Y-%m-%d') 
    configs=("/etc/inittab" "/etc/default/cron" "/etc/default/login" "/etc/default/su" "/etc/group" "/etc/inet/inetd.conf" "/etc/mnttab" "/etc/netmasks" "/etc/nsswitch.conf" "/etc/protocols" "/etc/services" "/etc/syslog.conf" "/var/sadm/install/contents" "/etc/user_attr" "/etc/passwd")  
    ssh $HOST 
    for ((i=0; i<${#configs[*]}; i++)) 
    do 
        digest -v -a md5 ${configs[$i]} | awk '{print $2,$4}' | sed 's/) (/\n/;s/[()]//g'; 
    done > /tmp/md5_config_$TODAY.txt
fi

答案1

如果我要写这样的东西我会

TODAY=`date --iso`
CONFIGS="/etc/inittab /etc/default/cron /etc/default/login ..."

ssh -q $HOST 'for cfg in '${CONFIGS}'; do digest -v -a md5 ${cfg}; done' |\
    awk '{print $2,$4}' | sed 's/) (/\n/;s/[()]//g' >> /tmp/md5_config_$TODAY.txt

但是,您可能想查看一下绊线以及为此目的而制作的其他工具。

相关内容