0

I want to execute below code,but it is highlighting error in code with red in vi. Getting error after sudo ssh -t root@$ip << EOF line .Where have I scripted wrongly?

#!/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           
2
  • maybe EOF at beginning of line ? Commented Jan 23, 2019 at 10:20
  • yeah corrected the script, Commented Jan 23, 2019 at 10:30

2 Answers 2

0

Use https://www.shellcheck.net/ (there is a vim plugin) It would tell you

Line 18:
  EOF
 ^-- SC1039: Remove indentation before end token (or use <<- and indent with tabs).

Then go on to list many other issues.

0

You are trying to store an array of values in iplist[@] but as a static declaration...

Try as follows:

#!/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   
3
  • error is not in that line. i have used iplist[@]=$(python -c "import json; print \",\".join(json.load(open('${repair.json}','r'))[\"iplist\"])") in other script and it runs. Commented Jan 23, 2019 at 10:33
  • i think error is in heredoc statements Commented Jan 23, 2019 at 10:33
  • 1
    I think i got it. code in red lines are not error. it looks like this because it is inside <<EOF....EOF block Commented Jan 23, 2019 at 11:03

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.