Ever want to test command line arguments in bash, mixing arguments and execution options? I have. Here’s one way to do it:
#!/bin/sh
filelist=
until [ -z "$1" ]; do
# use a case statement to test vars. we always test
# test $1 and shift at the end of the for block.
case $1 in
--home|-h )
# shift, so the string after --home becomes
# our new $1. then save the value.
shift
USERHOME=$1
;;
--force|-f )
# set to 1 for later testing
FORCE=1
;;
-- )
# set all the following arguments as files
shift
filelist="$filelist $@"
break
;;
-* )
echo "Unrecognized option: $1"
exit 1
;;
* )
filelist="$filelist $1"
;;
esac
shift
if [ "$#" = "0" ]; then
break
fi
done
echo -n "Files:"
for f in $filelist ; do
echo -e "\t$f"
done
echo "User home:" $USERHOME
echo "Forcing?" $FORCE
Here’s some sample output:
adam@aziz:~/prog/argstest$ sh argstest.sh foo1 --home /home/adam foo2 -- --force foo3
Files: foo1
foo2
--force
foo3
User home: /home/adam
Not forcing.
As you can see, it works with long and short options, options that take values, and the “terminate option list” operator. All it needs is a man page.
I thought you dident use shell scrips. d: You always tease me when I use them. (/me is just being a bitch, ignore him.)
It’s a valid question.
I use scripts when they’re appropriate for the task at hand. I’ve seen people script things when the work was already done for them in another program, which is silly.
I thought you were on vacation.
I never said that I was on vacation from reading other peoples stuff. d:
Damn I am getting good at doing the garbaldy-gook email address, I don’t even have to think about adding the .com on the end anymore. (:
lskfjsdlfj@sfldjf.com
sflfgfdslj@ldfjow.com
slfweor@sldfj.com
Wooo
Interesting… unfortunately, our Alpha’s here at work don’t have bash (oh csh, why do you hate me. I’ll have to wait until I’m home to try it).
God, our work machines don’t have anything: less, emacs, sudo, perl (well, they have perl now that I’ve bitched about it). On the plus side… with vi being the only editor, I no longer had any excuse to put off learning it. I’m at the point where I’m very comfortable with it now (after doing lots of perl and FORTRAN editing with it, I’d better be).
Still, our work computer were misconfigured improperly. For example, when vi starts up, .exrc isn’t read (gotta load it up manually). Heck even .cshrc doesn’t work.
Even worse, my department isn’t unix friendly. They’ve finally found it in their budget to scrap our Alphas and replace them with Windows 2000 servers for our development. Why? ’cause Visual Basic is the coolest thing since sliced bread.
Is it winter yet?
(p.s. I totally didn’t mean to hijack this thread)
Oh, which reminds me. Over the weekend, one of my friends and I were talking about Unix programming (He’s signed up for a unix course at the local community college). He was asking me about kernels and shells and Emily starts cracking up. I asked her what was so funny and she said:
Colonel? Shell? You guys are just doing this to pull my leg. What kind of shell is it? a Sea Shell?
I replied: Yeah, Colonel Panic designed the c shell, but it wasn’t cool enough so General Fault made the turbo c shell.
We started cracking up, but the joke was completely lost on her… *sigh* film majors. She just figured out yesterday how to connect her TiBook to her dialup connection. She’s had the laptop for almost a year now.
Lol, thats fucking great (the c/sea shell stuff)
My script doesn’t work 100%. Arguments with spaces break. They’re read in as single arguments, but not stored as single arguments. Don’t know how to do that just yet.
Did you edit the script, or am I just going crazy?
Yes, I did update the script. I wish I had some revision control, a la Mark Pilgrim, but no. I saved the old version for posterity, but I can’t do much but gaze lovingly in its direction for the time being.
Do you have any idea how Mark did his revision history?
What I’d like to see personally, is something like Apple’s GUI for diff. It’s really spiffy.
Actually, I do:
http://diveintomark.org/archives/2003/07/18/dive_into_accountability
You know what. I just did a search (for revisioning) and that page came up. I saw the George Orwell quote and got sidetracked. I started googleing for more orwell quotes and completely forgot about doing revisioning.
Don’t you just love having all this useless info at our fingertips?
Nice tip. Thanks.
My script doesn’t work 100%. Arguments with spaces break. They’re read in as single arguments, but not stored as single arguments.
Don’t sweat it; there are a number of implementation-specific ways to decide how to handle this. gcc does it by having options with spaces instead use commas (e.g., -Wl,-rpath,/path/to/embed ). Your post was very time-saving for me, thanks for the help.
and yet 5 years later, it did still help me !
thanks