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

时间:2017-02-03 02:33:37

标签: 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的消息并仅显示我的帮助消息?

1 个答案:

答案 0 :(得分:0)

正如弗雷德建议你最好把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