1

Here is my Bash script.

Assuming that : 1) go.sh is the script to execute 2) the user will launch the script this way

# ./go.sh -v file1.txt file2.txt

or

# ./go.sh --verbose file1.txt file2.txt

3) I would like to have a more elegant way to write this :

#!/bin/bash
Verbose_Mode=0

until [ "$#" == "0" ]; do
   case $1 in
     "-v" or "--verbose")
      Verbose_Mode=1
      ;;

This will result in a syntax error because of :"-v" or "--verbose".

My question : How can I properly write (if possible) the code :

#!/bin/bash
Verbose_Mode=0

until [ "$#" == "0" ]; do
   case $1 in
   "-v")
   Verbose_Mode=1
   ;;
   --verbose")
   Verbose_Mode=1
   ;;
   esac

Thanks in advance for answers.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Solid.Snake
  • 33
  • 1
  • 7
  • Refer to [How to write a case statement in Bash](http://stackoverflow.com/questions/16673570/bash-case-statement) for correct syntax of Bash case statements. – codeforester Feb 23 '17 at 05:15
  • Are you allowing the user to use the switch anywhere on the line or only as the first parameter? If only first you could simply test it, shift and move on. – grail Feb 23 '17 at 05:44
  • See also [BashFAQ/35](http://mywiki.wooledge.org/BashFAQ/035) and [ComplexOptionParsing](http://mywiki.wooledge.org/ComplexOptionParsing). – Benjamin W. Feb 23 '17 at 05:56
  • Thanks for answers. @grail there are others "case" following in the script – Solid.Snake Feb 23 '17 at 05:59

1 Answers1

2

Thanks to this link : How do I parse command line arguments in Bash?

Here is the answer to my first post :

 case $1 in
     -v|--verbose) 
      Verbose_Mode=1

Thanks

Community
  • 1
  • 1
Solid.Snake
  • 33
  • 1
  • 7