1

I am having an issue here trying to create a bash script for rclone

This give me an error:

20d: command not found


DAYS='20d'
PURGE='purge --min-age' ${DAYS}
rclone ${PURGE} ${DEST}/old/ $VERBOSE $LOGS

This works

PURGE='purge --min-age 20d'
rclone ${PURGE} ${DEST}/old/ $VERBOSE $LOGS

Can someone help me out here so I can use the first one? It makes it so much easier for others to use this script if all they have to do is change DAYS

jesse_b
  • 37,005

1 Answers1

2

Your quoting for the PURGE variable is causing this.

Currently you are setting:

PURGE='purge --min-age'

And then executing ${DAYS} (20d) with the PURGE variable in its environment.

You probably want:

purge=(purge --min-age "$days")

(Arrays are a much safer way to hold command line arguments than unquoted variables)

You would call it like:

rclone "${purge[@]}" "${dest}/old" "$verbose" "$logs"

Note: You should only use uppercase variable names for environment variables.


Take the following example:

$ FOO=bar env | grep FOO
FOO=bar
$ env | grep FOO
$

We are setting the FOO variable to bar and then executing the env command with this variable in its environment.

You are getting an error because 20d isn't a valid command.

jesse_b
  • 37,005
  • 1
    It's even more likely that they want purge=( purge --min-age "$days" ); rclone "${purge[@]}" "$dest" ... as to not have to rely on the field splitting of the shell to split the arguments in $purge correctly. – Kusalananda Jul 24 '19 at 19:25
  • ok, so if I use the format you recommend "${purge[@]}" how will that work with adding the rest of the info I want to the purge command? How does it get the 'purge --min-age 20d' part into the command? – prophetse7en Jul 24 '19 at 19:49
  • It gets that part from ${purge[@]}. Your request for clarification isn't clear – jesse_b Jul 24 '19 at 19:52
  • ok. The way I do it now based on the help I got days='20d' purge=(purge --min-age "$days") Then I use rclone $purge to rclone command and it give me this output purge --min-age 20d If I use ${purge[@]} will everything after = in purge=(purge --min-age "$days") be added were the @ is?

    So if I want to do the same in this example it will be like this? ignorefile=".rclone-ignore" exifpresent=('--exclude-if-present' ${ignorefile}) rcolne sync / "${dest}/recent" --backup-dir "${dest}/old/${date}" "${exitifpresent[@]}"

    – prophetse7en Jul 24 '19 at 20:25