1

I have a remote script on a machine (B) which works perfectly when I run it from machine (B). I wanted to run the script via ssh from machine (A) using:

ssh usersm@${RHOST} './product/2018/requests/inbound/delDup.sh'

However, machine (A) complains about the contents of the remote script (2018req*.txt is a variable defined at the beginning of the script):

ls: cannot access 2018req*.txt: No such file or directory
Sophie
  • 23
  • 5
  • can you show the beginning of your script? And are you sure that those files are present on your remote machine (B)? – Allan Feb 05 '18 at 01:41
  • 1
    globbing doesn't transmit well through SSH. I suggest that you find the synonymous way to do what you need to do with `find` instead. – yftse Feb 05 '18 at 01:44

2 Answers2

2

From the information provided, it's hard to do more than guess. So here's a guess: when you run the script directly on machine B, do you run it from your home directory with ./product/2018/requests/inbound/delDup.sh, or do you cd into the product/2018/requests/inbound directory and run it with ./delDup.sh? If so, using 2018req*.txt will look in different places; basically, it looks in the directory that you were in when you ran the script. If you cded to the inbound directory locally, it'll look there, but running it remotely doesn't change to that directory, so 2018req*.txt will look for files in the home directory.

If that's the problem, I'd rewrite the script to cd to the appropriate directory, either by hard-coding the absolute path directly in the script, or by detecting what directory the script's in (see "https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within" and BashFAQ #28: "How do I determine the location of my script? I want to read some config files from the same place").

BTW, anytime you use cd in a script, you should test the exit status of the cd command to make sure it succeeded, because if it didn't the rest of the script will execute in the wrong place and may do unexpected and unpleasant things. You can use || to run an error handler if it fails, like this:

cd somedir || {
    echo "Cannot cd to somedir" >&2
    exit 1
}

If that's not the problem, please supply more info about the script and the situation it's running in (i.e. location of files). The best thing to do would be to create a Minimal, Complete, and Verifiable example that shows the problem. Basically, make a copy of the script, remove everything that isn't relevant to the problem, make sure it still exhibits the problem (otherwise you removed something that was relevant), and add that (and file locations) to the question.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
0

First of all when you use SSH, instead of directly sending the output (stdout and stderr) to the monitor, the remote machine/ssh server sends the data back to the machine from which you started the ssh connection. The ssh client running in your local machine will just display it (except if you redirect it of course).

Now, from the information you have provided, it looks like the files are not present on server (B) or not accessible (last but not least, are you sure your ls target the proper directory? ) you could display the current directory in your script before running the ls command for debugging purpose.

Allan
  • 12,117
  • 3
  • 27
  • 51