how to use ssh ?

Posted on 2008-11-04
Last Modified: 2013-11-17
I wrote a shell script "A" which will invoke any shell script "B".

I have both script "A" and "B" on /tmp/test directory of the machine M2

On machine M1,

I do a

ssh M2 "cd /tmp; ls -l"
I see A and B from the list output

However, if I

ssh M2 "cd /tmp; A"
I got a message
/tmp/A[5]:B: not found

It seem that I was successfully cd to /tmp on M2 and executing A on /tmp but it doesn't find B.

Why is that? What should I do to execute A on the directory that I cd to on the remote host?

Question by:xewoox
    LVL 84

    Assisted Solution

    is B in $PATH on  M2?
    LVL 9

    Assisted Solution

    try to put the full path of B in A and check
    LVL 48

    Assisted Solution

    In script A, put




    when you call the second script.
    LVL 68

    Assisted Solution

    ssh M2 "export PATH=/tmp ; A"

    LVL 68

    Accepted Solution

    Hi again,
    now that it's morning again in old Europe, here some explanations -
    1. Your test above does only work because you have the dot ('.' = current directory) in your standard path, probably set in /etc/environment (AIX) or /etc/profile.This is not recommended, as some attacker could place a faked executable (e.g. 'ls') into a directory which is known to be often used as the current one, e.g. your homedir, and do some harmful things with it. If you need the dot in your path, at least take care to place it there as the last element.
    2. The 'cd /tmp' in your example does work because the subshell for script 'A' is started from the same shell as the 'cd' (see the 'dot' thing above). This shell, however, does not inherit this environment and thus can't find 'B', as its current directory is by default  your home directory.
    3. My solution works because we set the environment variable 'PATH' there which is recognized by script 'A'  and so, as the shell evaluates PATH to find executables, 'A' can find and execute 'B'
    Many words, but I hope I could make things a bit clearer.

    Author Comment

    Thank you

    Author Comment

    Thank you

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
    Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now