Case structure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case structure
# 1  
Old 06-08-2007
Case structure

Hi,

Can anyone help me with the following case structure?

Code:
echo "Please enter the date for which you want the Report:\c"
read dat
d1=`echo $dat|cut -c7-8`
m1=`echo $dat|cut -c5-6`
y1=`echo $dat|cut -c1-4`

yr=`expr "$d1" - 1`
case "$yr" in
        0)
           MONTH=`expr "$m1" - 1`
           YEAR=y1
           ;;
                case "$m1" in
                        0)
                           MONTH=12
                           YEAR=`expr "$y1" - 1`
                        ;;
                esac
        DAY=`cal $MONTH $YEAR |grep . | fmt -1 | tail -1`
esac

It returns me an error:

`"$m1"' is not expected

Thanks for your help
kamitsin
# 2  
Old 06-08-2007
Why do you need case? Use if
Code:
echo "Please enter the date for which you want the Report:\c"
read dat
d1=`echo $dat|cut -c7-8`
m1=`echo $dat|cut -c5-6`
y1=`echo $dat|cut -c1-4`

yr=`expr "$d1" - 1`
if [ $yr -eq 0 ]; then
           MONTH=`expr "$m1" - 1`
           YEAR=y1
elif [ $m1 -eq 0 ]; then
            MONTH=12
            YEAR=`expr "$y1" - 1`
fi
DAY=`cal $MONTH $YEAR |grep . | fmt -1 | tail -1`

# 3  
Old 06-08-2007
Thanks, i did use if in the end but i want to know that what is wrong with this case structure Smilie.
kamitsin
# 4  
Old 06-08-2007
Quote:
Originally Posted by kamitsin
Thanks, i did use if in the end but i want to know that what is wrong with this case structure Smilie.
Your syntax is not correct.
Code:
case $var in
expr)
    ...    
    ;;

expr1)  
    ...    
    ;;

*)
    ...    
esac

You have following code without any condition
Code:
  case "$m1" in
                        0)
                           MONTH=12
                           YEAR=`expr "$y1" - 1`
                        ;;
                esac
        DAY=`cal $MONTH $YEAR |grep . | fmt -1 | tail -1`

You can use this
Code:
case "$yr" in
        0)
           MONTH=`expr "$m1" - 1`
           YEAR=y1
           ;;
        *)      case "$m1" in
                        0)
                           MONTH=12
                           YEAR=`expr "$y1" - 1`
                        ;;
                esac
           ;;
esac
DAY=`cal $MONTH $YEAR |grep . | fmt -1 | tail -1`

# 5  
Old 06-08-2007
A small mistake and it made all the difference.

Code:
echo "Please enter the date for which you want the Report:\c"
read dat
d1=`echo $dat|cut -c7-8`
m1=`echo $dat|cut -c5-6`
y1=`echo $dat|cut -c1-4`

yr=`expr "$d1" - 1`
case "$yr" in
        0)
           MONTH=`expr "$m1" - 1`
           YEAR=$y1
#           ;;  - This was not required
                case "$m1" in
                        0)
                           MONTH=12
                           YEAR=`expr "$y1" - 1`
                        ;;
                esac
        DAY=`cal $MONTH $YEAR |grep . | fmt -1 | tail -1`
;;
esac

kamitsin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case structure combined with standard input

Hi folks, I am new to bash scripting so please excuse my question. Is there any chance to combine a case structure with the read command? Like case (read -p "" variable) in x) Thx! (7 Replies)
Discussion started by: haukee
7 Replies

2. Shell Programming and Scripting

case structure in .sh file

Hi, while true do printf "$FBOLD\nPlease enter the minutes (0-59): $FREG" read MIN case "$MIN" in ||s) break 2;; *) echo "" echo "Invalid minutes, please try again.";; esac done In the above... (4 Replies)
Discussion started by: milink
4 Replies

3. UNIX for Dummies Questions & Answers

Fall through case structure

Hello Experts, I was wondering how can we get a fall trough case structure. Eg: If we have two cases A and B and a default; if I do not break after A, it should execute all the statements of A as well as B if I break after B else fall through even lower. I tried this: #!/bin/sh v1=1... (6 Replies)
Discussion started by: hkansal
6 Replies

4. UNIX for Dummies Questions & Answers

ksh case structure

Hello Experts, I ve been trying to build another shell where I am using the following code. transact="tv5cpc1" case "$transact" in "...cp..") xActType="" ;; "...de..") xActType="sp_dep" ;; "...ep..") xActType="sp_epa" ;; "....v.") ... (4 Replies)
Discussion started by: hkansal
4 Replies

5. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

6. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

7. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

8. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

9. UNIX for Dummies Questions & Answers

Problem w. case structure

Hello, I am having a problem setting a range of numbers for the "case" structure. I can use with no problems, but when I use it doesn't work??? Does the case struture allow numeric ranges? eg: echo -e "enter number between 0 and 60: \c" read $answer case $answer in ) echo... (2 Replies)
Discussion started by: Joe54321
2 Replies

10. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question