how to execute multiple lines of code inside SSH in bash script












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









share|improve this question

























  • maybe EOF at beginning of line ?

    – Archemar
    Jan 23 at 10:20











  • yeah corrected the script,

    – Parul Chourasia
    Jan 23 at 10:30
















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









share|improve this question

























  • maybe EOF at beginning of line ?

    – Archemar
    Jan 23 at 10:20











  • yeah corrected the script,

    – Parul Chourasia
    Jan 23 at 10:30














0












0








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









share|improve this question
















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






linux shell-script ssh here-document






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 24 at 17:44









Rui F Ribeiro

39.8k1479133




39.8k1479133










asked Jan 23 at 10:09









Parul ChourasiaParul Chourasia

215




215













  • maybe EOF at beginning of line ?

    – Archemar
    Jan 23 at 10:20











  • yeah corrected the script,

    – Parul Chourasia
    Jan 23 at 10:30



















  • maybe EOF at beginning of line ?

    – Archemar
    Jan 23 at 10:20











  • yeah corrected the script,

    – Parul Chourasia
    Jan 23 at 10:30

















maybe EOF at beginning of line ?

– Archemar
Jan 23 at 10:20





maybe EOF at beginning of line ?

– Archemar
Jan 23 at 10:20













yeah corrected the script,

– Parul Chourasia
Jan 23 at 10:30





yeah corrected the script,

– Parul Chourasia
Jan 23 at 10:30










2 Answers
2






active

oldest

votes


















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.






share|improve this answer

































    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





    share|improve this answer


























    • 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.

      – Parul Chourasia
      Jan 23 at 10:33











    • i think error is in heredoc statements

      – Parul Chourasia
      Jan 23 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

      – Parul Chourasia
      Jan 23 at 11:03











    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496171%2fhow-to-execute-multiple-lines-of-code-inside-ssh-in-bash-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer






























      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.






      share|improve this answer




























        0












        0








        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 23 at 10:46

























        answered Jan 23 at 10:20









        user1133275user1133275

        3,174623




        3,174623

























            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





            share|improve this answer


























            • 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.

              – Parul Chourasia
              Jan 23 at 10:33











            • i think error is in heredoc statements

              – Parul Chourasia
              Jan 23 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

              – Parul Chourasia
              Jan 23 at 11:03
















            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





            share|improve this answer


























            • 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.

              – Parul Chourasia
              Jan 23 at 10:33











            • i think error is in heredoc statements

              – Parul Chourasia
              Jan 23 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

              – Parul Chourasia
              Jan 23 at 11:03














            0












            0








            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





            share|improve this answer















            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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 23 at 10:38

























            answered Jan 23 at 10:25









            msp9011msp9011

            4,23144065




            4,23144065













            • 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.

              – Parul Chourasia
              Jan 23 at 10:33











            • i think error is in heredoc statements

              – Parul Chourasia
              Jan 23 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

              – Parul Chourasia
              Jan 23 at 11:03



















            • 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.

              – Parul Chourasia
              Jan 23 at 10:33











            • i think error is in heredoc statements

              – Parul Chourasia
              Jan 23 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

              – Parul Chourasia
              Jan 23 at 11:03

















            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.

            – Parul Chourasia
            Jan 23 at 10:33





            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.

            – Parul Chourasia
            Jan 23 at 10:33













            i think error is in heredoc statements

            – Parul Chourasia
            Jan 23 at 10:33





            i think error is in heredoc statements

            – Parul Chourasia
            Jan 23 at 10:33




            1




            1





            I think i got it. code in red lines are not error. it looks like this because it is inside <<EOF....EOF block

            – Parul Chourasia
            Jan 23 at 11:03





            I think i got it. code in red lines are not error. it looks like this because it is inside <<EOF....EOF block

            – Parul Chourasia
            Jan 23 at 11:03


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496171%2fhow-to-execute-multiple-lines-of-code-inside-ssh-in-bash-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to make a Squid Proxy server?

            Is this a new Fibonacci Identity?

            19世紀