$OPTARG changes commandline input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $OPTARG changes commandline input
# 1  
Old 04-13-2010
$OPTARG changes commandline input

Hi there,

I hope, that I do not open an thread, that is already existing, but I didn't found something matching with my problem while searching for problems with "getopts"

My problem ist, that I'm taking arguments from commandline into my script with getopts, I've an flag -s, after that there comes an path to an *.sql File.
e.g. script.sh -s /batch/alpha/database/create_db.sql

This is no problem, but I want to have it more save, want, that the custumer must exactly hit ONE file, even, if he/she is using joker like *.

Therefore I wanted to count the hits, and if there is more than 1 hit, the script should terminate.

But if I type for example: ./script.sh -s /test/test*.sql and in the directory are the files "test.sql" and "test1.sql" I will not find the term "/test/test*.sql" in $OPTARG but "/test/test1.sql" !!!

Is there any chance, perhaps some kind of quoting, that I can get what I typed in on the commandline?? Only then I could check, if the term produces more than one matchings.


Thank you for the help and excuse my worse English, as I'm coming from Germany.

So long,

Thomas
# 2  
Old 04-13-2010
Hello,

You can get count of command line arguments using $# variable. Sample code

Code:
if [ $# -gt 1 ]
then
 echo "Only one argument can be provided to $0" ;
 exit 1 ;
else
 #your desired coding
 echo "Continue" ;
fi

-Nithin.
# 3  
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
Login or Register to Ask a Question