Link to home
Start Free TrialLog in
Avatar of cp5c0tt
cp5c0tt

asked on

Having problems with IFS Variable using SSH

I have a perl script that needs to pass a string of code to a remote system via ssh.  The code works perfectly while ssh'd into the command shell of the remote system, but when it is passed to the remote system via ssh, I am unable to manipulate the IFS Variable to and the code fails.  I have tried sourcing ~./bashrc at the beginning of the command string, but to no avail.  Any help would be greatly appreciated. Probably something simple with the enviornment, but it's driving me crazy.

Linux localsystem 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:19 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux
Linux remotesystem 2.6.18-92.el5 #1 SMP Tue Apr 29 13:16:15 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux
(keys have properly installed between systems)
(This Works)
ssh remotesystem
 echo -e "begin\n\n\n";  cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then for i in ${FILES[*]}; do echo -e "i:$i";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi


--------------------------------------Desired Output--------------------------------------------------------
/home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec
0000000  \n
0000001
NUMBR:2
FILES
this
this is also a file
1.)this
2.)this is also a file
-------------
i:this
d41d8cd98f00b204e9800998ecf8427e,"this"
i:this is also a file
6607e4132c9a84526d2a11b27979ec0c,"this is also a file"

(This Fails)
 ssh remotesystem 'source ~./bashrc; echo -e "begin\n\n\n";  cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then for i in ${FILES[*]}; do echo -e "i:$i";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'

----------------------------------FAILED OUTPUT------------------------------------------------------------
begin



/home/cscott/jboss-eap-4.2.CP07/jboss-as/server/firstrade/log_sec
0000000  \n
0000001
NUMBR:1
FILES
this
this is also a file
1.)this
this is also a file
2.)
-------------
i:this
this is also a file
,""
bash: ~./bashrc: No such file or directory
/usr/bin/md5sum: this
this is also a file: No such file or directory

Avatar of arober11
arober11
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi

Your:

source ~./bashrc;  

should probably read:

source ~/.bashrc;  

Anyway I don't believe that's your problem, as I dought your file loop:

for i in ${FILES[*]}; do echo -e "i:$i";

Will return the individual files, stick an extra colon on the end or you echo to prove this e.g.

echo -e "i:$i:";
Avatar of cp5c0tt
cp5c0tt

ASKER

Yes, I fixed the ~/.bashrc, hazards of cut and paste ;-)
Here is the applicable output from your recommeded change -

then for i in ${FILES[*]}; do echo -e "i:$i:";

---- OUTPUT -----

i:this
this is also a file:

---- END ----

As  I mentioned earlier, this code works perfectly when I ssh into a shell on the remote machine, it is only when I pass the code to the remote machine via an ssh command.  For some reason (I suspect Security) the IFS Variable can not be manipulated.  

Thank you for your input arobert11!
Nope, you can play happily play around with the IFS variable in a remote shell e.g.

ssh localhost 'echo -e "begin\n\n\n";  cd $HOME/boo; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then IFS=':';for i in `echo "${FILES[*]}"|/usr/bin/tr "\n" ":" `; do echo -e "i:$i:";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'

It's the escaping in your example that's the issue.
With your host / file names:

ssh remotesystem 'echo -e "begin\n\n\n";  cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then IFS=':';for i in `echo "${FILES[*]}"|/usr/bin/tr "\n" ":" `; do echo -e "i:$i";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'
Appears I have the same cut-n-paste issues, as your good self:

ssh remotesystem 'echo -e "begin\n\n\n";  cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then IFS=:;for i in `echo "${FILES[*]}"|/usr/bin/tr "\n" ":" `; do echo -e "i:$i";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'
ASKER CERTIFIED SOLUTION
Avatar of arober11
arober11
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cp5c0tt

ASKER

arobert11 you rock.  Thanks!  I believe my issue stemmed from the (incorrect) thought that the find was storing the results into $FILES (at least that is what I was trying to do) as an array based on the -printf "$f\n" statement.  

In anycase modifying the -printf to use ":" instead of "\n", allowed me to process the list using the IFS as a colon.  Awesome, Thanks again!
Avatar of cp5c0tt

ASKER

BTW Final code snippet: (If anyone cares :-P )
 ssh remotesystem 'cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; IFS=$:; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f:"  ) ); if [[ -n $FILES ]]; then for i in ${FILES[*]}; do  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'

--------- Desired Output ----------------------------------
d41d8cd98f00b204e9800998ecf8427e,"this"
6607e4132c9a84526d2a11b27979ec0c,"this is also a file"
--------- End of Output -----------------------------------