Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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.

  2. #2
    Just 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.

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,740
    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`
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  4. #4
    Just 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.

  5. #5
    Just 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •