LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 01: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, 01:11 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

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

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

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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, 01:14 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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, 01: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, 02:15 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 07:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration