argument count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting argument count
# 8  
Old 08-27-2009
Yay! thanks for that. I got the check numbers grep thing working fine but it still seems to have a problem with how many arguments were entered. If i use [ -n $2 ] it just runs like there is one no matter what,
[ $# -gt 0 ] always thinks there is 2. Smilie

---------- Post updated at 10:51 PM ---------- Previous update was at 10:41 PM ----------

Here's my code just in case it's something obvious i've done that's stopping it from working:
Code:
if [ -f $directory ]   
then
#This will take the number and/or name from the user storing it into $input
        echo -e "Input name or/and number: \c";
        read input
        #This if statement checks to see if the user inputted 1 argument ie name OR number or 2 ie name AND number
        if [ $# -ne 2 ]              
        then
        #This line checks to see if the input contains only digits or hyphens
                if echo $input | grep -qv '[^0-9-]' 
                        then
                        #This case will take the input and check it is a valid number (8digits not starting with 0)
                        case $input in
                        [1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
                        continue;;
                        *) echo "invalid number"; exit
                        esac
else
                        #This case will test if an inputed name is valid.
                        case $input in
                        [!\ a-zA-Z]*) echo "invalid name" ; exit
                        continue ;;
                        *[a-zA-Z]*)
                        continue ;;
                        esac
                fi
        #If the name or number was valid this block of commands will run, checking the #directory for the name/number and returning either no such entry or the entry itself.
        grep "$input" teledir.txt>/dev/null     
        work=$?
        if [ $work -ne 1 ]
        then
        grep "$input" teledir.txt
     else
        echo "No such entry"
        fi
          
        #If 2 statements were inputted this block of commands is run (ie similar but if      #entry doesn't exist it adds it instead of returning no such entry
        else
        echo "2 arguments entered"
        fi 
           
#Tells the user the Teledir.txt was not found
else
echo "Teledir.txt does not exist in this directory"
fi

# 9  
Old 08-27-2009
Quote:
Originally Posted by javajynx
[ $# -gt 0 ] always thinks there is 2.
You must put a shift somewhere in the loop.
It drops $1 and shifts $2 $3 ... to $1 $2 ... and decrements $#.
# 10  
Old 08-27-2009
Is it perhaps because its an inputted value inside the program so it doesn't count as a command line? That might be why it's not counting arguments. Is there a way to remove the need for the user input line so they'd type $sh phone.sh "bob" 1234567 instead of doing it in the program itself?
# 11  
Old 08-27-2009
Quote:
Originally Posted by KenJackson
I think grep -qv '[^0-9-]' is clever.

I think it is wasteful. There is no need to use an external command to determine whether a string contains another string or character. Use case. For example:

Code:
case $1 in
   [!0-9-]* | *[!0-9]* ) echo contains non-numbers ;;
   *) echo numbers only ;;
esac

# 12  
Old 08-29-2009
Hey cfajohnson,

The breadth of your scripting knowledge continues to amaze me. I can't remember ever seeing the sequence [! in any script.

But I found it in the Pattern Matching section of Filename Expansion in the Bash Manual. (Though you could have used the more familiar [^.)

Never-the-less, there are still two arguments in favour of my solution:
  1. Once Linux has executed the grep executable, it remains in memory, so it takes negligible time to execute, and
  2. My solution can fit within the test of a larger and more complex if-then-else sequence.
# 13  
Old 08-29-2009
Quote:
Originally Posted by KenJackson
Hey cfajohnson,

The breadth of your scripting knowledge continues to amaze me. I can't remember ever seeing the sequence [! in any script.

It's a basic pattern. I use it a lot.
Quote:
But I found it in the Pattern Matching section of Filename Expansion in the Bash Manual. (Though you could have used the more familiar [^.)

That is not POSIX and not portable.
Quote:
Never-the-less, there are still two arguments in favour of my solution:
  1. Once Linux has executed the grep executable, it remains in memory, so it takes negligible time to execute, and

The time to fork and exec an external command it still many times longer than a purely shell solution.
Quote:
  • My solution can fit within the test of a larger and more complex if-then-else sequence.

As can mine.
# 14  
Old 08-29-2009
Quote:
Originally Posted by cfajohnson
That is not POSIX and not portable.
Are you saying that the syntax of [!...] as described in the Filename Expansion section of the Bash manual is not POSIX and not portable?
Or do you just mean that Bash in general is not POSIX and not portable?

Oh! I just found a document from the OpenGroup that describes Pattern Matching Notation which says, A bracket expression starting with an unquoted <circumflex> character produces unspecified results. Great! A built-in gotcha.

It is true that Bash has extensions beyond POSIX, and therefore if something works perfectly in the Bash shell, there is no guarantee it will work in some other shell. For that reason there is wisdom in insisting on POSIX compatibility as you do.

But one has to consider the context in which one codes. Many scripts will never be used outside of Bash, or even be seen by someone who holds Bash extensions in other than the highest regard. If one writes a Perl or Python script, it's not wrong to require that it only be run on systems with Perl or Python installed. I don't think it's out of order to view Bash scripts similarly.
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