BASE_NAME="$(basename "${1}")"
FILE_NEW="$(echo "${BASE_NAME}" | sed -e "${REPLACE}")"
Here $1 refers to the file name that is passed to the function The REPLACE Variable:
REPLACE='s/\[NOW\]/'${NOW}'/g;s/\[TODAY\]/'${TODAY}'/g;s/\[DATETIME\]/'${DATETIME}'/g;s/\[RANDOM\]/'${RANDOM_NUMBER}'/g;s/\[NAME\]/'${NAME}'/g;s/\[FILENAME\]/'${FILENAME}'/g'
What I want to do is modify the file names with the sed operator by replacing the above patterns. But when I come across a file which contains spaces followed by "-" and them something that gives a problem. For an example take to look at the following file names:
file -2.txt
file -3.txt
file -e.txt
In all those cases the errors thrown are as follows:
sed: invalid option -- '2'
sed: invalid option -- '3'
sed: -e expression #1, char 1: unknown command: `.'
If the file name just contans spaces and no "-" still there are problems:
file name.txt
would give the following error:
sed: can't read ./file: No such file or directory
sed: can't read name.txt: No such file or directory
The FULL CODE TO THE SCRIPT: Full Script
sed
command you give here is ok, but the one in thereplace_file_content
function is not, and is probably where the error is coming from. The problem (there and a number of other places in the script) is failing to double-quote variable references. Use shellcheck.net to spot common mistakes like this. – Gordon Davisson Dec 17 '20 at 09:34