Reputation: 421
I'd like to write my own bash compiler command in c. In fact, I like to use the gcc compiler in this bash script but just to modify a bit.
So, I'd like to have optional commands like -help -backup. But also I want to have -o filename as mandatory input. How do I do that? I want to read -o filename. But the problem seems to be with my understanding of optional and mandatory parameters. How do I differentiate between those two? Here is the code I wrote till now (Thanks a lot for taking a look):
#!/bin/bash
for i in $@
do
case $i in
-help)
echo "This is how you use this command."
;;
-backup)
cp ./* ./backup
;;
*)
echo "Usage is this"
exit
;;
esac
done
Upvotes: 0
Views: 517
Reputation: 96266
You cannot loop on the parameters with for
as one of your arguments expects a value. Use $1
and shift
.
For mandatory parameters set a default (eg: empty string) for a the mandatory variable, if it's not set after the parameter parsing you know it's missing.
Also, as @etuardu suggested you can use getopt.
Upvotes: 1