1

I need to create a path with a variable value which contains yesterday date. I need to get files using SFTP. Here is my try.

#/bin/bash

TS=$(date -d '30 mins ago' +"%Y%m%d")

sshpass -p '12#$' sftp -P 2222 User_New@10.18.16.14 <<'END_SFTP' get /newNE/PSData$TS* /data/processedfiles END_SFTP

But TS date value is not passing to remote server to get data.

File "/newNE/PSData$TS*" not found.

Can someone help me to solve this?

edublog
  • 83

1 Answers1

1

For parameter expansion (and command substitution and arithmetic expansion and line continuation handling) to be performed inside a here document, no part of the delimiter must be quoted, so:

#! /bin/sh -

ts=$(date -d '30 mins ago' +"%Y%m%d")

SSHPASS='12#$' sshpass -e sftp -P 2222 User_New@10.18.16.14 <<END_SFTP get /newNE/PSData$ts* /data/processedfiles END_SFTP

Also note:

  • passwords should not be passed on the command line which is publicly available in the output of ps or sometimes in some accounting logs. Passing them as environment variables is generally fine as that's generally considered private. Even better would be to use public key authentication. You'll want to make sure read access to the script is restricted as well as it contains that password.
  • shebangs start with #! followed by the path to the interpreter. Adding a - is also good practice. With just #/bin/bash, that would be only a comment, so in the absence of a shebang, your system would likely run sh to interpret the script.
  • Here however, there's nothing bash-specific in that script, so you might as well use sh and remove that dependency to bash (and likely use fewer resources when running the script unless you're on a system where /bin/sh is bash).
  • Here, you do have a dependency on GNU date (for that non-standard -d option) which is even less portable than bash (bash is the GNU shell but is also commonly found on non-GNU systems). If using bash, you could remove that dependency by defining ts as printf -v ts '%(%Y%m%d)T' "$(( EPOCHSECONDS - 30 * 60 ))" (after printf -v EPOCHSECONDS '%(%s)T' -1 with older versions of bash).