LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-02-2010, 12:06 PM   #1
coffeeaddict19
LQ Newbie
 
Registered: Sep 2010
Location: houston
Distribution: fedorra
Posts: 4

Rep: Reputation: 0
issue with checking for no arguments


This is my problem:

#!/bin/bash
NUMOFARGS=$#
if [ $NUMOFARGS="0" ]
then
echo "no arguments"
fi

When I run the above script it prints "no arguments" regardless of whether or not I supply arguments. I know this is probably an idiotic question, but how do I fix this?
 
Old 10-02-2010, 12:11 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Mind the spacing: if [[ "$NUMOFARGS" = "0" ]]

Hope this helps.
 
Old 10-02-2010, 12:13 PM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Also another idea:

Since $NUMOFARGS should contain an integer, why not compare it as an integer?
Code:
if [ $NUMOFARGS -eq 0 ]
 
Old 10-02-2010, 12:14 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by coffeeaddict19
Code:
if [ $NUMOFARGS="0" ]
You're mixing up numbers and strings. Bash treats them differently. Read up on the test command (man test)

What you want is something like this:
Code:
if [ $NUMOFARGS -eq 0 ]
Oh well, I'm late to the party, but I'm not letting my typing go to waste...
 
Old 10-02-2010, 12:20 PM   #5
coffeeaddict19
LQ Newbie
 
Registered: Sep 2010
Location: houston
Distribution: fedorra
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for the help all, it works. I really appreciate the speedy response.
 
Old 10-03-2010, 01:15 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Or you could use the more correct brackets:
Code:
if (( NUMOFARGS == 0 ))
And yes it is correct that the variable does not have a leading $
 
  


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
arguments.......questions!! erwfili Programming 9 11-01-2009 08:02 AM
Checking library functions and its arguments kvtournh Linux - Software 2 03-14-2008 02:02 PM
2.4 too few arguments Problem thomas55 Linux From Scratch 5 11-29-2005 12:00 PM
checking htl instruction (1st time usr w/boot issue) oezo Linux - Newbie 1 05-20-2004 10:46 PM
Issue with recursive script passing arguments gauge73 Linux - Newbie 0 01-06-2004 07:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:34 AM.

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