linux - 将 -h(帮助)属性添加到自定义脚本 - bash

标签 linux bash shell

我想在调用 custom_script -h 时显示一条消息 目前我的简单脚本正在使用 sed 流编辑器 当尝试类似的东西时:

sed -i "myscript_here"
if [ "$1" == "-h" ]; then
  echo "Usage: `custom_script $0` [Help_message_here]"
  exit 0
fi

然后我首先收到 sed 的消息,然后才收到我的帮助消息

sed: invalid option -- 'h'
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern...................

............................
''''''''''''''''''''''''''''
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
sed: no input files
/usr/local/bin/comment.sh: line 5: custom_script: command not found
**Usage:  [Help_message_here]**

如何省略/隐藏 sed 的消息并只显示我的帮助消息?

最佳答案

正如 Fred 建议的那样,您最好将 sed 命令放在 IF 中 在您的情况下,您可以显示一条简单的帮助消息:

#!/bin/sh
if [[ "$1" == "-h" || "$1" == "--h" || "$1" == "-help" || "$1" == "--help" ]]; then
  echo "Usage: [Help_message_here]"
else
  sed -i your_script_here
exit 0
fi

关于linux - 将 -h(帮助)属性添加到自定义脚本 - bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42015799/

相关文章:

linux - 从 ifconfig 输出中提取 MAC 地址

bash - 用于导入的 sqlite 字段分隔符

shell - 变量的变量

linux - 为什么我无法使用此 linux shell 脚本检测文件是否存在?

python - 绑定(bind)回调以最小化和最大化 Toplevel 窗口中的事件

linux - SSH 进入 AWS EC2 实例给出权限被拒绝(公钥)

android - 我可以将带有 HTML/JavaScript UI 的 Go Web 服务器作为跨平台应用程序运行吗(Linux、Android、iOS、macOS、Windows)

bash - hadoop中的批量重命名

linux - 使用 7zip 解压文件后如何重命名这些文件并保存

linux - 使用 sed 替换与导出变量匹配的模式