Case loop condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case loop condition
# 1  
Old 03-11-2013
Case loop condition

hello,

I would like to do exit at the end ie list all errors before exiting
How to put the token exit in a variable with a loop ?

Thanks

Code:
function g1 () {
case "$1" in
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])
# nothing, OK !
  ;;
(*)
  echo 'Fatal, $1 = '"'$1'"', Date not conform OR absent' >&2
  exit 1
  ;;
esac

case "$2" in
([01][0-9] | 2[0-3])
  # nothing, OK !
  ;;
(*)
  echo 'Fatal, $2 = '"'$2'"', Time 2 digits between 00 and 23' >&2
  exit 1
  ;;
esac

case "$3" in
([01][0-9] | [2-5][0-9])
  # nothing, OK !
  ;;
(*)
  echo 'Fatal, $3 = '"'$3'"', Minute in two digits between 00 and 59' >&2
  exit 1
  ;;
esac
}

# 2  
Old 03-11-2013
Put EC= as first line of your function,
Replace each of your red text above with EC=1
and put [ "$EC" ] && exit $EC as the last line of your function

or try [ "$EC" ] as the last line and call the function like this:

Code:
if g1 $DATE $HOUR $MIN
then
     echo "Errors found in date/time - exiting now" >&2
     exit 1
fi


Last edited by Chubler_XL; 03-11-2013 at 12:34 PM..
# 3  
Old 03-11-2013
Well, since it's in a function, you have a few easy-outs for that. You could return early for good cases, and have bad cases return error outside:
Code:
g1 () {
        case "$1" in
        A)
                return 0 # No error
                ;;
       ... # Many error checks which don't return
       esac

       echo "Some sort of error happened"
       return 1
}

# 4  
Old 03-12-2013
Quote:
Originally Posted by Chubler_XL
Put EC= as first line of your function,
Replace each of your red text above with EC=1
and put [ "$EC" ] && exit $EC as the last line of your function

or try [ "$EC" ] as the last line and call the function like this:

Code:
if g1 $DATE $HOUR $MIN
then
     echo "Errors found in date/time - exiting now" >&2
     exit 1
fi

Thank you Chubler_XL
It doesn't work, i have this error
Code:
syntax error near unexpected token `"$EC"'

---------- Post updated at 08:09 AM ---------- Previous update was at 07:59 AM ----------

Quote:
Originally Posted by Corona688
Well, since it's in a function, you have a few easy-outs for that. You could return early for good cases, and have bad cases return error outside:
Code:
g1 () {
        case "$1" in
        A)
                return 0 # No error
                ;;
       ... # Many error checks which don't return
       esac

       echo "Some sort of error happened"
       return 1
}

Thanks Corona688 for this alternative.
I did not understand everything. Please, can you integrate your code in mine Y

Code:
g1() {
case "$1" in
([E,F,Q,P][A,C,S][A,I,V,P][D,R,T,V])
  # RAS, OK !
  ;;
(*)
  echo 'Fatal, $1 eq '"'$1'"', Bad context' >&2
exit=1
  ;;
esac

# 5  
Old 03-12-2013
All right:
Code:
g1() {
case "$1" in
([E,F,Q,P][A,C,S][A,I,V,P][D,R,T,V])
  # RAS, OK !
  return 0;
  ;;
(*)
  echo 'Fatal, $1 eq '"'$1'"', Bad context' >&2
  ;;
esac

return 1
}

if ! g1 whatever
then
        echo "G1 returned nonzero"
        exit 1
fi

# 6  
Old 03-12-2013
[QUOTE=amazigh42;302779143]Thank you Chubler_XL
It doesn't work, i have this error
Code:
syntax error near unexpected token `"$EC"'

You should have ended up with something like this:

Code:
function g1 () {
  EC=0
  case "$1" in
    ([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])
# nothing, OK !
    ;;
    (*)
      echo 'Fatal, $1 = '"'$1'"', Date not conform OR absent' >&2
      EC=1
    ;;
  esac

   case "$2" in
     ([01][0-9] | 2[0-3])
        # nothing, OK !
    ;;
    (*)
      echo 'Fatal, $2 = '"'$2'"', Time 2 digits between 00 and 23' >&2
      EC=1
    ;;
   esac

   case "$3" in
    ([01][0-9] | [2-5][0-9])
      # nothing, OK !
     ;;
    (*)
      echo 'Fatal, $3 = '"'$3'"', Minute in two digits between 00 and 59' >&2
      EC=1
    ;;
   esac
   [ "$EC" ]
}

if g1 $DATE $HOUR $MIN
then      
    echo "Errors found in date/time - exiting now" >&2
    exit 1
fi

# 7  
Old 03-12-2013
Hello Chubler_XL

Can you help me ?

I launch
Code:
./myscript file

The input file contains the variables (hour, log, minute, date)
Code:
cat file
15 server 21 2013-02-28
25 server 03 2013-02-10
01 server 60 2013-02-29
02 exploitation 21 2013-02-32
19 exploit 22 2013-03-13
18 serv 13 40 2013-02-24

The result is false because
The script takes the variable $1 as file
More, in the last blue line , the date is missing
Code:
Fatal, $1 = 'file', Time 2 digits between 00 and 23
Fatal, $2 = '', Type server OU exploitation.
Fatal, $3 = '', Minute in two digits between 00 and 59
Fatal, $4 = '', Date not conform OR absent
15 server 21 2013-02-28
25 server 03 2013-02-10
01 server 60 2013-02-29
02 exploitation 21 2013-02-31
19 exploit 22 2013-03-13
18 serv 13 40

This script contains 2 functions f1 and g1
(put your function in f1)

I follow yours instructions in red
Code:
g1()    {
EC=0
case "$1" in
([01][0-9] | 2[0-3])
  # nothing, OK !
  ;;
(*)
  echo 'Fatal, $1 = '"'$1'"', Time 2 digits between 00 and 23' >&2
  EC=1
  ;;
esac

case "$2" in
(server|exploitation)
  # nothing, OK !
   ;;
(*)
  echo 'Fatal, $2 = '"'$2'"', Type server OU exploitation.' >&2
  EC=1
  ;;
esac
return 1

case "$3" in
([01][0-9] | [2-5][0-9])
  # nothing, OK !
  ;;
(*)
  echo 'Fatal, $3 = '"'$3'"', Minute in two digits between 00 and 59' >&2
  EC=1
  ;;
esac

case "$4" in
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])


#([1-9][0-9][0-9][0-9]-[0{1-9} | 1{0-2}]-[0{1-9} | 1{0-9} | 2{0-9} | 3{0-1}])
  # nothing, OK !
  ;;
(*)
  echo 'Fatal, $4 = '"'$4'"', Date not conform OR absent' >&2
  EC=1
  ;;
esac
[ "$EC" ]
}
f1()  {
echo $1 $2 $3 $4
}
case $# in
        0)      echo -e "# comment1\n# comment2" > list_file
                read -p "Please fill in this list_file: "
                echo $REPLY
                ;;
        1)
                        if [ ! -f "$1" ]
                         then  > $1 && echo -e "# comment1\n# comment1" > $1
                         read -p "Please fill in this file "$1": "
                         echo $REPLY
                        else
                                if [ -f "$1" -a $(grep -v '^#' "$1" | wc -l ) -ne 0 ]
                                 then
                                        if ! g1 $1 $2 $3 $4
                                         then
                                         echo "Errors found in date/time - exiting now" >&2
                                         exit 1
                                        fi
                                 grep -v '^#' "$1" |
                                 while read arg1 arg2 arg3 arg4
                                 do f1 $arg1 $arg2 $arg3 $arg4 
                                 done
                                 else
                                 read -p "Please fill in this file $1 : "
                                 echo $REPLY
                                fi
                        fi
               ;;
        4)      echo -e "# comment1\n# comment2" > list_file
                echo $1 $2 $3 $4 >> list_file
                ;;
        5)      echo -e "# comment1\n# comment2" > $1
                f1 $2 $3 $4 $5 $6 >> "$1"
                ;;
 *)      echo "error msg"; exit
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case sensitive in If loop .

Hi All, select app from the menu: ABC DEF GHI JKL ALL # ALL will select all the apps in the menu echo "Enter your option" read option; if then <execute the below command> elif # option is the 1 selection from menu...not ALL <execute the below command> else (14 Replies)
Discussion started by: Devaraj A
14 Replies

2. Linux

How to use a case stmt in a for loop?

How can I merge the move statements with the "FOR" loop to do a move of a file right after it zips it and not wait until all of the files are zipped to move all outisde the for loop. Here is my current code: for file in `ls -rt $svdumpdir/* | grep -v '.gz$' | grep '.gtt$' ` do echo... (8 Replies)
Discussion started by: mrn6430
8 Replies

3. Shell Programming and Scripting

While Loop with if else condition

Hi, I was trying to write a shell script which reads csv file and sends mail in html format along with tables. Hope i have completed 1st part , but while sending mail i was trying to highlight some rows in the table based on the egrep outcome. If the string exists in line/INPUT, i am trying to... (4 Replies)
Discussion started by: varmas424
4 Replies

4. UNIX for Advanced & Expert Users

Lower case test condition

I want to locate directories that are upper, lower or have both upper and lower cases. What I have is: find /tmp/$var2 -type d' " ); && echo "host case is incorrect" || echo "host case is correct" This actually is part of a larger script and it does work but the problem is that it... (3 Replies)
Discussion started by: newbie2010
3 Replies

5. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

6. Shell Programming and Scripting

Help With Loop in Case Statement script

I am writing a bash script that asks the user for input and I need it to repeat until the user selects quit.. I dont know how to write the loop for it I searched all over but i still do not get it.. if anyone could help with this it would be greatly apprciated here is my script so far: #!... (2 Replies)
Discussion started by: Emin_Em
2 Replies

7. Shell Programming and Scripting

Use of -z in while loop condition

Hi, Could you please tell what is the meaning of -z in while loop condition. For example, while ; do echo "*** Enter the age " readage (3 Replies)
Discussion started by: vidyaj
3 Replies

8. Shell Programming and Scripting

if condition in a while loop

Gurus, I need to read a line from a file and strip the characters from it and compare the stripped value with the value I pass to the script while executing it. Below is the code for the same. But when i execute the code, it is throwing an error. #!/bin/ksh . /home/.i_env ... (14 Replies)
Discussion started by: svajhala
14 Replies

9. Shell Programming and Scripting

condition inside a for loop

I have a for loop in my script as shown below. for file_path in $file_list ; do ........my code .......... ...... done Can i restrict the number of files parsing to the variable file_path as 50? That is, even if I have some 100 files in file_list, I need to take only 50 files for... (7 Replies)
Discussion started by: Vijay06
7 Replies

10. UNIX for Dummies Questions & Answers

Testing For Loop condition

How do I test the return condition in the script if no files are found: for file in `Find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN}` do ... .. done I want to capture the return code so I can echo the error or condition. Using if ] always returns zero no matter where it's placed. ... (4 Replies)
Discussion started by: mavsman
4 Replies
Login or Register to Ask a Question