How to pass * as part of argument string to script?

Bo Berglund bo.berglund at gmail.com
Thu Aug 26 07:23:41 UTC 2021


On Wed, 25 Aug 2021 19:43:06 +1000, Karl Auer <kauer at biplane.com.au> wrote:

>> Do you mean that there is no way I can transfer a * character into a
>> script as part of an argument? 
>
>I told you how. Did you not see my message? Quote the argument on the
>command line.

OK, so I need to quote the argument if it contains a potentially expandable
character...
Not ideal but it does work.

However, now I have run into another problem when using this script:

The scrript to list the excess files in a directory now works OK when I execute
it on the command line like this:

$ prunelist 4 "2021-08-2*.mp4"
2021-08-23_00.mp4
2021-08-23_12.mp4
2021-08-23_18.mp4
2021-08-24_00.mp4
2021-08-24_06.mp4
2021-08-24_12.mp4
2021-08-24_18.mp4
2021-08-25_00.mp4

But when I try to use it from another script as follows it does not work:

#!/bin/bash
cnt=$1
template=$2
videofilescmd="prunelist $cnt \"$template\""
echo "Listcmd = $videofilescmd" #This shows a valid command!
videofiles=$($videofilescmd) #This does not assign the output to videofiles
echo "Videos to remove:"
echo "$videofiles" #Always empty!
for videofile in $videofiles
do
  command="rm $videofile"
  if [ $debug == 1  ]; then
    echo "$command"
  else
    eval $command
  fi
done

This is what I get (note use of quotes):

$ prunevideos 4 "2021-08-2*.mp4"
Listcmd = prunelist 4 "2021-08-2*.mp4"
Videos to remove:


So following the echo of Listcmd = ... nothing is actually happening.
But if I copy the string output from the echo and run it it produces the output
I need.

So the command actually computed is correct with quotes and all but when using
it to assign the variable videofiles the output list of files it fails...

I have spent hours trying various combinations of backticks and other tricks to
actually execute a command contained in a variable and assign its output to
another variable but failed at every step.

What am I missing here?



-- 
Bo Berglund
Developer in Sweden





More information about the ubuntu-users mailing list