Linux 参数脚本

标签 linux bash shell unix

假设我有一个脚本文件“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 的建议使用 getoptgetopts

但是,恕我直言,这通常是矫枉过正的。如果只有一两个不带参数的标志,一个简单的 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/

相关文章:

unix - 差异文件存在于两个不同的目录中

linux - 从 mac-address snmp 知道接口(interface)

linux - 为什么我的 bashrc 脚本不起作用?

Python:回显到文件(如 Bash)

Bash 脚本 sed 使用导致 ZSH 出现问题

linux - 用于从父文件夹名称重命名多个文件的 Shell 脚本

linux - PG + extconf 失败,在 Linux 中退出代码 1

c - SIGALRM 实现

c++ - linux下计算周期数

Bash 脚本 : Gzip an entire folder and keep files extensions same