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

Bo Berglund bo.berglund at gmail.com
Thu Aug 26 12:12:08 UTC 2021


On Thu, 26 Aug 2021 20:10:19 +1000, Karl Auer <kauer at biplane.com.au> wrote:

>   #!/bin/sh
>   SKIP=$1
>   shift
>   shift $SKIP
>   rm $*
>
>Call it like this:
>
>   ./myscript 5 202*.mp4


>The above has a limitation - the number of filenames that will fit into
>the maximum length of a command line (around 2 million characters). But
>unless you have many thousands of files in your directories, or your
>files have extremely long names, you are unlikely to hit that limit in
>practice.

Thanks!

I have a server side system that continuously downloads a youtube live stream in
1-hour chunks for later viewing via Kodi.
This results in 18 mp4 files each day (6 hours are not containing anything).
In order to preserve disk space I have to also automate the removal of these
files after some time, so this is what the prunevideos script is for and it uses
the prunelist script to extract the exact files to delete.

I did not at all know about the mechanism in the shell you describe so I made an
experiment:

#!/bin/bash
SKIP=$1
shift
shift $SKIP
echo $*

And called it like below with these 5 mp4 files in the directory:

2021-08-25_06.mp4
2021-08-25_12.mp4
2021-08-25_18.mp4
2021-08-26_00.mp4
2021-08-26_06.mp4


$ ./testscript.sh 3 *.mp4
2021-08-26_00.mp4 2021-08-26_06.mp4

It should list the files to *remove* in order to keep the last 3 (based on name
sorting), but it actually lists the two files among the ones I need to keep...

So the response should have been:
2021-08-25_06.mp4 2021-08-25_12.mp4

This is why my script prunelist is so elaborate because it lists the *older*
files to remove.


-- 
Bo Berglund
Developer in Sweden





More information about the ubuntu-users mailing list