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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-26-2016, 07:29 AM
|
#1
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Rep:
|
Exit function if no arguments passed in
Hi,
I wrote this:
#!/bin/bash
function check_args(){
if [ $# -eq 0 ]; then
echo "No arguments passed"
fi
}
check_args
and interestingly if I run it like this:
./testing a b c
it still goes into the if, but why??? I have passed in 3 arguments :-)
Please help me out!
Thanks,
Zoli
|
|
|
11-26-2016, 07:57 AM
|
#2
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021
|
You passed 3 to the script, but none to the function which has its own copy of $#
|
|
|
11-26-2016, 09:18 AM
|
#3
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
Quote:
Originally Posted by grail
You passed 3 to the script, but none to the function which has its own copy of $#
|
Yes! True. Thanks, could you help me how to organise this argument check function the way I wrote?
I like bash scripts this way:
Code:
function a(){
echo 'a'
}
function b(){
echo 'b'
}
function main(){
a
b
}
main
Last edited by kzo81; 11-26-2016 at 09:52 AM.
|
|
|
11-26-2016, 09:29 AM
|
#4
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,778
|
please use [code][/code] tags around your code!
you need to pass the arguments to the function (for example):
Code:
function check_args(){
if [ $# -eq 0 ]; then
echo "No arguments passed"
fi
}
check_args "$@"
|
|
1 members found this post helpful.
|
11-26-2016, 09:54 AM
|
#5
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
|
|
|
11-26-2016, 10:03 AM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,778
|
glad to help you
if you want to say thanks just click on yes
if you think your problem is solved, please mark thread as solved.
|
|
|
11-26-2016, 10:49 AM
|
#7
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
|
|
|
11-26-2016, 11:11 AM
|
#8
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,778
|
because main is a function too and you need to pass arguments to main too.
|
|
1 members found this post helpful.
|
11-26-2016, 11:34 AM
|
#9
|
Member
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207
Original Poster
Rep:
|
Thanks! I thought I only need to pass in the arguments only when I define main.
|
|
|
11-26-2016, 12:18 PM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,778
|
as it was already mentioned (in post #2) all the functions have their own arguments, "$@" means the argument list passed to the actual function (or to the script itself).
|
|
1 members found this post helpful.
|
11-26-2016, 01:34 PM
|
#11
|
Senior Member
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950
|
But $# is the number of arguments passed.
|
|
|
11-26-2016, 01:49 PM
|
#12
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021
|
Quote:
Originally Posted by JeremyBoden
But $# is the number of arguments passed.
|
Not sure what the purpose of this post was? Are you simply stating the fact?
|
|
|
All times are GMT -5. The time now is 02:40 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|