1

I need to run the below script test.sh file to run in remote system.

variables.txt:

name=stackexchange

test.sh:

#!/bin/bash
source ./variables.txt
echo $name

Running:

ssh -i "auth.key" user@host 'bash -s' < test.sh

But I am getting this error:

bash: line 2: ./variables.txt: No such file or directory
1
  • 2
    The error message indicates that the file variables.txt does not exist on the remote system. You have to copy it there before running the script that refers to it. – dhag Feb 14 '17 at 14:43
1

You're telling the shell on the other machine to source the ./variables file, which is not present in the pwd of that shell. Step by step, it looks like this:

  1. This command connects via ssh and sends the lines from the local test.sh

    ssh -i "auth.key" user@host 'bash -s' < test.sh
    
  2. The remote shells gets started by the ssh connection, and then gets the following lines:

    • #!/bin/bash The interpretation of this line by the remote shell depends on what shell that is. Most shells will treat this line as a comment. If the shell is sh, the remote shell will not become bash, because the shebang line is only valid in script files, and the scripts need the executable bit set.
    • source ./variables.txt Now the remote shell is told to source from the file variables.txt in the current working directory. If the file is not there, the shell will throw an error. In your case the shell is Bash, and the error is:

      bash: line 2: ./variables.txt: No such file or directory
      
    • echo $name Because $name is empty, there's only an empty line printed in the terminal.

To confirm this yourself, place a file variables.txt in the remote home directory with a different line to source, say:

name=remotehost

And run your test script like before.

1

Programs running on the remote machine see the files on the remote machine, not the files on the machine where you ran ssh from. You're telling SSH to copy the content of test.sh to the remote machine and pass it as input to bash there, so you have no problem running test.sh. But when the script (which bash receives on standard input) tries to read variables.txt, it looks for it on the machine that it's executing on (it's not as if it had a choice), and doesn't find it.

If your script isn't too complex and there aren't nested inclusions then you could do a text replacement to inline the content of the script. Note that this is fragile — it only works if source ./variables.txt appears directly in the script, not if it involves complex quoting, variables, eval, etc. For simplicity I'll assume that the source command is alone on its line. More complex transformations would be possible to cope with more complex scenarios.

<test.sh sed '/^[ \t]*source variables\.txt[ \t]*$/ {
  d
  r variables.txt
}' | ssh -i "auth.key" user@host 'bash -s'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.