Results 1 to 5 of 5
I am trying to execute 3 commands stacked together on a remote server. The purpose of the commands are to get the oldest path name, then the filename of the ...
- 08-19-2010 #1Just Joined!
- Join Date
- Aug 2010
- Posts
- 3
Running mulitple command through ssh not working
I am trying to execute 3 commands stacked together on a remote server. The purpose of the commands are to get the oldest path name, then the filename of the oldest file in that path, and then the date/time of that file.
The command I am running is:
stat -c %y $( find -L $( find -L /home/data -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1 ) | grep txt | sort -f | head -1 )
This command works fine if I execute it on the command line of the server. However, when I try to use it in a bash script and execute it from one server on the other server using ssh, the command does not work.
Example:
oldest_file=`ssh -l root $IP "stat -c %y $( find -L $( find -L /home/data -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1 ) | grep txt | sort -f | head -1 )"`
I don't know why the command will execute if I run it straight on the server but not through ssh. I broke it down to just the 2 Find commands and saw with verbose turned on that the path from the first (innermost) Find is not being passed to the second Find so it is not returning a filename which inevitably makes the Stat command fail.
Also, if there is an easier way to do what I am trying to accomplish without stacking these 3 commands like this, then I welcome those suggestions as well since I am a Linux newbie.
- 08-20-2010 #2Just Joined!
- Join Date
- Mar 2005
- Location
- Corona, CA
- Posts
- 29
It's your quoting. I'm not very good at proper quoting myself, but 'ssh server "$var"' will try to figure out the variable before it gets to the remote server. Maybe try "ssh server '$var' " so that the variables in your command get translated _after_ you ssh to the other server. Another thing to try would be to escape the variables ( \$var instead of $var ). That's the best guess I have.
- 08-20-2010 #3
I think its better to split this to 3 commands and redirect intremediate result into temp. file and then use it again. hints
find -L /home/laks -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1 > /tmp/getdir.txt
cd `cat /tmp/getdir.txt` && find -L . grep txt | sort -f | head -1 > /tmp/file.txt
stat -c %y `cat /tmp/file.txt`- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 08-20-2010 #4Just Joined!
- Join Date
- Aug 2010
- Posts
- 3
I actually had the IP in the ssh call instead of the variable and that didn't work either.
- 08-20-2010 #5Just Joined!
- Join Date
- Aug 2010
- Posts
- 3
I am trying to avoid having to make 3 ssh calls to get the data as I have over 100 servers I am going to have to connect to and run this command.


Reply With Quote