LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-27-2013, 07:33 AM   #1
vicky007aggrwal
Member
 
Registered: Aug 2012
Posts: 92

Rep: Reputation: Disabled
variable not getting populated via ssh


I'm writing below script in which I need to execute a command remotely but the problem in this script is that the "output" variable value is not getting populated & while running this script i'm getting the below error :-.
I do have the "abc war" existing over my remote machine even then this script is throwing no such file error.
)

please suggest how to solve this
*****************error******************
ls: cannot access /host/apps/tc/webapps/abc*: No such file or directory
*******************************************


**************script********************
Quote:
USER=$1
HOST=$2
WAR_NAME=$3

ssh $USER@${HOST} << EOF
output=`ls -ld /host/apps/tc/webapps/${WAR_NAME}*`
if [ `echo $output` != "" ];then
rm -rf /host/apps/tc/webapps/${WAR_NAME}*

fi
EOF
**********************************

************MY OS detail ********************
OS variant =Red Hat Enterprise Linux Server release 6.3 (Santiago

Last edited by vicky007aggrwal; 10-27-2013 at 08:21 AM.
 
Old 10-27-2013, 08:10 AM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
I think the problem might not be the populating of $output, but a syntax error in this line:

Code:
if [ `echo $output` != "" ];then
I get a "too many arguments" error if I try this:

Code:
$ output=`ls -ld /home/gerald`
$ echo $output
drwxr-xr-x 41 gerald gerald 4096 Oct 27 10:07 /home/gerald
$ if [ `echo $output` != "" ]; then echo "Not empty"; fi
bash: [: too many arguments
$ if [ "$output" != "" ]; then echo "Not empty"; fi # try this instead
Not empty
 
Old 10-27-2013, 08:19 AM   #3
vicky007aggrwal
Member
 
Registered: Aug 2012
Posts: 92

Original Poster
Rep: Reputation: Disabled
No that not the case, I ' m executing the command via SSH first of all & if I try printing the value of "output" variable just after the below command then it will come as BALNK.

Quote:
output=`ls -ld /host/apps/tc/webapps/${WAR_NAME}*`
if I can get the value of output variable it will solve my problem

Last edited by vicky007aggrwal; 10-27-2013 at 08:24 AM.
 
Old 10-27-2013, 08:53 AM   #4
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
Ah, sorry - I failed to spot that you'd included the error message.

Experimented a bit with a VM, and I found that the command substitution (`ls -ld...`) is being run on my laptop, before the ssh connection is established.

Not sure whether there's a solution to this when using a here document, but an alternative would be to use an "if file exists" test in the conditional statement:

Code:
if [ -a /host/apps/tc/webapps/${WAR_NAME}* ]; then
 
1 members found this post helpful.
Old 10-27-2013, 08:59 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
try

Code:
ssh $USER@${HOST} "test -e /host/apps/tc/webapps/${WAR_NAME}* && rm -rf /host/apps/tc/webapps/${WAR_NAME}*"
 
1 members found this post helpful.
Old 10-27-2013, 06:00 PM   #6
vicky007aggrwal
Member
 
Registered: Aug 2012
Posts: 92

Original Poster
Rep: Reputation: Disabled
Thanks Firerat& Robhogg for your inputs your solution worked.

But now i have another need of using variable in the script as mentioned below , again the variable CMD value is not get populated.
Please suggest how to fix this


Quote:
USER=$1
HOST=$2
WAR_NAME=$3

ssh $USER@${HOST} << EOF
output=`ls -ld /host/apps/tc/webapps/${WAR_NAME}*`
rm -rf /host/apps/tc/webapps/${WAR_NAME}* && rm -rf /host/apps/tc/webapps/${WAR_NAME}*

for i in `grep Context /host/apps/tc/conf/server.xml`
do
CMD=`echo $i|awk -F= '{ print $1 }'`
[ $CMD== "path" ] && echo "path entry exist"
done
fi
EOF

Last edited by vicky007aggrwal; 10-27-2013 at 06:02 PM.
 
Old 10-27-2013, 07:03 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
back ticks (`) are depreciated , $() is there replacement, and they are 'safer', easier to read and write

http://pubs.opengroup.org/onlinepubs...l#tag_02_06_03


a few things 'wrong' with your for loop
I don't know what is in server.xml, but given your grep patten, I amuse spaces,

so, lets say with have a line
Code:
path= Context is this
your for loop will 'pass'
  1. path=
  2. Context
  3. is
  4. this



even so , all you do is later process with awk, so..

try this replacement

Code:
for Cmd in $( awk -F= '/Context/{print $1)' /host/apps/tc/conf/server.xml );do
    [[ "$Cmd" == "path" ]] && echo "path entry exist"
done
Note, I have used CaMel case, uppercase variables are commonly used in the 'system environment' avoid assigning to UPPERCASE in scripts
UpperCase would be better

However, what is that actually doing?
This will do the same
Code:
awk -F\= '/Context/{if ( $1 == "path" ){print "path entry exist"}}'/host/apps/tc/conf/server.xml
or

Code:
grep -q "path=.*Context" /host/apps/tc/conf/server.xml && echo "path entry exist"
and none of them , as far as I see add any value to anything, you just get a bunch of lines that all read "path entry exist"

am I missing something?

Last edited by Firerat; 10-27-2013 at 07:06 PM. Reason: few typos
 
1 members found this post helpful.
Old 10-29-2013, 06:54 PM   #8
vicky007aggrwal
Member
 
Registered: Aug 2012
Posts: 92

Original Poster
Rep: Reputation: Disabled
Thanks all , my issues got fixed by using \ in front of $ varaibles
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] passing local variable with sed via ssh tripialos Linux - Newbie 11 09-05-2013 08:18 AM
[SOLVED] Bash - Passing variable to ssh zepphead5 Programming 8 05-09-2010 09:34 AM
Bash: How do I read data from a variable while that variable is being populated? theaceoffire Programming 4 04-23-2010 02:29 PM
No DISPLAY variable when connecting via SSH ciphyre Linux - General 3 09-12-2008 02:36 PM
SSH not setting Display variable lapope Linux - General 5 09-01-2008 01:46 PM


All times are GMT -5. The time now is 04:55 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration