![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
I would think the presence of a second input variable would be enough:
Code:
if [ -n $2 ] #do this else #do this |
|
||||
|
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 ^_^ |
|
||||
|
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
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.. |
|
||||
|
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? |
|
||||
|
Have you thought of maybe adding an option to your script:
Code:
script.sh --number 123456789 Code:
script.sh --name "Jim Jones" Code:
[0-9-]+ |
|
||||
|
Quote:
Code:
echo 1234 | grep -qv '[^0-9-]' && echo RIGHT echo 12A34 | grep -qv '[^0-9-]' && echo WRONG Quote:
Code:
grep "$NAME" file.txt |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|