-1

I am working on a script to get list of files from remote servers, each remote server has its own password, IP and user and path. That's why I am using sshpass to pass the password parameter dynamically. Here is my script to just get list of files for time being:

user='username'
pass='password'  ->>> LOCATION_OF_REMOTE='hostpath'
ip='hostip'
path='hostpath'
data=`/usr/bin/sshpass -p $pass sftp -oBatchMode=no -b - -oStrictHostKeyChecking=no $user@$ip <<_EOF_
cd $path
ls
_EOF_`
echo $data >> list_of_files.txt

When I run this script I am getting error:

ERROR: sshpass: Failed to run command: No such file or directory

while exact same command works fine outside the script on command line. It just doesn't work when it's within the script. I tried to run this as root and non root use both. I don't know why it doesn't work from within the script.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • probably missing something from environment like path that is loaded from your user profile. See https://stackoverflow.com/questions/14612444/bash-script-runs-manually-but-fails-on-crontab and http://www.linuxfromscratch.org/blfs/view/6.3/postlfs/profile.html – BurnsBA Dec 10 '19 at 16:59
  • @BurnsBA not really helpful – Aroosh Mishra Dec 10 '19 at 17:29

1 Answers1

0

I have a super silly mistake. By defining a variable "PATH", shell is considering it environment path and that's why I was getting that stupid error.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Your question shows `path` (which is safe), not `PATH` (which is not). Please ensure, in the future, that the question shows code that in and of itself generates the exact problem you're experiencing. – Charles Duffy Dec 28 '19 at 18:29
  • (btw, this class of error is why [the POSIX specification advises](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html) that variable names with at least one lower-case character be reserved for application use and not modify system or shell behavior) – Charles Duffy Dec 28 '19 at 18:31