LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-25-2009, 04:14 PM   #1
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Rep: Reputation: 15
Validate and sort input in script


Hi,

This will most likely be a simple answer.

Currently I have a situation where my script will be sent various options:

Code:
-o1 -o2 -oe3@somthing.com
Now, if I want to run a certain command based on the option I am sent, I am doing the following.



Code:
for o in $(echo $options)
do
  if [ $o == "1" ]
  then
  
run commands
        exit 0
  fi
  if [ $o == "2" ]
  then
          run commands
        exit 0
  fi
done
However you will notice from my first example I gave "-oe3@something.com"

I need to be able to find where "-eXXX" is and then remove the 'e' and use the bit after (3@something.com). Or if it is easier, just forget about the "e" and look for anything with "@" init, but I would perfer to do the "e" method.

This next bit is later on at somepoint.

I will then want to "validate" what I have to check that is it a valid email address (You know so that it has an @ sign a domain name etc.
 
Old 08-25-2009, 04:51 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It's hard to understand exactly what you are trying to do. You list several options but your script only processes one but you use a variable called options for them (or it). The arguments passed to a script appear in $1, $2 etc. but you are processing them (or it) from $options which has the -o already stripped off and you say you get "-eXXX" but the example shows -oeXXX ... so many uncertainties.

Assuming (correct me if I'm wrong) that your script only gets one of these options at a time an that it has no embedded whitespace (so it all arrives in $1) and that somehow $1 gets into $options with the leading -o stripped off then
Code:
case $options in
    1 )
        <commands>
        ;;
    2 )
        <commands>
        ;;
    e* )
        address="${options#e}"
        <validate address>
        <commands>
        ;;
    * )
        <error messaging commands>
        ;;
esac
 
Old 08-25-2009, 08:51 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Maybe getopt or getopts would be better: http://aplawrence.com/Unix/getopts.html
 
Old 08-26-2009, 01:48 AM   #4
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
It's hard to understand exactly what you are trying to do. You list several options but your script only processes one but you use a variable called options for them (or it). The arguments passed to a script appear in $1, $2 etc. but you are processing them (or it) from $options which has the -o already stripped off and you say you get "-eXXX" but the example shows -oeXXX ... so many uncertainties.

Assuming (correct me if I'm wrong) that your script only gets one of these options at a time an that it has no embedded whitespace (so it all arrives in $1) and that somehow $1 gets into $options with the leading -o stripped off then
Code:
case $options in
    1 )
        <commands>
        ;;
    2 )
        <commands>
        ;;
    e* )
        address="${options#e}"
        <validate address>
        <commands>
        ;;
    * )
        <error messaging commands>
        ;;
esac

When I receive the options they will look like '1 2 eXXXX', so I don't think what you put will work? But I may be wrong.

I can't use getopts in my partiuclar situation.
 
Old 08-26-2009, 01:23 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by stuaz View Post
When I receive the options they will look like '1 2 eXXXX'
It would help if you showed us a) what the command line is like when your script is called and b) how you get $options from $1, $2 ... (or $* or $@).

Assuming your $options is a a string containing space-separated values as you said then something like this will work
Code:
set -o noglob
for option in $options
do
    case $option in
        1 )
            <commands>
            ;;
        2 )
            <commands>
            ;;
        e* )
            address="${option#e}"
            <validate address>
            <commands>
            ;;
        * )
            <error messaging commands>
            ;;
    esac
done
set +o noglob
The set -o noglob prevents $options being subjected to file name expansion in case it matches any files in your current directory. set +o noglob turns it back on (the default setting)

Last edited by catkin; 08-26-2009 at 01:24 PM. Reason: Typo
 
Old 08-26-2009, 04:18 PM   #6
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
It would help if you showed us a) what the command line is like when your script is called and b) how you get $options from $1, $2 ... (or $* or $@).

Assuming your $options is a a string containing space-separated values as you said then something like this will work
Code:
set -o noglob
for option in $options
do
    case $option in
        1 )
            <commands>
            ;;
        2 )
            <commands>
            ;;
        e* )
            address="${option#e}"
            <validate address>
            <commands>
            ;;
        * )
            <error messaging commands>
            ;;
    esac
done
set +o noglob
The set -o noglob prevents $options being subjected to file name expansion in case it matches any files in your current directory. set +o noglob turns it back on (the default setting)
Ok, I didn't realise I would need to provide that information so sorry that I left it out. My script is actually an interface script held in /etc/cups/interfaces which when I send a print job will activate my script and do whatever.

So the line I would be sending would be something like:

Code:
lp -dprintername -o1 -oetest@example.com /etc/hosts
So the variable "options" which is fed through to my interface script will contain:

Code:
1 etest@example.com
So what my script current does is search through that varaible for "1" and executes a command when it finds it. However the next stage is for me to send Two commands (like above) - so something like:

Finds Option 1
Also finds Option that starts with 'e', so then find out whatever is after 'e' (to the last blank space - cos i may in the future want to add further commands after). Then at that point I have some text which needs to be verfied as an email address - so maybe check that it has a @ sign? If it is valid then execute an email other wise carry on with the rest of the script.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT: Please validate this script PKrishna Linux - Newbie 1 04-01-2008 11:03 AM
INPUT, SORT with Alpha numaric characters nabmufti Programming 11 02-06-2008 05:43 PM
INPUT, SORT with Alpha numaric characters nabmufti Linux - Newbie 1 02-05-2008 03:57 PM
Validate input and preventing buffur overflows Bluesuperman Programming 1 02-04-2005 12:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration