Bash Script for parse input like option and value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script for parse input like option and value
# 1  
Old 06-01-2012
Bash Script for parse input like option and value

I would create a bash script than parse like this:
Code:
test.sh -p (protocol) -i (address) -d (directory)

I need retrive the value after -p for example...
understand???
I hope...

thanks

Last edited by vbe; 06-01-2012 at 08:33 AM.. Reason: code tags please...
# 2  
Old 06-01-2012
Code:
#!/bin/bash
while getopts p:i:d: opt
do
    case $opt in
        p)      protocol="$OPTARG";;
        i)      address="$OPTARG";;
        d)      directory="$OPTARG";;
        ?)      bad=1;;
    esac
done

echo "protcol   -> $protocol" ;
echo "addres    -> $address" ;
echo "directory -> $directory" ;
..........
..............

like this ?
Code:
# ./test.sh -p TCP -i 10.100.100.100 -d /tmp/
protcol   -> TCP
addres    -> 10.100.100.100
directory -> /tmp/

This User Gave Thanks to ygemici For This Post:
# 3  
Old 06-01-2012
Quote:
Originally Posted by ygemici
Code:
#!/bin/bash
while getopts p:i:d: opt
do
    case $opt in
        p)      protocol="$OPTARG";;
        i)      address="$OPTARG";;
        d)      directory="$OPTARG";;
        ?)      bad=1;;
    esac
done

echo "protcol   -> $protocol" ;
echo "addres    -> $address" ;
echo "directory -> $directory" ;
..........
..............

like this ?
Code:
# ./test.sh -p TCP -i 10.100.100.100 -d /tmp/
protcol   -> TCP
addres    -> 10.100.100.100
directory -> /tmp/

exactly!!!!

but, I don't understand completly... could you explain me more please???
thanks
# 4  
Old 06-01-2012
Quote:
Originally Posted by ionral
exactly!!!!

but, I don't understand completly... could you explain me more please???
thanks
Code:
./test.sh -p TCP -i 10.100.100.100 -d /tmp/

We use the getopts command for take the arguments..
OPTARG is the value of the last option argument processed by the getopts built-in command.

while loop goes for as the number of options (so its --> 3 ; p ,i and d ) , so while executed for three 3 times..
protocol , address and directory values assings to $OPTARG at the every case loop process..

HOW?
Code:
in the first while loop OPTARG equals to "TCP" and opt equals to p (so protocol value = "TCP" in the case loop)
second while loop OPTARG equals to "10.100.100.100" and opt equals to i ( address value = "10.100.100.100" in the case loop)
last while loop OPTARG equals to "/tmp/" and opt equals to d (directory value = "/tmp/" in the case loop)

regards
ygemici
# 5  
Old 06-01-2012
thanks, but I test this script and the options must be exactly in this order... other then it's wrong result...
Can I turn over this implementation?
like:

Code:
test.sh -i 10.10.10.10 -p tcp -d /tmp

# 6  
Old 06-01-2012
Quote:
Originally Posted by ionral
thanks, but I test this script and the options must be exactly in this order... other then it's wrong result...
I don't know what you tested but it doesn't sound like the code he posted. It works perfectly fine in any order for me.

You can also roll your own easily enough for simple things:

Code:
#!/bin/sh

while [ "$#" -gt 0 ]
do
        case "$1" in
        -p) PROTOCOL=$2 ; shift ;;
        -i) IP=$2 ; shift ;;
        -d) DIR=$2 ; shift ;;
        *) break ;;
        esac

        shift
done


Last edited by Corona688; 06-01-2012 at 02:10 PM.. Reason: little bugs
# 7  
Old 06-05-2012
Quote:
Originally Posted by Corona688
I don't know what you tested but it doesn't sound like the code he posted. It works perfectly fine in any order for me.

You can also roll your own easily enough for simple things:

Code:
#!/bin/sh

while [ "$#" -gt 0 ]
do
        case "$1" in
        -p) PROTOCOL=$2 ; shift ;;
        -i) IP=$2 ; shift ;;
        -d) DIR=$2 ; shift ;;
        *) break ;;
        esac

        shift
done

sorry... I get mistake... now is ok, thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Menu Driven Bash Shell Script with Default Option

Hi All, I have written a menu driven bash shell script. Current Output is as below: ------------------------------------- Main Menu ------------------------------------- Option 1 Option 2 Option 3 Option 4 Exit ===================================== Enter your... (3 Replies)
Discussion started by: kiran_j
3 Replies

2. Shell Programming and Scripting

Bash Script to parse Perforce Logs

Hi All, I need to write a bash script that will parse some perforce log files, the log files will contain user login information, the script would need to pare the log, and check who logs in, and if the user is a superadmin, then the script will check the ip address to see which server the... (4 Replies)
Discussion started by: BostonRob
4 Replies

3. Shell Programming and Scripting

Bash script - cygwin (powershell?) pull from GitHub API Parse JSON

All, Have a weird issue where i need to generate a report from GitHub monthly detailing user accounts and the last time they logged in. I'm using a windows box to do this (work issued) and would like to know if anyone has any experience scripting for GitAPI using windows / cygwin / powershell?... (9 Replies)
Discussion started by: ChocoTaco
9 Replies

4. Shell Programming and Scripting

BASH script to parse XML and generate CSV

Hi All, Hope all you are doing good! Need your help. I have an XML file which needs to be converted CSV file. I am not an expert of awk/sed so your help is highly appreciated!! XML file looks like this: <l:event dateTime="2013-03-13 07:15:54.713" layerName="OSB" processName="ABC"... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

5. UNIX for Dummies Questions & Answers

bash script to parse sequence...

Hi, I have 4000 list files and 4000 sequence data files. Each list file contains a number of 'headers' and data file contains 'header and data'. I would like to extract data from the data file using the list file and write into a new file. As each of the files are quite large, an efficient piece... (6 Replies)
Discussion started by: Fahmida
6 Replies

6. Shell Programming and Scripting

Bash Shell Script to parse file

Raw Results: results|192.168.2|192.168.2.1|general/udp|10287|Security Note|For your information, here is the traceroute from 192.168.2.24 to 192.168.2.1 : \n192.168.2.24\n192.168.2.1\n\n results|192.168.2|192.168.2.1|ssh (22/tcp)|22964|Security Note|An SSH server is running on this port.\n... (2 Replies)
Discussion started by: jroberson
2 Replies

7. Shell Programming and Scripting

Bash Script to read a file and parse each record

Hi Guys, I am new to unix scripting and I am tasked to parse through a CSV file delimited by #. Sample: sample.csv H#A#B#C D#A#B#C T#A#B#C H = Header D = Detail Record T = Tail What I need is to read the file and parse through it to get the columns. I have no idea on how... (8 Replies)
Discussion started by: 3vilwyatt
8 Replies

8. Shell Programming and Scripting

Need to Parse XML from bash script

I am completely new to bash scripting and now need to write a bash script that would parse a XML file and take out values from specific tags. I tried using xsltproc, xml_grep commands. But the issue is that the XML i am trying to parse is not UTF 8. so those commands are unable to parse my XML's... (4 Replies)
Discussion started by: shivashankar.g
4 Replies

9. Shell Programming and Scripting

Shell script to parse/split input string and display the tokens

Hi, How do I parse/split lines (strings) read from a file and display the individual tokens in a shell script? Given that the length of individual lines is not constant and number of tokens in each line is also not constant. The input file could be as below: ... (3 Replies)
Discussion started by: yajaykumar
3 Replies

10. Shell Programming and Scripting

How do you parse a variable in a bash script?

I have a script I use on my web server (Apache2). I am changing to Lighttpd and need to make a few changes. This is what I use on my apache server #!/bin/bash # accepts 3 parameters: <domain name> <user name> <XXXXXXXX> # domain name is without www (just domain.com) # username would be... (3 Replies)
Discussion started by: vertical98
3 Replies
Login or Register to Ask a Question