ksh function getopts get leading underscore unless preceded by hyphen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh function getopts get leading underscore unless preceded by hyphen
# 1  
Old 07-14-2010
ksh function getopts get leading underscore unless preceded by hyphen

If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the underscores.

Can someone, please, point out what I'm doing wrong.

Here's the function (with debugging "echo"s included):

Code:
function logm
{
echo inside logm
#OPTSTRING=":tTdDiIwWeEfFoO"
#while getopts ${OPTSTRING} opt
while getopts ":tTdDiIwWeEfFoO" opt
        do
echo OPTIND=${OPTIND}
echo OPTARG=${OPTARG}
echo opt=$opt
echo 1=$1
echo 2=$2
echo 3=$3
echo 4=$4
                case ${opt} in
                        t|T)
                          echo log TRACE ${2}
                          log TRACE ${2}
                          ;;
                        d)
                          echo log DEBUG ${2}
                          log DEBUG ${2}
                          ;;
                        D)
                          echo log DEBUG ${2}
                          log DEBUG ${2}
                          ;;
                        i|I)
                          echo log INFO ${2}
                          log INFO ${2}
                          ;;
                        w|W)
                          echo log WARN ${2}
                          log WARN ${2}
                          ;;
                        e|E)
                          echo log ERROR ${2}
                          log ERROR ${2}
                          ;;
                        f|F)
                          echo log FATAL ${2}
                          log FATAL ${2}
                          ;;
                        o|O)
                          echo log OFF ${2}
                          log OFF ${2}
                          ;;
                        *)
                         echo unknown opt $opt
                         ;;
                esac
        done
unset OPTSTRING
unset OPTIND
}

Here's how I call it:

Code:
logm -TDIWEFO "this is the tdiwefo message"

Here're the results:

Code:
inside logm
OPTIND=1
OPTARG=
opt=T
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
log TRACE this is the tdiwefo message
OPTIND=1
OPTARG=
opt=_D
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _D
OPTIND=1
OPTARG=
opt=_I
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _I
OPTIND=1
OPTARG=
opt=_W
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _W
OPTIND=1
OPTARG=
opt=_E
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _E
OPTIND=1
OPTARG=
opt=_F
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _F
OPTIND=2
OPTARG=
opt=_O
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _O

# 2  
Old 07-14-2010
Quote:
Can someone, please, point out what I'm doing wrong.
Either use positional parameters ($1 , $2 etc.) or "getopts" but not both.


Code:
We need to introduce a parameter to contain the message. For example "m".

while getopts ":tTdDiIwWeEfFoOm:M:" opt

And in the case statement

m|M)
       MESSAGE_TEXT="${OPTARG}"
       ;;

We can then call the script as:

logm -TDIWEFO -m "this is the tdiwefo message"

However this creates a logic problem in the script where the message gets processed after the "log" command. So in the case statement we need to just store the error message category e.g. "WARN". then issue the "log" command after the end of the case statement.
# 3  
Old 07-14-2010
Thanks for the suggestion, but that won't do what I need: it would only process the last option (tdiwefo) specified before the -m. I want to 'log' for each message category specified in the -tdiwefo option.

What I really want to do is:

Code:
function logm
{
for mc in $(some_command_to_separate $1 into letters)
do
  log $mc $2
done
}

where $1 is some combination of the letters t, d, i, w, e, f, o.

I thought I could use "getopts" instead of "$(some_command_to_separate $1 into letters)".

Anyway ... any idea about the mysterious underscores in my original message (or an elegant way to "$(some_command_to_separate $1 into letters)" )?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Deleting directory with leading hyphen in name

I asked this question last month in Stack Exchange (linux - delete directory with leading hyphen - Server Fault) and none of the answers supplied worked. I have somehow created a directory with a leading hyphen and cannot get rid of it. # ls -li | grep p 2621441 drwxr-xr-x. 2 root root... (4 Replies)
Discussion started by: edstevens
4 Replies

2. UNIX for Dummies Questions & Answers

Removing directory with leading hyphen from root directory

I know that this basic question has been asked many times and solutions all over the internet, but none of the are working for me. I have a directory in the root directory, named "-p". # ls -l / total 198 <snip> drwxr-xr-x 4 root root 4096 Dec 3 14:18 opt drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: edstevens
2 Replies

3. Shell Programming and Scripting

Awk, function of underscore char.

Hello Friends, I would appreciate so much if you could explain how the underscores works at the following code? Sorry if it sounds a bit novice question. awk -F',' 'NR==FNR{_=1;next}!_{print}' exclude infile KR, Eagle (6 Replies)
Discussion started by: EAGL€
6 Replies

4. Shell Programming and Scripting

Help on getopts in ksh

I am trying to make a Shell Script in KSH on Linux box and Solaris box which takes few arguments... with long options using getopts (not getopt). I have my sample code... at the end I will say my requirement... Please help me folks... $ cat dummy #!/bin/bash # Argument = -t test -r server... (6 Replies)
Discussion started by: explorer007
6 Replies

5. Shell Programming and Scripting

sorting file names with hyphen and underscore

Hi, I have two types of file names filename1_12345 or filename1-12345 at the same time I have second type filename2-12345 in a txt file. I am trying to write awk script that will sort these names with below pattern ALL file names like filename1_12345 and filename1-12345 will go to... (1 Reply)
Discussion started by: rider29
1 Replies

6. Shell Programming and Scripting

getopts function in Perl

Hi All I have searches getopts function in Perl a lot, but yet i didn't cleared with it. First I want to know what is the meaning of getopts('t:c:', \%options); and please explain getopts function in an easy way.. (4 Replies)
Discussion started by: parthmittal2007
4 Replies

7. Shell Programming and Scripting

Problem to initialize ksh array when first element includes hyphen

Hi I'm trying to create an array with variable including hyphen but ksh refuses the first element set -A allArgs set +A allArgs ${allArgs} -all set +A allArgs ${allArgs} -date set +A allArgs ${allArgs} test ./test.ksh: -all: bad option(s) It happens only when first element is like... (4 Replies)
Discussion started by: gdan2000
4 Replies

8. Shell Programming and Scripting

Using of gsub function in AWK to replace space by underscore

I must design a UNIX script to monitor files whose size is over a threshold of 5 MB in a specific UNIX directory I meet a problem during the for loop in my script. Some file names contain spaces. ls -lrt | awk '$5>=5000000 && length($8)==5 {gsub(/ /,"_",$9); print};' -rw-r--r-- 1 was61 ... (2 Replies)
Discussion started by: Scofield38
2 Replies

9. Shell Programming and Scripting

egrep string except when preceded by underscore

having trouble with this seemingly simple taks.. :mad: Test data: we want to keep lines 2,3 & 4 -- want to exclude when ${.*schema} is preceded with an underscore; Valid {.*schema} should always be preceded spaces or be found at the beginning of a line. egrep... (5 Replies)
Discussion started by: danmauer
5 Replies

10. Shell Programming and Scripting

ksh: can you use getopts in a function?

I wrote a script that uses getopts and it works fine. However, I would like to put the function in that script in a startup file (.kshrc or .profile). I took the "main" portion of the script and made it a function in the startup script. I source the startup script but it doesn't seem to parse... (4 Replies)
Discussion started by: lyonsd
4 Replies
Login or Register to Ask a Question