1

I have the file named as: /home/cti/10min_test/CTI_poult_20240902005000.csv

and wrote this shell script below:

CURRENT_TIME=$(date -d "+1 hour +50 minutes" +%Y%m%d%H%M)
CURRENT_TIME_TRIMMED=${CURRENT_TIME:0:11}
FILE_NAME_PART=${CURRENT_TIME_TRIMMED}
echo "FILE_NAME_PART: ${FILE_NAME_PART}"
echo "SOAP PROJECT: Starting SFTP…"
lftp -d -u myuser,mypass sftp://myip:port <<EOF
mput /home/cti/10min_test/CTI_poult_${FILE_NAME_PART}*.csv
bye
EOF
echo "done"

And when this is executed, only shows this message:

FILE_NAME_PART: 20240902011
SOAP PROJECT: Starting SFTP…
*.csv: no files found_test/CTI_poult_20240902011
done

It seems "*.csv" is not properly processed by the script. Anyone could tell me what the heck is wrong?

8
  • Are you sure this is the exact script? The script has/home/cti/10min_test/, but the error message has found_test/. Commented Sep 1, 2024 at 14:32
  • Looks like you have a carriage return in the filename CTI_poult_20240902011\r*.csv -- make sure the script file iteself does not contain \r\n line endings. The dos2unix command is helpful. Commented Sep 1, 2024 at 14:43
  • The date part in CTI_poult_20240902005000.csv doesn't match your date code (date -d "+1 hour +50 minutes" +%Y%m%d%H%M,) Either you need to add %S, or remove the ending "Seconds stamp" of 00 .... better debugging can be achieved with set -x (set +x to disable) AND export PS4='${LINENO} >' (export PS4="+" is the default value) . It will show each Line of executed code with all env-vars, cmd-substitutions, etc, expanded so you know exactly what command the shell tried to Run. All dbl-quoted strings are "normalized" to single-quoted strings. Good luck. Commented Sep 1, 2024 at 23:45
  • @glennjackman : I first also thought so, but wouldn't then the two echo be also messed up, the second one overwriting the first? But you are right, in that it would make sense to make a hex dump of the script and look for stray carriage returns. Commented Sep 2, 2024 at 6:36
  • You can also do a printf %s "$FILE_NAME_PART" | oc -cx and look for CRs or other control characters. Commented Sep 2, 2024 at 6:39

1 Answer 1

0

It seems "*.csv" is not properly processed by the script. Anyone could tell me what the heck is wrong?

Why "*.csv" is not properly processed by the script probably that the use of wildcards (*) inside the lftp command can not automatically expanded.

mput /home/cti/10min_test/CTI_poult_${FILE_NAME_PART}*.csv

What you can try to expand the wildcard in the shell before passing the files to lftp.

#!/bin/bash

CURRENT_TIME=$(date -d "+1 hour +50 minutes" +%Y%m%d%H%M)
CURRENT_TIME_TRIMMED=${CURRENT_TIME:0:11}
FILE_NAME_PART=${CURRENT_TIME_TRIMMED}

echo "FILE_NAME_PART: ${FILE_NAME_PART}"
echo "SOAP PROJECT: Starting SFTP…"

# Expand the file path before passing it to lftp
FILES_TO_UPLOAD=$(ls /home/cti/10min_test/CTI_poult_${FILE_NAME_PART}*.csv 2>/dev/null)

if [ -z "$FILES_TO_UPLOAD" ]; then
  echo "No files found for pattern: /home/cti/10min_test/CTI_poult_${FILE_NAME_PART}*.csv"
else
  lftp -d -u myuser,mypass sftp://myip:port <<EOF
  mput ${FILES_TO_UPLOAD}
  bye
EOF
fi

echo "done"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.