?
Solved

for loop don't pick file after ssh

Posted on 2007-10-16
32
Medium Priority
?
929 Views
Last Modified: 2008-01-09
hi guys,
below script is working without ssh but won't work if i use ssh.
do any body know what's reason.

ssh -v build2@server20
hostname
pwd
cd /home/build2/projectl
for pvtfile in *.txt
do
print "deciphering '$pvtfile'"
done

output:

deciphering ''
deciphering ''
not pick value from pvtfile...
0
Comment
Question by:parlepoint
  • 15
  • 12
  • 5
32 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 20087053
Hi,

Do you mean that you want to run the subsequent commands on the remote node?

Is this a shell script to run on unix / linux?

try:

ssh -v build2@server20 << END
hostname
pwd
cd /home/build2/projectl
for pvtfile in *.txt
do
print "deciphering '$pvtfile'"
done
END
0
 

Author Comment

by:parlepoint
ID: 20087519
run on sun os in unix

0
 
LVL 48

Expert Comment

by:Tintin
ID: 20088763
omarfarid's solution will work, but the usual way is to invoke the script on the remote server.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:parlepoint
ID: 20089973
some how everything is working but not picking up *.txt files
also one more thing i want to pass private variable to remote app server ..
like $x=123 and same thing should work for remote application...
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
ID: 20090004
You'd be better of scp'ing the script to the server and then invoking it, eg:

#!/bin/sh

cat >/tmp/$$ <EOF
hostname
pwd
cd /home/build2/projectl
for pvtfile in *.txt
do
print "deciphering '$pvtfile'"
done
EOF

scp /tmp/$$ build2@server20:script
ssh build2@server20 "sh script"
0
 

Author Comment

by:parlepoint
ID: 20090435
Thanks for your reply...
it works...but only problem is variable...it's not taking...
can you tell me how variable works in remote application...after ssh..
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20090466
You'll need to show how you're trying to use it.

Simplistically

$ x=123
$ ssh remote-server "echo x is $x"
x is 123
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20090485
Hi,

| agree with Tintin recommendation to copy the script to remote server, then execute it.

0
 

Author Comment

by:parlepoint
ID: 20092646
i want to use like this and want to use abc value in hello.ksh script.
give me suggestion.

abc=server1
ssh -v build2@server20 -C "echo var is $abc" && "/home/cmbuild2/abc/hello.ksh"


0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20092826
Hi,

I do not see any problem with your example.

I tried the following:

a=a.txt
ssh omar@remoteserver -C "echo $a" && "ls"

works fine and echo a.txt and list remote dir

ssh omar@remoteserver  "echo $a ; ls-l $a"

works fine and echo a.txt and do long listing of remote file a.txt

But, it return error with

ssh omar@remoteserver -C "echo $a" && "ls -l $a"

bash: ls -l  a.txt: command not found
0
 

Author Comment

by:parlepoint
ID: 20093324
thanks for your reply but problem is $a value is not work ...i tried with your example..but didn't work..
this is output i am getting.

also variable value is going to use in script..


$>ls -l $a
total 4
-rwxrwsrwx   1 cmbuild2 cmbuild        0 Oct 16 09:09 a.doc
-rwxrwsrwx   1 cmbuild2 cmbuild        0 Oct 16 09:09 b.doc
-rwxrwsrwx   1 cmbuild2 cmbuild        0 Oct 16 09:09 bb.txt
-rwxrwsrwx   1 cmbuild2 cmbuild        0 Oct 16 09:09 c.doc
-rwxrwsrwx   1 cmbuild2 cmbuild        0 Oct 16 09:09 cc.txt
-rwxrwxrwx   1 cmbuild2 cmbuild      117 Oct 17 08:50 hello.ksh
-rwxrwxrwx   1 cmbuild2 cmbuild      141 Oct 17 08:51 test.ksh
BUILD:cmbuild2@pitt30:/home/cmbuild2
$>ssh build2@server20  "echo $a ; ls-l $a"
cmbuild2@pittwls20's password:
/home/cmbuild2
ksh: ls-l:  not found.
BUILD:cmbuild2@pitt30:/home/cmbuild2
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20093843
Hi,

there is space in ls-l

change  ls-l to ls  -l
0
 

Author Comment

by:parlepoint
ID: 20094330
yes..ls -l is working..but if variable calling in  different script it want work

$>ssh build2@server20  "echo $a ; /home/build2//hello.ksh"
build2@server20's password:

THIS IS HELLO.KSH SCRIPT
i am remote '$a'
+ print i am remote ''
BUILD:cmbuild2@pitt30:/home/build2
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20095141
Hi,

try

a=a.txt
eval ssh user@remoteserver -C "echo a=$a ; export a ; a.sh"

on the remote server, a.sh is executable and does the following

ls -l $a


0
 

Author Comment

by:parlepoint
ID: 20096137
i try...but didn't work..

if you trying then first unset variable from remote machine...
then check....won't work ..

0
 
LVL 48

Expert Comment

by:Tintin
ID: 20096317
parlepoint.

How about you try to explain what you are trying to achieve.  This all seems pretty messy the way you want to do it.
0
 

Author Comment

by:parlepoint
ID: 20096831
I was trying this way:

a=qas
eval ssh user@remoteserver -C "echo a=$a ; export $a ; a.sh"

in a.sh
#! /usr/bin/ksh
print " this is '$a'"

output
this is ' '
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20096928
Hi,

I think

print " this is '$a'"

should be

print " this is $a"
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20097247
export $a

should be

export a

However, I still go back to my previous comment that this is all very messy.

Please tell us what problem you are trying to achieve.  I'm sure there will be a much better way of doing it.
0
 

Author Comment

by:parlepoint
ID: 20100707
Okey..sorry for late reply

This is script which i want it..
i have to create hello2.ksh only because want to pick up= for PVT_FILE in *ssm_*.ksh after ssh..


eval ssh $PVT_SUNAME@$PVT_CONNECTHOST "echo a=$PVT_LOC_CONFIG ; echo b=$SSM_DECIPHERCODE ; export a ; export b ; /home/mms/hello2.ksh"

================In hello2.ksh==========

cd "$PVT_LOC_CONFIG/javaserver"
 for PVT_FILE in *ssm_*.ksh
   do
      echo $PVT_FILE
      chmod 600 "$PVT_FILE" ; PVT_RETURN0="$?"
   done
========================================

Still don't get it then let m know i give bit more description..
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20100897
Hi,

Notes:

eval ssh $PVT_SUNAME@$PVT_CONNECTHOST "echo a=$PVT_LOC_CONFIG ; echo b=$SSM_DECIPHERCODE ; export a ; export b ; /home/mms/hello2.ksh"

- variables  PVT_SUNAME    PVT_CONNECTHOST     PVT_LOC_CONFIG    SSM_DECIPHERCODE

are known to your connecting and get replaced by their actual values before executing the ssh command

- variables a & b get set on the remote server and take their values from the evaluated variables. They are not used on the remote node by the script hello2.ksh

- in hello2.ksh script which will run on the remote node, variable PVT_LOC_CONFIG is used (which the remote server knows nothing about it) and instead variable a can be used.

So, change

cd "$PVT_LOC_CONFIG/javaserver"

to

cd "$a/javaserver"

and see if you get what you want.

0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20100913
Hi,

correction:

.
.
.
are known to your connecting server and get replaced ...
.
.
.
0
 

Author Comment

by:parlepoint
ID: 20101579
i was given $a instead of..$PVT_LOC_CONFIG ---- but still didn't work..
i make sample script.

=================in hello1.ksh=================
a=hello
b=chear
c=tiger
d=elephant

echo $a $b $c $d

eval ssh cmbuild2@pittwls20 -C " echo aa=$a ; echo dd=$c ; export aa ; export dd ; a.ksh"

===============in a.ksh========================
for pvtfile in 1 2 3  
 do
print "=============== '$aa'====== '$dd'==========  "
done

---------------------------------------------------------------------------------------
OUTPUT: with debug mode ON
-------------------------------------------------------------------------------------
$>hello1.ksh
+ a=hello
+ b=chear
+ c=tiger
+ d=elephant
+ echo hello chear tiger elephant
hello chear tiger elephant
+ eval ssh cmbuild2@pittwls20 -C  echo aa=hello ; echo dd=tiger ; export aa ; export dd ; a.ksh
+ ssh cmbuild2@pittwls20 -C echo aa=hello
cmbuild2@pittwls20's password:
aa=hello
+ echo dd=tiger
dd=tiger
+ export aa
+ export dd
+ a.ksh
=============== ''====== ''==========
=============== ''====== ''==========
=============== ''====== ''==========
BUILD:cmbuild2@pitt30:/home/cmbuild2/
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20101712
Hi,

Why do you need single quot around $aa ?

 I have done the following and as you can see, it works!!

[omar@p4 omar]$ a=omar
[omar@p4 omar]$ eval ssh omar@0 "a=$a ; export a ; ./a.sh"
omar@0's password:
omar
omar.yyy

[omar@p4 omar]$ cat a.sh
echo $a
echo $a.yyy

If you still not able to get it done, we might think of some alternative way
0
 

Author Comment

by:parlepoint
ID: 20101913
i try without quote...but still not working..
is there problem with AIX unix machine..or ksh script...

still not taking variable...
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20102011
Hi,

try:

eval ssh cmbuild2@pittwls20 -C "echo aa=$a > /tmp/myfile ; echo dd=$c >> /tmp/myfile ; cat a.ksh >> /tmp/myfile ; mv /tmp/myfile a.ksh ; a.ksh"
0
 

Author Comment

by:parlepoint
ID: 20102293
it's not overwriting stuff...if i change a & c value..keep giving me old value..because old value still existing in script...

rest is fine...!!!
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20102331
Hi,

I don't understand. You could resolve the issue or not?

0
 

Author Comment

by:parlepoint
ID: 20102692
keep adding variable value in a.ksh script...& existing value are still there....
that' problem..varibal still picking up old values....
==========a.ksh===========================

aa=me
dd=you
aa=disco
dd=done

#! /usr/bin/ksh

cd /home/cmbuild2
echo $aa
echo $dd
for pvtfile in *.txt  
 do
print "=============== $aa====== $dd==========  "
done

for pvtfile in *.txt  
 do
print "my name is === $aa====== $dd==========  "
done
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 400 total points
ID: 20102951
Hi,

I got you.

Try

eval ssh cmbuild2@pittwls20 -C "echo aa=$a > /tmp/myfile ; echo dd=$c >> /tmp/myfile ; cat a.ksh >> /tmp/myfile ; mv a.ksh a.ksh.orig ; mv /tmp/myfile a.ksh ; a.ksh ; mv a.ksh.orig a.ksh"

Here I move a.ksh to a.ksh.orig then when I finish executing the new a.ksh, I move back a.ksh.orig to a.ksh

0
 

Author Comment

by:parlepoint
ID: 20103773
Thanks man...it works..
but i have some problem in same script.....

==============main script===========
ssh ssmqas@pitt34 <<EOT
cd /qual/ssm/stat/current/config
test1.ksh
EOT
================test1.ksh============
#!/usr/bin/ksh
cd /qual/ssm/stat/1.0/config/javaserver/com/fedex/common/security_framework

/qual/ssm/stat/current/config/decypherenv.pl -l:QAS card.props >card.props.decyphered

---------------instead of previous line i use for loop then getting error(see the output)-------------------
for  PVT_FILE in *.props
do
  $PVT_LOC_CONFIG/decypherenv.pl "-l:$SSM_DECIPHERCODE" "$PVT_FILE" >"$PVT_FILE.decyphered" ; PVT_RETURN1="$?"
done
=====================================
decyper.pl is only select file from txt file...if QAS than he selct qas file and project name...
output:

card.props
project=card
DECYPHER:QAS:encrypted_file=card_qas_encrypted_file
DECYPHER:QAS:unencrypted_file=card_qas_unencrypted_file
DECYPHER:PRD:encrypted_file=card_prd_encrypted_file
DECYPHER:PRD:unencrypted_file=card_prd_unencrypted_file
DECYPHER:DEV:encrypted_file=card_dev_encrypted_file
DECYPHER:DEV:unencrypted_file=card_dev_unencrypted_file

card.props.decypherd
project=card
DECYPHER:QAS:encrypted_file=card_qas_encrypted_file
DECYPHER:QAS:unencrypted_file=card_qas_unencrypted_file

AFTER FOR LOOP GIVING.-----------------
project=card

what will be problem....i couldn't figure out....can you know...
0
 

Author Comment

by:parlepoint
ID: 20104802
I managed it....
systex error....


Succesfully done.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question