Computer scienceSystem administration and DevOpsCommand lineCommand line basics

Parameters and options

Building a command-line options handler script

Report a typo

Here is a command-line program in Bash with some gaps. The program can be invoked from terminal with fileName.sh [option] [arg]. When invoked with -h or --help option, the script displays a help message. In any other case – with another argument [arg] – the script prints the message You entered: [arg]. Fill in the blanks to complete the program.

Recall needed keywords:

  • if..then construction is used to perform execution of commands based on a condition

  • operator || means logical OR

  • echo prints the specified text or variables to the terminal

  • $0 refers to the name of the script

  • $1 refers to the first positional parameter passed to a script or a function.

Fill in the gaps with the relevant elements
#!/bin/bash

 [ "$1" == "-h" ]  [ "$1" == "--help" ]; 
   "Usage: `basename $0` [option] [arg]"
  echo "Options:"
  echo "-h, --help  Show help."
  exit 0
fi

echo "You entered: $1"
if||echothen

Create a free account to access the full topic