Sponsored Content
Top Forums Shell Programming and Scripting $OPTARG changes commandline input Post 302412732 by thommes_pommes on Tuesday 13th of April 2010 11:00:27 AM
Old 04-13-2010
I'm afraid, that you don't understand my problem right now.

I'm reading many arguments with getopts from the commandline. Many of them are optional. My problem is, that I'm reading an argument, that includes an absolute path, as "/test/test*.sql". When I read the argument from $OPTARG, I found the joker "*" substituted with the first file, that matches, for example, "/test/test1.sql", but I need the non substituted argument, so that I can use the input.

So if I had a variable with "/test/test*.sql" I could do the following:

e.g.:

Code:
var_path="/test/test*.sql"

var_file_matches=`ls $var_path | wc -l`
if [ $var_file_matches -gt 1 ]
then
       echo "More than one match with you argument, BREAK!"
       exit 99
fi

.
.
.
other programmcode



This is the part in my script, reading arguments from the shell:

#! /usr/bin/ksh
.
.
.
Code:
while getopts ":rRvVHhk:K:FfQ:q:D:d:Z:z:s:S:p:P:cC" arg # Variablen einlesen 
   do 
        case $arg in 
        q|Q) 
                QUELL_PFAD="$OPTARG" 
                ;; 
        d|D) 
                QUELL_DATEI="$OPTARG" 
                ;; 
        k|K) 
                KONFIG_PFAD="$OPTARG" 
                ;; 
        s|S) 
                SQL_PFAD="$OPTARG" 
                ;; 
        p|P) 
                PARAMETER="$OPTARG" 
                ;; 
        z|Z) 
                ZIEL_PFAD="$OPTARG" 
                ;; 
        c|C) 
                COMPRESS_FLAG=1 
                ;; 
        h|H) 
                usage 
                exit_log 0 
                ;; 
        v|V) 
                version 
                exit_log 0 
                ;; 
        f|F) 
                FORCIERE_NEU_DIR=1 
                ;; 
        r|R) 
                REKURSIV_FLAG=1 
                ;; 
        *) 
                para_fehler 
                ;; 
        esac 
   done 
shift

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Usage of optarg

Hello Friends, I need to pass arguments to a shell scripts. but for this i need to take the arguments only if they are supplied to the script as arguments. Like : Prompt > scriptname -d device_cd -s message so how do i capture these arguments ?> i think we have to use optarg.but i dont... (5 Replies)
Discussion started by: sveera
5 Replies

2. Programming

command line option: optarg

Hi I'm learning how to add in programm another option by command line. For example in the "my_prog" i want to add a " -k " option and then write a number. I mean: my_prog -k 50 and the i should use the number 50. I'm reading about getopt_long and optarg, and what i have done in the code... (0 Replies)
Discussion started by: Dedalus
0 Replies

3. Shell Programming and Scripting

OPTARG is not taking SPACE

Hi I have below code in one of my shell script: if ; then fail $USAGE; fi while getopts hz:r:t:dz: o do case "$o" in h) echo $USAGE ; exit 0;; r) export REQ_ID="$OPTARG";; t) TIMESPAN="$OPTARG";; d) detail="true";; ) ... (0 Replies)
Discussion started by: mohsin.quazi
0 Replies

4. Shell Programming and Scripting

Using curl in commandline

Hi, I am using curl to hit a url using http in solaris 10 using commandline, I want to transfer an attachment(using multipart curl -F) also as a part of the request. If anyone has used kindly help me with the syntax. I am using below command: /usr/local/bin/curl -v... (1 Reply)
Discussion started by: manishmaha
1 Replies

5. Shell Programming and Scripting

Pass date range into optarg

here is the code echo begin while getopts 1:2:3:4: mode do case $mode in 1)echo "You have chosen mode1" case $OPTARG in a) echo "User Specified Date Range" rangelist.sh ;; b) echo "user specified month and year";; ?) echo "Default, Current... (4 Replies)
Discussion started by: pravsripad
4 Replies

6. Shell Programming and Scripting

Multiple commandline switches

I have a script that has commandline switches that work no problem. But i don't know how to enable it to have multiple switches at one time. So I'd want myscript -h -o or even myscript -ho but i'm having no luck incorporating this. I tried shifting but i'm not getting it. Thanks ----------... (0 Replies)
Discussion started by: DC Slick
0 Replies

7. Shell Programming and Scripting

Checking commandline

mymk target How to check is on commandline or no? Cannot to find out ;( Know that I need to use if.....new in shell sorry Please use code tags next time for your code and data. (2 Replies)
Discussion started by: Manueldo
2 Replies

8. Shell Programming and Scripting

[BASH] getopts, OPTARG is passed but empty

EDIT: -- SOLVED -- Heyas, Getting used to optargs, but by far not understanding it. So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals. Passing: browser -t test -d sect $HOME Where -t should change the title, and -d... (0 Replies)
Discussion started by: sea
0 Replies

9. Shell Programming and Scripting

Getopts - space in argument (OPTARG)

Hi, I want to capture space as well from the argument eg: script.ksh -m "Message from xyz" -e "email@xyz.com" script.ksh -m 'Message from xyz' -e 'email@xyz.com' I am parsing using getopts, but for option "m" OPTARG is returning only "Message". Please use code tags next time for... (9 Replies)
Discussion started by: tostay2003
9 Replies
getoptcvt(1)                                                       User Commands                                                      getoptcvt(1)

NAME
getoptcvt - convert to getopts to parse command options SYNOPSIS
/usr/lib/getoptcvt [-b] filename /usr/lib/getoptcvt DESCRIPTION
/usr/lib/getoptcvt reads the shell script in filename, converts it to use getopts instead of getopt, and writes the results on the standard output. getopts is a built-in Bourne shell command used to parse positional parameters and to check for valid options. See sh(1). It supports all applicable rules of the command syntax standard (see Rules 3-10, intro(1)). It should be used in place of the getopt command. (See the NOTES section below.) The syntax for the shell's built-in getopts command is: getopts optstring name [ argument...] optstring must contain the option letters the command using getopts will recognize; if a letter is followed by a colon (:), the option is expected to have an argument, or group of arguments, which must be separated from it by white space. Each time it is invoked, getopts places the next option in the shell variable name and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell or a shell script is invoked, OPTIND is initialized to 1. When an option requires an option-argument, getopts places it in the shell variable OPTARG. If an illegal option is encountered, ? will be placed in name. When the end of options is encountered, getopts exits with a non-zero exit status. The special option -- may be used to delimit the end of the options. By default, getopts parses the positional parameters. If extra arguments (argument ...) are given on the getopts command line, getopts parses them instead. So that all new commands will adhere to the command syntax standard described in intro(1), they should use getopts or getopt to parse posi- tional parameters and check for options that are valid for that command (see the NOTES section below). OPTIONS
The following option is supported: -b Makes the converted script portable to earlier releases of the UNIX system. /usr/lib/getoptcvt modifies the shell script in file- name so that when the resulting shell script is executed, it determines at run time whether to invoke getopts or getopt. EXAMPLES
Example 1: Processing the arguments for a command The following fragment of a shell program shows how one might process the arguments for a command that can take the options -a or -b, as well as the option -o, which requires an option-argument: while getopts abo: c do case $c in a | b) FLAG=$c;; o) OARG=$OPTARG;; ?) echo $USAGE exit 2;; esac done shift `expr $OPTIND - 1` Example 2: Equivalent code expressions This code accepts any of the following as equivalent: cmd -a -b -o "xxx z yy" filename cmd -a -b -o "xxx z yy" -filename cmd -ab -o xxx,z,yy filename cmd -ab -o "xxx z yy" filename cmd -o xxx,z,yy b a filename ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of getopts: LC_CTYPE, LC_MESSAGES, and NLSPATH. OPTIND This variable is used by getoptcvt as the index of the next argument to be processed. OPTARG This variable is used by getoptcvt to store the argument if an option is using arguments. EXIT STATUS
The following exit values are returned: 0 An option, specified or unspecified by optstring, was found. >0 The end of options was encountered or an error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |CSI |enabled | +-----------------------------+-----------------------------+ SEE ALSO
intro(1), getopts(1), sh(1), shell_builtins(1), getopt(3C), attributes(5) DIAGNOSTICS
getopts prints an error message on the standard error when it encounters an option letter not included in optstring. NOTES
Although the following command syntax rule (see intro(1)) relaxations are permitted under the current implementation, they should not be used because they may not be supported in future releases of the system. As in the EXAMPLES section above, -a and -b are options, and the option -o requires an option-argument. The following example violates Rule 5: options with option-arguments must not be grouped with other options: example% cmd -aboxxx filename The following example violates Rule 6: there must be white space after an option that takes an option-argument: example% cmd -ab oxxx filename Changing the value of the shell variable OPTIND or parsing different sets of arguments may lead to unexpected results. SunOS 5.10 7 Jan 2000 getoptcvt(1)
All times are GMT -4. The time now is 04:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy