Sponsored Content
Top Forums Shell Programming and Scripting using getopts to parse a command line Post 302517386 by DGPickett on Tuesday 26th of April 2011 04:10:59 PM
Old 04-26-2011
Is this schoolwork? There are guidelines, a mandatory template.

Did you chmod it executable?

What is your command line, output?

I never use getopt*, as I like to force simpler, more readable options, easily parsed with while case shift: -a -b whatever -c -d -e

Last edited by DGPickett; 04-26-2011 at 05:59 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies

2. Shell Programming and Scripting

File handling, getopts command in unix

I need to create a shell script having the menu with few options such as 1. Listing 2. Change permissions 3. Modify Contents 4. Delete Files 5. Exit 1. For 1. Listing: Display a special listing of files showing their date of modification and access time (side by side) along with their... (2 Replies)
Discussion started by: bab123
2 Replies

3. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies

4. UNIX for Dummies Questions & Answers

getopts - command line arguments

Hi, I'm having problems with a script where I wanted every single option specified in the command line to have an argument taken with it, but for some reason only d works in the code I will be showing below. For example if I did ./thisfile -a something it would come up with "a chosen with " as... (2 Replies)
Discussion started by: IceX
2 Replies

5. Programming

Parse parameters with getopts

Hi, this is my problem I have script with two parameters -n name and -s surname. Both have arguments and I want to know how parse these parameters with getopts. When you write ./names -n John -s White it find you all persons, which name is John White, but when you write ./names -n John ... (1 Reply)
Discussion started by: frees
1 Replies

6. Shell Programming and Scripting

Help with getopts command

Hello All, I have shell script as below in a.ksh. #! /usr/bin/ksh while getopts a: b: ab:f: VAR do case $VAR in a) A=${OPTARG} echo $A;; b) B=${OPTARG} echo $B;; ab) AB=${OPTARG} echo $AB ;; f) F=${OPTARG} echo $F ;; esac done When I execute sh a.ksh -a 1 -b 2 -ab 3 -f 4 as below... (7 Replies)
Discussion started by: tonsat
7 Replies

7. Shell Programming and Scripting

bash:getopts command help

How can I say one of the options is required? can I use an if statement? let say: while getopts ":c:u:fp" opt; do case $opt in c) echo "-c was triggered, Parameter: $OPTARG" >&2;; u) echo "-u was triggered, Parameter: $OPTARG" >&2;; f) echo "-u was triggered,... (2 Replies)
Discussion started by: bashily
2 Replies

8. Shell Programming and Scripting

Getopts how to handle missing '-' in command line args.

I'm using getopts to process command line args in a Bash script. The code looks like this: while getopts ":cfmvhs:t:" option; do case $option in c) operationMode="CHECK" ;; f) operationMode="FAST" ;; m) ... (6 Replies)
Discussion started by: gencon
6 Replies

9. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies

10. Shell Programming and Scripting

How-to enforce check on getopts command

here is my script that expects the user to run it like ./best.sh -f /tmp/log.txt more best.sh #!/bin/bash while getopts ":f:" opt; do case $opt in f) file_in="$OPTARG" ;; \?) echo "Invalid option -$OPTARG" >&2 ;; esac done uname -a SunOS mymac 5.11 11.2 sun4v... (15 Replies)
Discussion started by: mohtashims
15 Replies
getopt(1)						      General Commands Manual							 getopt(1)

Name
       getopt - parse command options

Syntax
       set - - getopt optstring $*

Description
       The  command  breaks  up  options in command lines for easy parsing by Shell procedures and checks for legal options.  The optstring option
       letters are recognized if a letter is followed by a colon, the option expects an argument which may or may not  be  separated  from  it	by
       white space.  For further information, see

       The special option, specified by two minus signs (- -), delimits the end of the options.  If the delimiters are used explicitly, recognizes
       it; otherwise, generates it.  In either case, places the delimiter at the end of the options.  The positional parameters ($1 $2 ...) of the
       shell  are reset so that each option is preceded by a single minus sign (-) and is in its own positional parameter; each option argument is
       also parsed into its own positional parameter.

Examples
       The following code fragment shows how you can process the arguments for a command that can take the options a or b, as well as  the  option
       o, which requires an argument:
       #!/bin/sh5
       set -- getopt abo: $*
       if [ $? != 0 ]
       then
	    echo $USAGE
	    exit 2
       fi
       for i in $*
       do
	    case $i in
	    -a | -b)  FLAG=$i; shift;;
	    -o)  OARG=$2; shift 2;;
	    --)  shift; break;;
	    esac
       done
       This code accepts any of the following as equivalent:
       cmd -aoarg file file
       cmd -a -o arg file file
       cmd -oarg -a file file
       cmd -a -oarg -- file file

Diagnostics
       The command prints an error message on the standard error when it encounters an option letter not included in optstring.

See Also
       sh5(1), getopt(3)

																	 getopt(1)
All times are GMT -4. The time now is 02:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy