IF condition failing in a SSH script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting IF condition failing in a SSH script
# 1  
Old 11-19-2007
IF condition failing in a SSH script

Hi,

I'm ssh-in to a remote machine (ubuntu) and trying to execute a little script in there.The script looks like this:

Code:
ssh user@ubuntu <<EOF
cd ~/test
ls -l
echo "Continue counting files starting with a`s ?"
read answer
if [ "$answer" = y ]
then 
ls -l a*
else
exit
fi
EOF

Now everything works fine up to the IF test. The script seems to escape the read command, (doesn't prompt ), and all the rest of the IF gets ignored. It shows an error near the "then" part. I tried building a local script in the remote machine and recalling that from the SSH script, but that seems to fail too. Built a simple script with the case condition but that seems to have an error - "unexpected end of file", even though everything looks correct,( checked even with od -c for any hidden character)
In other words every simple script that I built with a test condition in a SSH script
seems to fail.

Anybody any ideas ?

And in general, is there a rule that I should keep in mind when executing a test condition in a remote machine thru a SSH script ?

Last edited by rubionis; 04-15-2008 at 06:42 PM.. Reason: added code tags
# 2  
Old 11-19-2007
Quote:
Originally Posted by rubionis
echo "Continue counting files starting with a`s ?"
How about removing the back quote?

Do you have "#!/bin/sh" at the start of the script?
# 3  
Old 11-19-2007
Yes, I forgot to say I have #!/bin/bash at the head of the script.

And I actually don't have back tick in the script, ( Yes I'm aware of special characters conflicts in a script), I just put a`s as an example. Say I avoid all special characters in the ssh script, does the script look OK ?

So my question is, if there is any known conflict when using test condition in a SSH script,.which I'm not aware of, or put it in other words what is a general format of test condition in a SSH script. Or am I missing something in my script?

Thanks for your quick reply.
# 4  
Old 11-19-2007
Indeed, you have an open-ended single-quote which should be closed.

But...you have bigger problems than that, because this is a *remote* script. So, statements like this:

Code:
ssh HOST <<EOF
    echo $HOSTNAME
EOF

will show the local value of HOSTNAME, since the script is evaluated by the shell before it is sent to the remote host. So, all variables that you don't want evaluated need to be escaped (i.e., "\$HOSTNAME"). If you don't need to reference ANY local variables, you can prevent the script from being evaluated be escaping the "EOF" separator:

Code:
ssh HOST <<\EOF
    # the backslash above will prevent the Variables below from being evaluated
    echo $HOSTNAME
EOF

Generally, when attempting to code/debug these kinds of scripts, you should send them to a local file to ensure the code evaluates the way you expect, and then you can run "sh -n" to verify the syntax:

Code:
cat <<EOF > /tmp/test.script
    # this is a temporary location
    echo $VALUE1 \$VALUE1
EOF

# 5  
Old 11-19-2007
Thanks gus2000,

The separator \EOF did the trick regarding all the variables in the SSH script. Another useful info to keep in mind !
( I know, I have to be careful with the variables, BTW I'm using a longer awk command in the script and I escaped all the $ with \$ . )

But unfortunately the IF test fails again:

Quote:
syntax error near unexpected token `then'
even though "everything looks OK".

So my MAIN question remains the IF test. In other how would I write an IF test (or a TEST cmd in general ) in ssh script without failing ?

Last edited by rubionis; 04-15-2008 at 06:58 PM.. Reason: code tags
# 6  
Old 11-19-2007
Do You even get a prompt for answer to Your read statement? I think that since ssh is non-interactive (not a controlling terminal) You can't have a terminal like session, either You must work with something like expect or keep the script locally and just "collect" whatever info You need from the remote host and process it locally.

Make a local script and run it locally:

Quote:
#!/bin/bash
ssh user@ubuntu ls -l \~/test
echo "Continue counting files starting with a ?"
read answer
if [ "$answer" = y ]
then
ssh user@ubuntu ls -l 'a*'
else
exit
fi
You must protect any arguments from being interpreted by Your local shell wich backslash or single quotes.
/Lakris
# 7  
Old 11-20-2007
Quote:
Originally Posted by Lakris
I think that since ssh is non-interactive (not a controlling terminal) You can't have a terminal like session, either You must work with something like expect or keep the script locally and just "collect" whatever info
X100!!!

I've had this bite me before with some programs that want a terminal to send data too... Root get's email garbage then.

Quote:
And in general, is there a rule that I should keep in mind when executing a test condition in a remote machine thru a SSH script ?
Act like there is nowhere for it to read info from except local system variables, files and program output. I would usually use a script like this to cycle between many boxes If I want to interact with the script, I have my box do the control while the remote does the data gathering.

Like this:

ssh user@ubuntu "ls -l /test"
echo "Continue counting files starting with a`s ?"
read answer
if [ "$answer" = y ]
then
ssh user@ubuntu "ls -l a*"
fi

It's not as efficient with multple sign-ons, but the logic is all local. The only exception is if there is large amounts of data to filter out (with a grep or something) - I'll try and be nice to the network.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SSH is failing due to unknown reason

Hi, I have setup keys between user1@server1 and user2@server2 however, the ssh is failing. server1 is Linux 3.10.0-514.6.2.el7.x86_64 #1 SMP whereas server2 is 5.10 Generic_150400-40 sun4v sparc sun4v I have checked port 22 to be open and keys to be correct. I also find the permissions... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Ssh failing due to Bad owner error

i am logged in with "user1" on host1. I m trying to ssh to host2 using user id "user2" $ ssh user2@host2 Bad owner or permissions on /home/user1/.ssh/config Question 1: Can you please tell me why am i getting the Bad owner permissions error on that directory ? Question 2: Where is it... (7 Replies)
Discussion started by: mohtashims
7 Replies

3. Shell Programming and Scripting

Writing if condition in shell script and failing to do requirement

Hi, I am trying to edit the values in a file. For example i am trying to edit the value of "ABC" in a file by executing shell script. Please Note that ABC value can be there mulitple times or it may not be there in the file Conditions for it is 1. If ABC is less than 123 then it should... (14 Replies)
Discussion started by: darling
14 Replies

4. UNIX for Dummies Questions & Answers

Crontab script failing

Hello, A crontab script is failiing everyday but when we execute manually it runs fine Below is the script scheduled: 00 23 * * * sh /db2backup/scripts/db2_hot_backup.ksh TRAVFF > /dev/null 2>&1 Error: cat TRAVFF_online_04022014_2300.log Started : Wed Apr 2 23:00:00 EDT 2014... (2 Replies)
Discussion started by: Vishal_dba
2 Replies

5. Shell Programming and Scripting

Connect (SSH) to Windows server via Linux server through a script and passing command.. but failing

I am trying to connect to Windows server via Linux server through a script and run two commands " cd and ls " But its giving me error saying " could not start the program" followed by the command name i specify e g : "cd" i am trying in this manner " ssh username@servername "cd... (5 Replies)
Discussion started by: sunil seelam
5 Replies

6. UNIX for Advanced & Expert Users

SSH public key failing without error message

My password-free ssh connection has worked in the past but has stopped working and I can't get it going again. The files in .ssh on both source and target are set to 600: drwx------ 2 ingres 1024 Mar 2 13:57 . drwxr-xr-x 25 ingres 2048 Mar 29 09:38 .. -rw------- 1 ingres ... (9 Replies)
Discussion started by: Catullus
9 Replies

7. Shell Programming and Scripting

SCP Failing - In Script

I create a file that may contain several full path name files on a remote Linux that are to be copied. This file, called AWKOUTPUT is created from another script. It contains: X/picture1.png The script is very simple ------------------------------------------- REMOTEDIR="/var/CC/Stuff"... (4 Replies)
Discussion started by: mohrsville12
4 Replies

8. Shell Programming and Scripting

Expect script help needed- script failing if router unavailable

Hey all. Sometimes I'm tasked to change some router configs for the entire network (over 3,000 Cisco routers). Most of the time its a global config parameter so its done with a loop and an IP list as its the same configuration change for all routers. This is working OK. However, sometimes an... (3 Replies)
Discussion started by: mrkz1974
3 Replies

9. Shell Programming and Scripting

Using ssh to transfer file not working inside if-condition

Hi all, ssh uname@remote_server 'cat /tmp/remote_file_name > home_dir/a512386/new/local_file_name' The above given command is working fine. but if i try to move the file only if exists in the remote server i.e) giving the same within if condition it executes but the file is not stored in my... (1 Reply)
Discussion started by: Shri123
1 Replies

10. Shell Programming and Scripting

ssh - rm failing

Hi, Please help me... I am creating a string of filenames with absolute path and deleting those files situated in the remote server using ssh .. but it doesnot work.. Can anyone help me... here is my code for FileName in ${myDire} do Tmp=`basename... (4 Replies)
Discussion started by: shihabvk
4 Replies
Login or Register to Ask a Question