我想执行下面的代码,但它在 vi 中用红色突出显示代码中的错误。之后出现错误sudo ssh -t root@$ip << EOF行。我哪里写错了?
#!/bin/bash
cassandra_home=$(python -c "import json; print \",\".join(json.load(open('${repair.json}','r'))[\"cassandra_home\"])")
iplist[@]=$(python -c "import json; print \",\".join(json.load(open('${repair.json}','r'))[\"iplist\"])")
for ip in ${iplist[@]}
do
sudo ssh -t root@$ip << EOF
for ip in ${iplist[@]}
do
echo Checking $ip for ongoing repairs
${cassandra_home}nodetool -h $ip tpstats | grep Repair#
response=$?
if [ $response -eq 0 ]; then
repair_ongoing=true
echo "Ongoing repair on $ip"
fi
done
if ! [ $repair_ongoing ]; then
## echo "Taking a snapshot."
## ${cassandra_home}bin/nodetool -h $ip snapshot
echo "Starting repair on $ip"
start=$(date +%s)
${cassandra_home}bin/nodetool -h $ip repair -pr -inc -local metadata
sleep 3
${cassandra_home}bin/nodetool -h $ip cleanup metadata
end=$(date +%s)
#echo "ks.tab,st,et,last run,status">>repair_status.csv
echo "Repair and cleanup completed for metadata in $((end - start)) seconds"
fi
exit 0
EOF
done
答案1
使用https://www.shellcheck.net/(有一个vim插件)它会告诉你
Line 18:
EOF
^-- SC1039: Remove indentation before end token (or use <<- and indent with tabs).
然后继续列出许多其他问题。
答案2
您正在尝试将值数组存储在iplist[@]
但作为静态声明中......
尝试如下:
#!/bin/bash
cassandra_home=(`python -c "import json; print \",\".join(json.load(open('${repair.json}','r'))[\"cassandra_home\"])"`)
iplist[@]=(`python -c "import json; print \",\".join(json.load(open('${repair.json}','r'))[\"iplist\"])`)
for ip in ${iplist[@]}
do
sudo ssh -t root@$ip "
for ip in ${iplist[@]}
do
echo Checking $ip for ongoing repairs
${cassandra_home}nodetool -h $ip tpstats | grep Repair#
response=$?
if [ $response -eq 0 ]; then
repair_ongoing=true
echo \"Ongoing repair on $ip\"
fi
done
if ! [ $repair_ongoing ]; then
## echo \"Taking a snapshot.\"
## ${cassandra_home}bin/nodetool -h $ip snapshot
echo \"Starting repair on $ip\"
start=`date +%s`
${cassandra_home}bin/nodetool -h $ip repair -pr -inc -local metadata
sleep 3
${cassandra_home}bin/nodetool -h $ip cleanup metadata
end=`date +%s`
#echo \"ks.tab,st,et,last run,status\">>repair_status.csv
echo \"Repair and cleanup completed for metadata in $end - $start seconds\"
fi
exit 0"
done