Check and control params in parsing file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check and control params in parsing file
# 1  
Old 02-26-2013
Check and control params in parsing file

Hello,

I would like to control and check the right parameters
$1 must have 4 alphabetics digits among eora qora pora fora
$2 must have 2 numerics digits 00 to 11
$3 must have 2 numerics digits 00 to 59
$4 must have 10 characters alpha numerics as 2013-02-26

For example :
In case 5) if i launch my script with these parameters
Code:
./my_script dora 09 30 2013-02-26

The first parameter dora is bad, i would like that the script returns "The first parameter dora is bad"
and so on.
Can you give me some advices ?
Thank you

Code:
#!/bin/bash
# set -vx

f1()    {
         echo $1 $2 $3 $4                                       # put your function here
        }


case $# in
        0)      echo -e "# comment1\n# comment2" > list_file    # create list_file as desired
                read -p "Please fill in this list_file: "       # prompt as desired, read answer
                echo $REPLY                                     # do whatever with the user's reply
                ;;
        1)      [ -f "$1" ] || exit                             # check if file exists; otherwise exit
                grep -v '^#' "$1" |                             # do what you did before
                        while read arg1 arg2 arg3 arg4
                                do f1 $arg1 $arg2 $arg3 $arg4
                                done
                ;;
        4)      echo -e "# comment1\n# comment2" > list_file    # create list_file as desired
                echo $1 $2 $3 $4 >> list_file                   # put your function here
                ;;
        5)      echo -e "# comment1\n# comment2" > $1           # create file with $1 file name as desired
               f1 $1 $2 $3 $4 >> "$1"                   # put your function here
                ;;

        *)      echo "error msg"; exit
esac


Last edited by amazigh42; 03-02-2013 at 05:22 AM..
# 2  
Old 03-01-2013
Doesn't bash do full regex matching in:
Code:
$ bash -c 'if [[ 99 == '[0-9][0-9]' ]] ; then echo true ; fi'
true
$

# 3  
Old 03-01-2013
Quote:
Originally Posted by DGPickett
Doesn't bash do full regex matching in:
Code:
$ bash -c 'if [[ 99 == '[0-9][0-9]' ]] ; then echo true ; fi'
true
$

No. It is using filename pattern matching. Try:
Code:
bash -c 'if [[ 9a9 == '[0-9].[0-9]' ]] ; then echo true ; fi'

and
Code:
bash -c 'if [[ 9a9 == '[0-9]?[0-9]' ]] ; then echo true ; fi'
true

---------- Post updated at 10:37 ---------- Previous update was at 10:23 ----------

Quote:
Originally Posted by amazigh42
Hello,

I would like to control and check the right parameters
$1 must have 4 alphabetics digits among eora qora pora fora
$2 must have 2 numerics digits 00 to 11
$3 must have 2 numerics digits 00 to 59
$4 must have 10 characters alpha numerics as 2013-02-26

For example :
In case 5) if i launch my script with these parameters
Code:
./my_script dora 09 30 2013-02-26

The first parameter dora is bad, i would like that the script returns "The first parameter dora is bad"
and so on.
Can you give me some advices ?
Thank you

Code:
#!/bin/bash
# set -vx

f1()    {
         echo $1 $2 $3 $4                                       # put your function here
        }


case $# in
        0)      echo -e "# comment1\n# comment2" > list_file    # create list_file as desired
                read -p "Please fill in this list_file: "       # prompt as desired, read answer
                echo $REPLY                                     # do whatever with the user's reply
                ;;
        1)      [ -f "$1" ] || exit                             # check if file exists; otherwise exit
                grep -v '^#' "$1" |                             # do what you did before
                        while read arg1 arg2 arg3 arg4
                                do f1 $arg1 $arg2 $arg3 $arg4
                                done
                ;;
        4)      echo -e "# comment1\n# comment2" > list_file    # create list_file as desired
                echo $1 $2 $3 $4 >> list_file                   # put your function here
                ;;
        5)      echo -e "# comment1\n# comment2" > $1           # create file with $1 file name as desired
                shift
                f1 $1 $2 $3 $4 >> "$1"                   # put your function here
                ;;

        *)      echo "error msg"; exit
esac

Is this a homework item? If so, this is the wrong forum.
Note: "-" is not an alphanumeric character. It is a punctuation character.
# 4  
Old 03-01-2013
File glob == is nice, but regex =~ is nicer!
Code:
$ bash -c 'if [[ 99 =~ '[0-9]\{2\}' ]] ; then echo true ; fi'
true
$

# 5  
Old 03-02-2013
Hello,
Here my new modifed script.
I only verify the first parameter in the inputfile named list_file
Code:
$ cat list_file
# comment1
# comment2
dora B C D

When i launched
Code:
./my_script list_file
bad argument

there would have
Code:
./my_script list_file

Thanks in advance for your help.

dora B C D

Code:
#!/bin/bash
# set -vx

f1()    {
      bash -c 'if [[ "$arg1" =~ '^\{d,q,p,f}ora' ]]; then
       echo $1 $2 $3 $4
      else echo "bad argument";
       fi'
        }
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# comment2" > $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 arg4
                                do f1 $arg1 $arg2 $arg3 $arg4
                                done
                   else
                   read -p "Please fill in $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 >> "$1"
                ;;

        *)      echo "error msg"; exit
esac

# 6  
Old 03-02-2013
You're not running bash within bash to do that parameter check, are you? And you know, that single quotes within single quotes don't work?
And, I have to admit, even though I gave you the first hints on that script snippet, I'm totally lost now trying to read and understand what you are aiming for.
Would you mind to present - in plain words - what the target of your script is?
# 7  
Old 03-02-2013
Quote:
Originally Posted by RudiC
You're not running bash within bash to do that parameter check, are you? And you know, that single quotes within single quotes don't work?
And, I have to admit, even though I gave you the first hints on that script snippet, I'm totally lost now trying to read and understand what you are aiming for.
Would you mind to present - in plain words - what the target of your script is?
Thank you RudiC,
I'm learning scripting, I want increase the robustness of the script.
There, i want to control each parameters entered by the user in the input file.
$1 must have 4 alphabetics digits like eora qora pora fora
$2 must have 2 numerics digits 00 to 11 (hours)
$3 must have 2 numerics digits 00 to 59 (minutes)
$4 must have 10 characters alpha numerics and ponctuations as 2013-02-26
./my_script dora 09 30 2013-02-26

I used to take this script in order to test for example regular expressions, certains conditions etc... Then i can implement them in others scripts.
I hope I was clear Mister RudiC. I learn a lot with you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing a control file loop

Hi, Let's say I have a control file like this: RHEL apple "echo apple" RHEL bravo "ls -l bravo*" RHEL church "chmod church.txt" SUSE drive "chown user1 drive.txt" SUSE eagle "echo "eagle flies"" SUSE feather "ls -l feather*" HP-UX google "sed 's/^Google.*$/&\ ACTION: go to... (14 Replies)
Discussion started by: The Gamemaster
14 Replies

2. Shell Programming and Scripting

Control machine and check value

I'm thinking how to control and execute command to control my machine like update them, scenario is creating a cron in machine 1 machine 2 machine 3 which download .txt file from core machine, this txt file contain update function like: kernel=1 yum=0 ssl=1 and store this file in... (4 Replies)
Discussion started by: nimafire
4 Replies

3. Shell Programming and Scripting

Check and control params in parsing file

Hello, I would like to control and check the right parameters $1 must have 4 alphabetics digits among eora qora pora fora $2 must have 2 numerics digits 00 to 11 $3 must have 2 numerics digits 00 to 59 $4 must have 10 characters alpha numerics as 2013-02-26 For example : In case 5) if i... (1 Reply)
Discussion started by: amazigh42
1 Replies

4. Shell Programming and Scripting

Check whether file is processed in a control file list

I've a list of files which got processed in Target table A and stored in a control file(list of control files). I've want to trigger another process (later) based on this list of files and load into Target table B and continue running this process until this file list is exhuasted. How do I come... (1 Reply)
Discussion started by: manojg9
1 Replies

5. Fedora

how to check if autosys or control-M is running?

Hi, On a unix/linux server, how do I check if Autosys or Control-M (scheduler) is running? are there unique processes for these applications that I could do ps -ef | grep ??? thanks, Jason (11 Replies)
Discussion started by: seafan
11 Replies

6. Shell Programming and Scripting

Check params for number

I have 2 and three params, both I should make sure thay numbers at one single line insted of checking for each one . Example I wroote the following way.. checking for 2 and three seperately but I shud be able to do it at on statement echo $2 | egrep '^+$' >/dev/null 2>&1 if ; then echo... (2 Replies)
Discussion started by: raopatwari
2 Replies

7. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

8. UNIX for Dummies Questions & Answers

How to automate check outs from version control?

I'm in a fustrating situation where I am repeatidly checking code, editing, synchronizing, finding something is broke, reverting all my changes and starting over. This if often easier than trying to merge my changes with someone who has beat me to the checkin. Is there a way I can mitigate... (5 Replies)
Discussion started by: siegfried
5 Replies

9. Programming

parsing a string to check if it's an IP address

Hello everybody! I woul quickly need a function bool ParseIPString(char*) that parses a string to check if it's in IP format. thanks in advance for any help! best regards, nadiamihu (1 Reply)
Discussion started by: nadiamihu
1 Replies

10. Shell Programming and Scripting

sed & awk--get section of file based 2 params

I need to get a section of a file based on 2 params. I want the part of the file between param 1 & 2. I have tried a bunch of ways and just can't seem to get it right. Can someone please help me out.....its much appreciated. Here is what I have found that looks like what I want....but doesn't... (12 Replies)
Discussion started by: Andy Cook
12 Replies
Login or Register to Ask a Question