LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 12-11-2008, 01:13 PM   #1
CartersAdvocate
Member
 
Registered: Sep 2003
Location: Columbus, OH
Distribution: Slackware 12.2
Posts: 166

Rep: Reputation: 30
Question 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:

Code:
sudo ffmpeg -i `basename "$1" .avi`.avi -s 640x352 -vcodec libxvid -b 1037k -acodec libmp3lame -ab 96k `basename "$1" .avi`-new.avi && rm "$1" && mv `basename "$1" .avi`-new.avi "$1"
Feel free to suggest any other improvements to it as well.

Thanks,
Jeff
 
Old 12-11-2008, 01:24 PM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Well, you could do something like:

Code:
for i in "$1"
do
    ffmpeg -i `basename "$i" .avi`.avi -s 640x352 -vcodec libxvid -b 1037k -acodec libmp3lame -ab 96k `basename "$i" .avi`-new.avi && rm "$i" && mv `basename "$i" .avi`-new.avi "$i"
done
 
Old 12-11-2008, 01:27 PM   #3
CartersAdvocate
Member
 
Registered: Sep 2003
Location: Columbus, OH
Distribution: Slackware 12.2
Posts: 166

Original Poster
Rep: Reputation: 30
That would work for both all of my examples?

./avi2ps3 file*.avi
./avi2ps3 ./*
./avi2ps3 filename1.avi filename2.avi
 
Old 12-11-2008, 01:32 PM   #4
saulgoode
Member
 
Registered: May 2007
Distribution: Slackware
Posts: 288

Rep: Reputation: 155Reputation: 155
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.
 
Old 12-11-2008, 01:34 PM   #5
CartersAdvocate
Member
 
Registered: Sep 2003
Location: Columbus, OH
Distribution: Slackware 12.2
Posts: 166

Original Poster
Rep: Reputation: 30
I only need to pass filenames, so that should work. Thanks for the help
 
Old 12-11-2008, 01:36 PM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by j3ff3r View Post
That would work for both all of my examples?

./avi2ps3 file*.avi
./avi2ps3 ./*
./avi2ps3 filename1.avi filename2.avi
It would only handle the first two, I think. But the third one is not much use, because you can just do:

Code:
./avi2ps3 filename*.avi
 
Old 12-11-2008, 01:38 PM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
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.
 
Old 12-11-2008, 01:47 PM   #8
CartersAdvocate
Member
 
Registered: Sep 2003
Location: Columbus, OH
Distribution: Slackware 12.2
Posts: 166

Original Poster
Rep: Reputation: 30
Edit: Deleted after reading other responses...ignore this one

Last edited by CartersAdvocate; 12-11-2008 at 02:02 PM.
 
Old 12-11-2008, 02:01 PM   #9
CartersAdvocate
Member
 
Registered: Sep 2003
Location: Columbus, OH
Distribution: Slackware 12.2
Posts: 166

Original Poster
Rep: Reputation: 30
Ok, how's this one?

Code:
#!/bin/bash
ls $*|while read l_file
do
   if [ -f "`basename "$l_file" .avi`.avi" ]
   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
 
Old 12-11-2008, 02:31 PM   #10
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
That should work for the specified examples, however it should be noted that:
  1. If you do not specify a parameter, it will process all files in the current working directory.
  2. It will not work when specifying files in a different directory.

You may want to have a look at dirname to enhance the script further:

if [ -f "`dirname "$l_file"`/`basename "$l_file" .avi`.avi" ]
 
Old 12-11-2008, 02:39 PM   #11
CartersAdvocate
Member
 
Registered: Sep 2003
Location: Columbus, OH
Distribution: Slackware 12.2
Posts: 166

Original Poster
Rep: Reputation: 30
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
 
Old 12-11-2008, 02:40 PM   #12
CartersAdvocate
Member
 
Registered: Sep 2003
Location: Columbus, OH
Distribution: Slackware 12.2
Posts: 166

Original Poster
Rep: Reputation: 30
Oh, and if I just add an

Code:
if $*
clause to it, that would fix the problem of not specifying a paramater, right? (sorry, I'm a bash scripting newb)
 
Old 12-11-2008, 02:44 PM   #13
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
To make life easier just run dirname and basename once:

Code:
#!/bin/bash
ls $*|while read l_file
do
   l_basefile="`dirname "$l_file"`/`basename "$l_file" .avi`"

   if [ -f "${l_basefile}.avi" ]
   then
      sudo ffmpeg -i "${l_basefile}.avi" -s 640x352 -vcodec libxvid -b 1037k -acodec libmp3lame -ab 96k "${l_basefile}-new.avi" && rm "${l_basefile}" && mv "${l_basefile}-new.avi" "${l_basefile}"
   fi
done
 
Old 12-11-2008, 02:46 PM   #14
CartersAdvocate
Member
 
Registered: Sep 2003
Location: Columbus, OH
Distribution: Slackware 12.2
Posts: 166

Original Poster
Rep: Reputation: 30
Cool, thanks
 
Old 12-11-2008, 02:46 PM   #15
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by j3ff3r View Post
Oh, and if I just add an

Code:
if $*
clause to it, that would fix the problem of not specifying a paramater, right? (sorry, I'm a bash scripting newb)
You could test the number of parameters passed:

Code:
if [ $# -eq 0 ]
then
   echo "You didn't enter a parameter!"
   exit 1
fi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with shell script and wildcards BlueNoteMKVI Programming 3 10-04-2006 07:18 AM
make arguments johnrage SUSE / openSUSE 2 11-29-2005 08:10 AM
Script arguments jnusa Programming 1 12-20-2004 01:38 AM
Function accept() and its arguments. krajzega Programming 1 01-20-2004 01:26 AM
Perl: Getting a script to accept arguments JStew Programming 10 03-10-2003 06:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 09:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration