Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getopts bash
01-14-2010, 08:26 AM
Post: #1
getopts bash
getopts is use pretty much the same in c c++ python fpc and bash. It enables a user to deal with arguments. Here is a example script.

Code:
#!/bin/bash

while getopts "abcn:" option; do
case "$option" in
    a)
        echo 'alpha';;
    b)
        echo 'bravo';;
    c)
        echo 'charlie';;
    n)
        echo "$OPTARG";;
esac
done

abcn is all arguments in getopts. You can choose any characters you want. If you have a : after a letter it means that arguments must have input. Here is a example of runing the script. The script name is getoptsExamp

./getoptsExamp -abc
output:
alpha
bravo
charlie

./getoptsExamp -a -b -c
output:
alpha
bravo
charlie

./getoptsExamp -b -n codecaine
output:
bravo
codecaine

if you don't put in a argument after the -n it will display
/getoptExamp: option requires an argument -- n
Visit this user's website Find all posts by this user
Quote this message in a reply
01-14-2010, 08:21 PM
Post: #2
RE: getopts bash
Very good and interesting !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: