Spandan Brahmbhatt
Spandan Brahmbhatt

Reputation: 4044

Execute Script available on remote machine

I am trying to execute a script available on remote machine using ssh. The output differ if it is run from client via ssh or run after ssh into the server.

The script will trim a file.

tail -n 100 users.txt > temp.txt
rm users.txt
mv temp.txt users.txt
echo $(wc -l users.txt)
echo Done

On running from client side :

client@client_mac $ ssh user@server_mac '~/path_to_script/demo_script.sh'

Output :

0 users.txt
Done

while after ssh'ing at server side :

client@client_mac $ ssh user@server_mac
user@server_mac $ cd ~/path_to_script/
user@server_mac $ ./demo_script.sh

Output :

100 users.txt
Done

How do we execute a script that is available on remote machine ? Is the syntax different ?

Upvotes: 0

Views: 113

Answers (1)

chepner
chepner

Reputation: 530813

Your script always looks for users.txt in the current working directory.

In the first example, the current working directory is your home directory; that's why you have to run the script with ~/path_to_script/demo_script.sh rather than ./demo_script.sh. As such, you are getting the line count of ~/users.txt in your output.

In the second example, you change the working directory from ~ to ~/path_to_script before executing the script, so the output contains the line count of ~/path_to_script/users.txt.

Upvotes: 1

Related Questions