Sponsored Content
Full Discussion: Case loop condition
Top Forums Shell Programming and Scripting Case loop condition Post 302779493 by amazigh42 on Tuesday 12th of March 2013 06:46:22 PM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 08:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy