[英]Writing a bash script that modifies gcc c compiler
I'd like to write my own bash compiler command in c. 我想在c中编写自己的bash编译器命令。 In fact, I like to use the gcc compiler in this bash script but just to modify a bit.
实际上,我喜欢在此bash脚本中使用gcc编译器,但只是进行了一些修改。
So, I'd like to have optional commands like -help -backup. 因此,我想拥有-help -backup之类的可选命令。 But also I want to have -o filename as mandatory input.
但我也想使用-o文件名作为强制输入。 How do I do that?
我怎么做? I want to read -o filename.
我想读取-o文件名。 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. 您不能使用
for
循环参数,因为其中一个参数需要一个值。 Use $1
and shift
. 使用
$1
和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 . 另外,如@etuardu所建议,您可以使用getopt 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.