How do I make a script accept wildcards in arguments?
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do I make a script accept wildcards in arguments?
I have a script that I pass a username to, and it converts an avi file into a format that my PS3 can read. As of right now, I have to run ./avi2ps3 filename.avi
I was wondering how to get it to accept wildcards such as ./avi2ps3 file*.avi or ./avi2ps3 ./*
Also, if I run ./avi2ps3 filename.avi filename2.avi would that work?
Here is the one line script as it stands right now:
Before your script is invoked, all of the filenames are expanded into a list (separated with spaces by default). This list of filenames is accessible from within your script with the positional parameters $1, $2, $3, et cetera; OR in its entirety with $* .
The following example should demonstrate how this can be used in your script:
Code:
for f in $* ; do echo $f ; done
If you need to pass parameters to your script which are not filenames (switches, for example) then your script will need to process those properly (a task for which the BASH built-in 'shift' command is most helpful). I would recommend examining Slackware's package management scripts (e.g., /sbin/installpkg) for examples of handling more complicated situations.
If you have files with spaces in their names, you will get errors.
Alternative:
Code:
#!/bin/bash
ls $1|while read l_file
do
if [ -f "$l_file" ]
then
sudo ffmpeg -i `basename "$l_file" .avi`.avi -s 640x352 -vcodec libxvid -b 1037k -acodec libmp3lame -ab 96k `basename "$l_file" .avi`-new.avi && rm "$l_file" && mv `basename "$l_file" .avi`-new.avi "$l_file"
fi
done
Additionally, if you pass * without the .avi extension, you will pick up files that you probably don't want to process, therefore you might want to change the file test to:
if [ -f "`basename "$l_file" .avi`.avi" ]
Last edited by Disillusionist; 12-11-2008 at 01:44 PM.
Ahh, I was using basename to strip off the extension, I didn't realize it stripped off the path also...I'll look into adding dirname to it...that shouldn't be too hard
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.