-2

I have wrote a shell script for logging into servers and run few commands to start and stop processes running on it, So I tried below code, but it is showing error, please help me with the script.

for i in 'cat /tmp/hb`     //server name would be saved in /tmp/hb
ssh $i
j=hostname 

If $i -ne $j
then
echo "server not logged in"

else echo "Server logged in"
if
$i == $j
sudo /opt/OV/bin/opcagt -status |grep -i ”message agent are not buffering" 

elif
sudo /opt/OV/bin/opcagt -stop
sudo /opt/OV/bin/opcagt -start
sudo /opt/OV/bin/opcagt -stop
sudo /opt/OV/bin/opcagt -status |grep -i ”message agent are not buffering

elif
sudo /opt/OV/bin/opcagt -kill
sudo /opt/OV/bin/opcagt -start
sudo /opt/OV/bin/opcagt -status |grep -i ”message agent are not buffering 

fi

Above script gives error :

./heart: syntax error at line 2: `ssh' unexpected
  • 1
    You start the cat /tmp/hb with a single quote, not a back tick – Eric Renouf Jun 13 '16 at 17:48
  • 2
    Run your code through shellcheck.net first. Correct the errors that it finds (which will be numerous) and then update your question. – John1024 Jun 13 '16 at 17:48
  • Unfortunately the code makes so little sense that I would be surprised if anyone was able to correct the errors with a sufficient probability of the result matching what you think you intend. Describing what it's supposed to do would be one good starting point. Learning a little about the scripting language you're trying to use would be sensible, too. – roaima Jun 13 '16 at 23:04
  • Yes , I am trying to fix errors using shellcheck.net.. I didn't know about this site before, it's helpful to me ... as I can check for errors in my script before execution. – Noopur Dubey Jun 13 '16 at 23:11
1

Your first problem is that the block that is to be looped through by a for needs to have a do and done block around it:

for f in `cat file`
do
  ...
done

Your second problem is that the remainder of the loop body (the if statements will not be executed inside the ssh session on the target host(s). You'll have to somehow push the if statements to the target host and execute a script there.

  • Thanks a lot... it cleared the idea to alot. I'll come up with the script , with above mentioned points in my mind – Noopur Dubey Jun 13 '16 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.