编写修改gcc c编译器的bash脚本
我想用 c 编写自己的 bash 编译器命令。事实上,我喜欢在这个 bash 脚本中使用 gcc 编译器,但只是稍微修改一下。
因此,我想要一些可选命令,例如 -help -backup。但我也想将 -o 文件名作为强制输入。我该怎么做?我想读取-o 文件名。但问题似乎出在我对可选参数和强制参数的理解上。我如何区分这两者?这是我到目前为止编写的代码(非常感谢您的阅读):
#!/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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用
for
循环参数,因为您的参数之一需要一个值。使用$1
和shift
。对于强制参数,为强制变量设置默认值(例如:空字符串),如果在参数解析后未设置它,您就知道它丢失了。
另外,正如 @etuardu 建议的那样,您可以使用 getopt。
You cannot loop on the parameters with
for
as one of your arguments expects a value. Use$1
andshift
.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.