18.01.2020 Views

Working with Linux

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

If we run our script with the arguments a b c, we can see that our while looped through the parameters

and printed each one on a separate line. Now let's extend our CLI arguments parser and add a case

statement for interpreting the arguments.

Let's assume that our script will have a help option. The Posix standard recommends doing a long

argument version with --, and a short version with only one -. So both -h and --help will print the

help message. Also, it is recommended to always have a default case and print a message when the

user sends invalid options and then exits with a non-zero exit value:

while [[ ! -z ${1} ]]; do

case "$1" in

--help|-h)

echo "This is a help message"

shift

;;

*)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!