[Solved] Strange Problem in case statement while shift


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Strange Problem in case statement while shift
# 1  
Old 02-18-2013
[Solved] Strange Problem in case statement while shift

Hi,

Here is my code as below:

test.ksh:
=======
Code:
 
#!/bin/ksh
option="${1}" 
while [ $# -gt 0 ]
do
case $1 in 
   -f) FILE="${2}" 
   echo "File name is $FILE"
   ;; 
   -d) DIR="${2}" 
   echo "Dir name is $DIR"
   ;;
  -*)  
   echo "`basename ${0}`:usage: [-f file] | [-d directory]" 
   exit 1 # Command to come out of the program with status 1
   ;; 
   *)  ;;
   esac 
   shift 1
done

While i am running the above script:

Code:
 
[kanix@voodoo mydir]$ ./test.ksh -f "abc.txt"
File name is abc.txt

The above output is as expected.. But when i am running as below:

Code:
 
[kanix@voodoo mydir]$ ./test.ksh -f "-abc.txt"
File name is -abc.txt
test.ksh:usage: [-f file] | [-d directory]

the usage code is getting displayed.. which is not expected..

But again when i am providing a space between the quote and hyphen it is again showing the expected result.

Code:
 
[kanix@voodoo mydir]$ ./test.ksh -f " -abc.txt"
File name is  -abc.txt

Please advise why this is happening and how to fix this.. i need to run the code as : ./test.ksh -f "-abc.txt"
# 2  
Old 02-18-2013
I think ist your minus sign before a playing with you...
# 3  
Old 02-18-2013
yes.. That i know.. but how to avoid this prob?
# 4  
Old 02-18-2013
Code:
ran:/home/vbe/wks $ ksh001 -f -t
ksh001:usage: [-f file] | [-d directory]
ran:/home/vbe/wks $ ksh001 -f -titi.txt
File name is -titi.txt
ran:/home/vbe/wks $ 
Solution:
       -? )
       echo "`basename ${0}`:usage: [-f file] | [-d directory]"

Addendum: a bit more testing:
Code:
ran:/home/vbe/wks $ ksh001 -f -titi.txt      
File name is -titi.txt
ran:/home/vbe/wks $ ksh001 -f -titi.txt -a DD
File name is -titi.txt
ksh001:usage: [-f file] | [-d directory]
ran:/home/vbe/wks $ ksh001 -f -titi.txt -d DD
File name is -titi.txt
Dir name is DD
ran:/home/vbe/wks $ ksh001 -f -titi.txt -d -DD
File name is -titi.txt
Dir name is -DD


Last edited by vbe; 02-18-2013 at 09:42 AM..
This User Gave Thanks to vbe For This Post:
# 5  
Old 02-18-2013
Quote:
Originally Posted by vbe
Code:
. . .
Solution:
        -? )
. . .

I don't think that will cure the problem although it suppresses the symptoms. Try it with ./test.ksh -f -a, and - voila, the usage msg.

As your "option" and "file/dir name" always come in pairs, make it shift 2, and it will fly, always operating on a new pair!
This User Gave Thanks to RudiC For This Post:
# 6  
Old 02-18-2013
I wanted to solve the 3rd post issue, and forgot to read all the code again to see what it was for... You are absolutely correct RudiC... thanks for passing behind me Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[SOLVED] Problem in wait statement

Iam having a script which is used to load users and dumpfile in any given schema.Iam trying to autolog the script and have added two fucntion in it. function init_stdout_redirect { OUT_LOG=$1 OUT_PIPE=$(mktemp -u) # Create the output pipe mkfifo $OUT_PIPE # Save stdout and... (15 Replies)
Discussion started by: Vikram_Tanwar12
15 Replies

2. Shell Programming and Scripting

Problem using bash case statement

I have the following bash script and it is not accepting the lines "--"|"--""-") "--""-"") while do echo "Current Argument is ${1}" case "$1" in "--"|"--""-") echo "Argument is ${1}" shift # Skip ahead one to the next argument. ... (1 Reply)
Discussion started by: kristinu
1 Replies

3. Homework & Coursework Questions

Problem with executing a possible if or case statement script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a phonebook program. It should use functions to perform the required tasks. It should be menu-based,... (1 Reply)
Discussion started by: Rgasin02
1 Replies

4. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

5. Shell Programming and Scripting

Problem using 'Case' statement

Hi, When I execute the below script, I am getting the error as ' is not expected.ror at line 3 : `in #!/bin/sh case $1 in -r) echo Force deletion without confirmation ;; -i) echo Confirm before deleting ;; *) echo Unknown argument ;; esac I could not see any problem with... (1 Reply)
Discussion started by: Sathish_Obla
1 Replies

6. Shell Programming and Scripting

Problem with CASE statement

Guys, Here is the script syntax which is not accepting the parameters & not performing the said activity. $ ./routing.sh xyz123-ra str enable ********************************************************************** Preparing to service the request for Device xyz123-ra in Question... (9 Replies)
Discussion started by: raghunsi
9 Replies

7. Shell Programming and Scripting

Problem with case statement

Hi, I need modify the all lines in a file into one format. cat file htec.twe34c.ATI .hesh.twdghu..ATI ..hesh.twdghu..ATI htec.twe3 hjsct14567ati Output should have 16 characters htectwe34c ATI heshtwdghu ATI heshtwdghu ATI htectwe3 ATI hjsct14567 ATI (4 Replies)
Discussion started by: kartheek
4 Replies

8. Programming

strange effect: if ... else in C influence a previous statement

I write a short code in c it may work well, but when I add an if ... else ... structure, a computation before the structure can not give a correct result compilation and run can do well, but the result is wrong. Very strange (1) y2 = expression; //y2 = 0 always 0 ... (8 Replies)
Discussion started by: cdbug
8 Replies

9. Shell Programming and Scripting

what is problem with this small shell script.. case statement related

Hi All, this small script is written to recognize user input character.. it is in small case .. upeer case or is a number... but when i input first capital letter say A.. it always gives small character.... what is the problem. #!/bin/bash echo "Enter the character" read a case $a in )... (2 Replies)
Discussion started by: johnray31
2 Replies

10. UNIX for Advanced & Expert Users

Case statement problem

I need to display this menu and accept variables. Can someone tell me why i am having a problem with this case statement, please # TAPE MANAGER MAIN MENU tapemgr_Main_Menu() { echo "Legato Tape Management System Menu" echo " This system is used to report Legato ERV Offsite and Tapes... (6 Replies)
Discussion started by: gzs553
6 Replies
Login or Register to Ask a Question