简体   繁体   中英

Writing a bash script that modifies gcc c compiler

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM