0

I am trying to check the existing directory on a remote server. I am using the below code to check that but somehow It doesn't seem working correctly. your help will be much appreciated.

sftp -o "IdentityFile=${sftpkey}" ${sftpaccount}@${sftphost} [:dir [$pocOutbound]] <<EOF >/dev/null 2>&1
cd "${pocOutbound}" 
bye
EOF

if [ $? -ne 0 ]
then   
logmsg "folder does not exist"   
exit 2
fi
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
  • Please prefix your code/data with four white spaces. – Cyrus Nov 11 '21 at 20:14
  • 1
    See: [Bash Shell sftp check if directory exists before creating](https://stackoverflow.com/q/51437924/3776858) – Cyrus Nov 11 '21 at 20:18
  • Cyrus, thank you for the prompt suggestion but still struggling to solve the issue. – user17389495 Nov 11 '21 at 21:44
  • Nic3500, not really I tried ssh command but no help. ssh -no "IdentityFile=${sftpKey}" ${sftpAcct}@${sftpHost} "ls -d ${pocOutbound}" >>${pocLogFile} 2>&1 – user17389495 Nov 11 '21 at 21:45
  • Thank you all, I was able to solve the issue using sftp -b - -o command. – user17389495 Nov 12 '21 at 03:01
  • See also [Why is testing “$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Nov 12 '21 at 10:27

0 Answers0