The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
csh failing to call an 2 embedded csh script pollsizer Shell Programming and Scripting 1 06-07-2008 01:22 PM
Shell script - If---fi condition yog_chavan Shell Programming and Scripting 4 05-07-2008 09:46 AM
need help with test condition in shell script pieman8080 Shell Programming and Scripting 9 09-11-2006 05:20 PM
UNIX script failing .... khan1978 UNIX for Advanced & Expert Users 1 12-05-2005 01:28 PM
OR'ing condition in script vino Shell Programming and Scripting 9 04-21-2005 08:34 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-19-2007
rubionis rubionis is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 18
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 05:42 PM.. Reason: added code tags
  #2 (permalink)  
Old 11-19-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by rubionis View Post
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 (permalink)  
Old 11-19-2007
rubionis rubionis is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 18
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 (permalink)  
Old 11-19-2007
gus2000 gus2000 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 157
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 (permalink)  
Old 11-19-2007
rubionis rubionis is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 18
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 05:58 PM.. Reason: code tags
  #6 (permalink)  
Old 11-19-2007
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 242
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 (permalink)  
Old 11-20-2007
edcrosbys edcrosbys is offline
Read Only
  
 

Join Date: Jul 2007
Posts: 15
Quote:
Originally Posted by Lakris View Post
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.
Closed Thread

Bookmarks

Tags
grep or, linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0