Problem with my case statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with my case statements
# 1  
Old 04-17-2008
Problem with my case statements

Hi there,

Im having some problems with this function, I pass two arguments to the function $1 $2 (Arguments are month and date inputted by the user)

for some reason the case always fails... however in the cases defined below where it shouldnt fail the result is:

Quote:
Unrecognised input: $
Run Program with no arguments to display program usage.
if it fails with input outside of the scopes defined it works properly:

Quote:
Unrecognised input: 2009
Run Program with no arguments to display program usage.
for the life of me I cannot find the issue..

call to the function is :

Code:
errorCheck $1 $2

Code:
errorCheck ()				#ErrorCheck Function defines user input, and if necessary changes, or displays error message
{
while [ -n "$1" ] ; do

  case $1 in
  
     	#-h*|-H*)             helpMessage ; shift ;;
    
	[1][9][9][0-9])	     searchYear=$1 ; shift ;;
	[2][0][0][0-8])	     echo "what te fuck" ; searchYear=$1 ; shift ;;
	[9][0-9])	     searchYear=`expr $1 + 1900` ; shift ;;
	[0][0-8])            searchYear=`expr $1 + 2000` ; shift ;; 	                        

 	Jan*|jan*|JAN*)      searchMonth="Jan" ; shift ;;
 	Feb*|feb*|FEB*)      searchMonth="Feb" ; shift ;;
  	Mar*|mar*|MAR*)      searchMonth="Mar" ; shift ;;
 	Apr*|apr*|APR*)      searchMonth="Apr" ; shift ;;
 	May*|may*|MAY*)      searchMonth="May" ; shift ;;
 	Jun*|jun*|JUN*)      searchMonth="Jun" ; shift ;;
 	Jul*|jul*|JUL*)      searchMonth="Jul" ; shift ;;
 	Aug*|aug*|AUG*)      searchMonth="Aug" ; shift ;;
 	Sep*|sep*|SEP*)      searchMonth="Sep" ; shift ;;
	Oct*|oct*|OCT*)      searchMonth="Oct" ; shift ;;
	Nov*|nov*|NOV*)      searchMonth="Nov" ; shift ;;
	Dec*|dec*|DEC*)      searchMonth="Dec" ; shift ;;

    *)  echo "Unrecognised input: $1" ;
    	echo "Run Program with no arguments to display program usage.";
	exit ; shift ;;
  esac
  done  
}

# 2  
Old 04-17-2008
Your function works fine for me under bash (cygwin) end ksh (AIX).

Jean-Pierre.
# 3  
Old 04-17-2008
hmm ok maybe ill try restarting cygwin

grats on 1000 posts Smilie
# 4  
Old 04-17-2008
hmmm its not working for me...

ill include all the code, perhaps its something else I have done..

Code:
# ! /usr/xpg4/bin/sh
# cp2377 lab 4
# Author: Michael Abbott
# $Id: 
# 

file="my_sample_log.txt"
oldIFS=$IFS
IFS=":/"

count=0		#count variable
print=0		#variable for print type
month=( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
year=( 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008) 	# Month array
monthCount=(0 0 0 0 0 0 0 0 0 0 0 0) 									# Month Count array
yearCount=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

printResults () 			#Print function that echo's the results with the corresponding counts

{	# if no searchMonth	
	if [ "$print" = "1" ]; then
	
		echo "Results for" $searchYear ":"
		echo

		for (( z=0; z<12;z++))
		do
		
			echo ${month[$z]}  $searchYear ":" ${monthCount[$z]}
		
		done
		
	# if no searchYear
	elif [ "$print" = "2" ]; then
		
		echo "Results for" $searchMonth ":"
		echo

		for (( z=0; z<"${#year[*]}"; z++ ))
		do	
			
			echo ${year[$z]} ":" ${yearCount[$z]}	
			
		done	
		
	# if both SearchMonth and SearchYear	
	elif [ "$print" = "3" ]; then
	
		echo " Results for" $searchMonth $searchYear ":" $count	
	fi
	
}

helpMessage ()				# Prints Program usage 
{
	echo "Invalid Arguments Entered." 
	echo 
	echo "Usage of this program defined with either of the following inputs:"
	echo "sh programName Month Year"
	echo "sh programName Month"
	echo "sh programName Year"
	echo 
	echo "Full month and year inputs (eg: March 2004) can be used, abbreviation is also a valid input (eg: Mar 04)"
	echo 
	exit				# Exits Program
	
}

errorCheck ()				#ErrorCheck Function defines user input, and if necessary changes, or displays error message
{
while [ -n "$1" ] ; do

  case $1 in
  
     	#-h*|-H*)             helpMessage ; shift ;;
    
	[1][9][9][0-9])	     searchYear=$1 ; shift ;;
	[2][0][0][0-8])	     echo "what te fuck" ; searchYear=$1 ; shift ;;
	[9][0-9])	     searchYear=`expr $1 + 1900` ; shift ;;
	[0][0-8])            searchYear=`expr $1 + 2000` ; shift ;; 	                        

 	Jan*|jan*|JAN*)      searchMonth="Jan" ; shift ;;
 	Feb*|feb*|FEB*)      searchMonth="Feb" ; shift ;;
  	Mar*|mar*|MAR*)      searchMonth="Mar" ; shift ;;
 	Apr*|apr*|APR*)      searchMonth="Apr" ; shift ;;
 	May*|may*|MAY*)      searchMonth="May" ; shift ;;
 	Jun*|jun*|JUN*)      searchMonth="Jun" ; shift ;;
 	Jul*|jul*|JUL*)      searchMonth="Jul" ; shift ;;
 	Aug*|aug*|AUG*)      searchMonth="Aug" ; shift ;;
 	Sep*|sep*|SEP*)      searchMonth="Sep" ; shift ;;
	Oct*|oct*|OCT*)      searchMonth="Oct" ; shift ;;
	Nov*|nov*|NOV*)      searchMonth="Nov" ; shift ;;
	Dec*|dec*|DEC*)      searchMonth="Dec" ; shift ;;

    *)  echo "Unrecognised input: $1" ;
    	echo "Run Program with no arguments to display program usage.";
	exit ; shift ;;
  esac
  done  
}
  
evaluateArguments ()			#Compares user input with $file			
{
while read file; do	
	
	set $file			
	
	if [ -z $searchMonth ]; then		# if no searchMonth entered
	
		for (( i=0; i<12; i++))
		do	
			#string comparison
			if [ "$2" = ${month[$i]} -a "$3" = "$searchYear" ]; then
			
				monthCount[$i]=`expr ${monthCount[$i]} + 1`
			
			fi		
				
		done	
		
		print=1			# Print variable used in Print function to ensure correct output
	
		
	elif [ -z "$searchYear" ]; then 	# if no searchYear entered
		
		for (( i=0; i<"${#year[*]}"; i++))
		do		
			#string comparison
			if [ "$2" = "$searchMonth" -a "$3" = "${year[$i]}" ]; then
			
				yearCount[$i]=`expr ${yearCount[$i]} + 1`
				
			fi
			
		done
	print=2
	
	#string comparison, both searchMonth and searchYear entered
	elif [ "$2" = "$searchMonth" -a "$3" = "$searchYear" ]; then

		count=`expr $count + 1`

	print=3		

	fi
	
done < $file

}						

	if [ $# = 0 ]; then		# if no inputs entered call helpMessage function
	
	helpMessage
	
	fi
echo $1 $2
errorCheck $1 $2		$ Error check arguments entered by the user
evaluateArguments 		$ Call evaluate Arguments fuction
printResults $count

IFS=$oldIFS			$ Sets the IFS back to the original version

# 5  
Old 04-17-2008
you don't have a valid option for 2009 !!
2008 works just fine however.
Code:
[2][0][0][0-8])

I'm confused on why you're using braces around some of the numbers where there's exactly one option?
[2][0][0][0-8]) vs 200[0-8]

PS: additionally you could use tr's [:toupper:] or [:tolower:] function to modify your month input so you don't have to guess at every upper/lower case input your users might try to use.
# 6  
Old 04-17-2008
For comments, replace $
Code:
echo $1 $2
errorCheck $1 $2		$ Error check arguments entered by the user
evaluateArguments 		$ Call evaluate Arguments fuction
printResults $count

IFS=$oldIFS			$ Sets the IFS back to the original version

with #
Code:
echo $1 $2
errorCheck $1 $2		# Error check arguments entered by the user
evaluateArguments 		# Call evaluate Arguments fuction
printResults $count

IFS=$oldIFS			# Sets the IFS back to the original version

Jean-Pierre.
# 7  
Old 04-17-2008
Thanks for that, it was the comments that were the problem.

Thanks also for the advice, im new to shell ill take all i can get.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for pattern in variable using case statements

i would like to search a variable for a pattern, without having make any calls to external tools. i have a code like this: COUNTPRO2="gine is very bad vine is pretty good" case "${COUNTPRO2}" in *vine*) factor=${COUNTPRO2} echo $factor ;; esac If the variable contains... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

Convert to case statements from if/elif

Hello, I wrote the case on code but it mistakes. I am not sure. If/elif code: #!/bin/ksh you=$LOGNAME hour=`date | awk '{print substr($4, 1, 2)}'` print "The time is: $(date)" if (( hour > 0 && $hour < 12 )) then print "Good morning, $you!" elif (( hour == 12 )) then (7 Replies)
Discussion started by: Masterpoker
7 Replies

3. Programming

Problem with IF ELSEIF and GOTO statements in FORTRAN

Hi I am reading a book about Fortran 90 and I write the following code, to test my understanding of the first chapter. I have a problem with the last section of the code with deals with an IF, ELSEIF, and GOTO statements. Here is my Code PROGRAM sim ! This code is used to solve two... (3 Replies)
Discussion started by: faizlo
3 Replies

4. Shell Programming and Scripting

problem with case

hello everyone.. i want to make a file which, when u run it, it asks for your name and how many times you want to see your name displayed.. the program ever 10 times it will ask you if you want to continue and u have to answer with Y or N. if you type Y it continues displaying your name from where... (8 Replies)
Discussion started by: Telis
8 Replies

5. Shell Programming and Scripting

Multiple If statements in bash problem

Hi everyone, May you help me with the correct syntax of the follow bash statements please X=10 if ]; then echo "The value is between 1 and 5" for ((i=1;i<=${X})); do echo $i done else if ]; then echo "The value is between 6 and 10" for ((i=1;i<=${X})); do ... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

6. Homework & Coursework Questions

Case statements and creating a file database

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: The assignment is posted below: Maintain automobile records in a database Write a shell script to create,... (1 Reply)
Discussion started by: Boltftw
1 Replies

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

8. Shell Programming and Scripting

Not able to exit from case statements

Hi all, I wrote the following simple shell script to perform addition, subtraction, multiplication and division. In the below program, i am not able to exit from the script Shell Script ----------- #!/bin/sh bgcal() { cal="" echo "Enter the Option Number: \c" read cal if then... (3 Replies)
Discussion started by: uxpassion
3 Replies

9. UNIX for Dummies Questions & Answers

How to combine case statements

Hi, I need to change military time to regular time. I know to use case to indicate whether a.m. or p.m. as follows: case "$hour" in 0? | 1 ) echo a.m.;; 1 ) echo p.m.;; * ) echo p.m.;; esac My question is how do I add the hour and minute... (2 Replies)
Discussion started by: karp3158
2 Replies

10. Shell Programming and Scripting

case statements

i need to use a case statement to do something when the user enters nothing at the prompt. i know about the if statement and that isnt' what i'm interested in using for this. i want to use case. heres the scenerio. a program asks a user for an input. i want to use a case statement to... (1 Reply)
Discussion started by: Terrible
1 Replies
Login or Register to Ask a Question