【问题标题】:adding a -h (help) attribute to a custom script - bash将 -h(帮助)属性添加到自定义脚本 - bash
【发布时间】:2017-06-20 07:49:43
【问题描述】:

我想在调用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 的消息并仅显示我的帮助消息?

【问题讨论】:

标签: linux bash shell


【解决方案1】:

正如 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

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多