The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bad argument count, tryig to FTP rechever Shell Programming and Scripting 1 07-21-2009 11:46 AM
get positive number n as argument script must calculate the factorial of its argument I-1 Shell Programming and Scripting 3 04-28-2009 10:24 AM
Getting Sum, Count and Distinct Count of a file singhabhijit Shell Programming and Scripting 4 03-02-2009 12:30 AM
How to find the last argument in a argument line? nehagupta2008 UNIX for Dummies Questions & Answers 4 06-20-2008 12:05 PM
How to count the record count in an EBCDIC file. oracle8 UNIX for Dummies Questions & Answers 1 07-26-2006 08:22 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-24-2009
javajynx javajynx is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 13
argument count

I'm writing a program that takes input from the user of a phone number or a name then either tells them if that entry doesn't exist in a text document or returns the entry if it does exist. But if they enter a name AND number it either returns the entry if it exists or adds it to the document.
To do this involves lots of if statements etcetc but to start I need to know how to tell the program if the user has only put in a number or name or if they've put in both name and number.

Long rant short is there a statement that tests if 1 or two arguments have been inputted?
would the following work?
Code:
if [ $1$2 ]
#test for number and name in document
elif [ $1 ] 
#test if its a valid number or name then test if it's in document
  #2 (permalink)  
Old 08-24-2009
chompy chompy is offline
Registered User
  
 

Join Date: Aug 2009
Location: pwd
Posts: 65
I would think the presence of a second input variable would be enough:

Code:
if [ -n $2 ]
#do this
else
#do this
of course you could always use a case statement, i guess it would depend on the number of variables you are testing for
  #3 (permalink)  
Old 08-24-2009
javajynx javajynx is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 13
if cases

Thanks for your help.
Well the first variable is 1 argument or 2, then (if 1) number or name then validating the number or name then checking if the number/name is in the document (if 2) validating the number and name, checking if it's in the document and adding.
I was thinking having an outer if statment then cases in them to validate with if statements in those to check the document. Ah nesting, gotta love it.
With your [ -n $2 ] I'll be able to start it correctly.
Thanks very much ^_^
  #4 (permalink)  
Old 08-25-2009
KenJackson KenJackson is offline
Registered User
  
 

Join Date: May 2008
Location: Maryland, USA
Posts: 105
Maybe you want something like this:
Code:
#!/bin/sh
NAME= NUMBER=
while $# -gt 0; do
    if echo $1 | grep -qv '[^0-9-]'; then
        # $1 contains only digits and dashes
        test -n "$NUMBER"  &&  { echo "Too many numbers"; exit 1; }
        NUMBER=$1
    else
        test -n "$NAME"  &&  { echo "Too many names"; exit 1; }
        NAME="$1"
    fi
    shift
done

# Now process $NAME and $NUMBER if they aren't blank
I think grep -qv '[^0-9-]' is clever.
It quietly returns true if the value piped in does not contain any characters that are not digits or hyphens.
That is, if true, it only contains those characters.


-------------
Oops. I for forgot the brackets. The 'while' line should be:
while [ $# -gt 0 ]; do
or
while test $# -gt 0; do

Last edited by KenJackson; 08-27-2009 at 08:11 AM..
  #5 (permalink)  
Old 08-25-2009
javajynx javajynx is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 13
Mindtaker!

lol well my next question was how do I determine input is digits or not?
I tried the if echo $1 | grep -qv '[^0-9-]'
but it doesn't appear to accept it as false if I enter a name ie enter 12345678 and it does the number part enter bob and it says invalid number.

Also after I get that figured out how do I get the line from the document that contains the name or number ie entered "bob jones" and it was found in the document. It needs to echo that result
ie Bob Jones 12345567
is there a grep option that returns the line itself?
  #6 (permalink)  
Old 08-25-2009
chompy chompy is offline
Registered User
  
 

Join Date: Aug 2009
Location: pwd
Posts: 65
Have you thought of maybe adding an option to your script:

Code:
script.sh --number 123456789
Code:
script.sh --name "Jim Jones"
That way you would be able to determine what type of validation you should apply. Also you may want to try this regular expression to match the number:

Code:
[0-9-]+
It will match more than one instance, I think yours was stopping at the first instance the expression was satisfied.
  #7 (permalink)  
Old 08-25-2009
KenJackson KenJackson is offline
Registered User
  
 

Join Date: May 2008
Location: Maryland, USA
Posts: 105
Quote:
Originally Posted by javajynx View Post
lol well my next question was how do I determine input is digits or not?
I tried the if echo $1 | grep -qv '[^0-9-]'
but it doesn't appear to accept it as false if I enter a name ie enter 12345678 and it does the number part enter bob and it says invalid number.
Try this from the command line:
Code:
echo 1234 | grep -qv '[^0-9-]'  &&  echo RIGHT
echo 12A34 | grep -qv '[^0-9-]'  &&  echo WRONG
Quote:
Originally Posted by javajynx View Post
Also after I get that figured out how do I get the line from the document that contains the name or number ie entered "bob jones" and it was found in the document. It needs to echo that result
ie Bob Jones 12345567
is there a grep option that returns the line itself?
What's the format of your file? Maybe something as simple as this:
Code:
grep "$NAME" file.txt
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0