Case loop condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case loop condition
# 8  
Old 03-12-2013
If you look at this block of code:

Code:
    if ! g1 $1 $2 $3 $4
    then
         echo "Errors found in date/time - exiting now" >&2
         exit 1
    fi

$1 is file
$2 - $4 are blank

I assume you want to process contents of file $1 something like this:

Code:
grep -v '^#' "$1" | while read hour log min date rest
do
  if ! g1 $date $hour $min
  then
         echo "Errors found in date/time - exiting now" >&2
         exit 1
  f1
done


Last edited by Chubler_XL; 03-12-2013 at 08:14 PM..
# 9  
Old 03-13-2013
Quote:
Originally Posted by Chubler_XL
If you look at this block of code:

Code:
    if ! g1 $1 $2 $3 $4
    then
         echo "Errors found in date/time - exiting now" >&2
         exit 1
    fi

$1 is file
$2 - $4 are blank

I assume you want to process contents of file $1 something like this:

Code:
grep -v '^#' "$1" | while read hour log min date rest
do
  if ! g1 $date $hour $min
  then
         echo "Errors found in date/time - exiting now" >&2
         exit 1
  f1
done

Hello,

Can you explain what "if ! g1 $date $hour $min" do exactly.
For me it means if the function doesn't return something then send this message "Errors found in date/time - exiting now". Is it right ?
# 10  
Old 03-14-2013
Hello,

Can you help me ? Whatever i want, if there is a bad parameter, the function f1 should not work.
I tried everything according to my poor skills.

Here my inputfile
Code:
cat file
2013-03-31 09 31

With
Code:
[ "$EC" = 1 ]

and right parameters
Code:
./my_script file
Errors found in date/time - exiting now

With
Code:
[ "$EC" = 0 ]

and right parameters
Code:
./my_script file
2013-03-31 09 31

With
Code:
[ "$EC" = 1 ]

and bad parameters
Code:
./my_script file
Errors found in date/time - exiting now

With
Code:
[ "$EC" = 0 ]

and bad parameter
Code:
./my_script file
2013-03-31 09 32

Code:
./my_script file
Errors found in date/time - exiting now

Another weird thing
When the file has 2 lines with [ "$EC" = 0 ] and bad parameter or right parameter there are 4 lines
.
Code:
/my_script file
2013-03-31 09 31
2013-03-31 09 23
2013-03-31 09 31
2013-03-31 09 23

When the file has 3 lines with [ "$EC" = 0 ] and bad parameter or right parameter there 8 lines
Code:
my_scrip file
2013-03-31 09 31
2013-03-31 09 23
2013-03-31 09 18
2013-03-31 09 31
2013-03-31 09 23
2013-03-31 09 18
2013-03-31 09 31
2013-03-31 09 23
2013-03-31 09 18

And so on with 4 lines the print returns 16 lines.

Here the script
Code:
#!/bin/sh
#########################################################################
DATE=$1
HOUR=$2
MN=$3
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'"', Hour in 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" = 1 ] && exit $EC
   #[ "$EC" = 1 ]
   [ "$EC" = 1 ]
}


f1() {
#echo -e "$DATE $HOUR $MIN"
echo -e "$1 $2 $3"
}
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 "$1": "

                        echo $REPLY
               else
                        if [ -f "$1" -a $(grep -v '^#' "$1" | wc -l ) -ne 0 ]
                        then
                                grep -v '^#' "$1" |
                                while read arg1 arg2 arg3
                                do
                                if ! g1 $arg1 $arg2 $arg3
                                then
                                        echo "Errors found in date/time - exiting now" >&2
                                        exit 1
                                else

                                         grep -v '^#' "$1" |
                                                while read arg1 arg2 arg3
                                                do f1 $arg1 $arg2 $arg3
                                                done
                                 fi
                                done
                        else
                                read -p "Please fill in "$1": "
                                echo $REPLY
                        fi
                fi
               ;;

        3)      echo -e "# Fill in the 3 variables below\n# DATE HOUR MN" > $1
                echo $2 $3 $4 >> "$1"                  # concatenation of variables in $ 1
                                grep -v '^#' "$1" | # Omits the comments
                                while read arg1 arg2 arg3 # Parse the parameters
                                do f1 $arg1 $arg2 $arg3 # Processing parameters for the function f1
                                done
                ;;
 *)      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