0

I would like to display a message when calling custom_script -h currently my simple script is using sed stream editor and when trying something like :

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

Then I'm getting sed's message first and only then my help message

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]**

How can I omit/hide sed's message and display only mine help message ?

  • 4
    Why do you not simply put the `sed` after your `if` block? – Fred Feb 03 '17 at 02:39
  • check these SO posts [Best way to parse cmdline args - BASH?](http://stackoverflow.com/questions/14786984/best-way-to-parse-cmdline-args-bash) and [How do I parse command line arguments in Bash?](http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) – bansi Feb 03 '17 at 02:52

1 Answers1

1

As Fred suggested you'd better put your sed command inside IF in your case to display a simple help message you may do:

#!/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
Zaza
  • 458
  • 2
  • 7
  • 15