argument count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting argument count
# 1  
Old 08-24-2009
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  
Old 08-24-2009
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  
Old 08-24-2009
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.Smilie
Thanks very much ^_^
# 4  
Old 08-25-2009
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  
Old 08-25-2009
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  
Old 08-25-2009
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  
Old 08-25-2009
Quote:
Originally Posted by javajynx
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
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find the count of IP addresses that belong to different subnets and display the count?

Hi, I have a file with a list of bunch of IP addresses from different VLAN's . I am trying to find the list the number of each vlan occurence in the output Here is how my file looks like 1.1.1.1 1.1.1.2 1.1.1.3 1.1.2.1 1.1.2.2 1.1.3.1 1.1.3.2 1.1.3.3 1.1.3.4 So what I am trying... (2 Replies)
Discussion started by: new2prog
2 Replies

2. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

3. Shell Programming and Scripting

kill: bad argument count

Hi Team, I am getting the below error when running the script. Please let me know how to solve this error. start_WFA.sh: kill: bad argument count Below is the Script: #!/bin/ksh kill -9 `ps -ef|grep classpath |grep "/apps/ap" |grep -v "Xmx" |grep $LOGNAME |awk '{print $2}'` Thanks, (6 Replies)
Discussion started by: Mukharam Khan
6 Replies

4. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

5. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

6. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

7. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

8. Shell Programming and Scripting

bad argument count, tryig to FTP

I have a file in a Unix directory called 97210900.EFT I am getting this error: miis_ftp.ELM_EFT.shl: cd: bad argument count + + type=1 + ErrorHandle Here is the piece of code that is checking the file # Change the directory to one contains the file to be transported ##cd... (1 Reply)
Discussion started by: rechever
1 Replies

9. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

10. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies
Login or Register to Ask a Question