假设我有一个脚本文件“display.sh”,它显示一些输出。
Name -Xander
Age-29 // Step 1
我想在此输出中添加一些额外的内容。
Name -Xander
Age-29
occupation-Cricketer //extra stuff
现在,当我发出命令 display.sh 时,只应显示步骤 1 但是当我发出命令(display.sh -t 或 -f 或其他任何内容)时,步骤 1 和额外的内容都应该显示
最后一部分如何实现?
最佳答案
如果您想要完整的解决方案,请按照 Aserre 的建议使用 getopt
或 getopts
。
但是,恕我直言,这通常是矫枉过正的。如果只有一两个不带参数的标志,一个简单的 if
-then
可能就足够了。
您将需要研究如何在 bash 中使用命令行参数
举个例子来帮助您:
#!/bin/bash
if [ "$1" = "-f" ] ; then
do_the_rest=1
else
do_the_rest=0
fi
# lots of code for your display script
if [ $do_the_rest = 1 ] ; then
# code to display the rest of the lines
fi
关于Linux 参数脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42536295/