Parse Command Line Argument Using SHELL Script

SHELL Script Example: parse command line input using shell script

SHELL Script Example: how to parse command line input using shell script?
getopts receive argument(s) from command line for shell.

create a file named "shell-script-example-parse-command-line-input.sh" then copy and paste the below code:

#!/bin/bash

while getopts p:abcd input
do
   case $input in
      a) echo "You press a";
      b) echo "You press b";
      c) echo "You press c";
      d) echo "You press d";
   esac
done

SHELL Script Example: parse command line input using shell script?

Now run the program (shell-script-example-parse-command-line-input.sh) from shell command prompt with some argument(s) & get the desired result as follows:

SHELL Script Example: parse command line input using shell script - output in the shell command prompt

The result in the windows command prompt

 

Related Tutorial Examples

  1. Arithmetic Operations in SHELL Script Example
  2. Unix wc-l Equivalent DOS Command Using C Program
  3. AWK SHELL Script File Last Modification Information Changing Delimiter
  4. UNIX Command Example: head - Read beginning of a file
  5. UNIX Command Example: grep - Searching word or string in file or content
  6. UNIX Command Example: cat - Read file(s)
  7. UNIX Command Example: chgrp - Change group ownership
  8. 20 Steps to Make CakePHP Blog Project
  9. 19 Examples to Learn MySQL
  10. 28 Tutorials to Learn Oracle

No comments:

Post a Comment

leave your comments here..