LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-26-2016, 07:29 AM   #1
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Rep: Reputation: Disabled
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
 
Old 11-26-2016, 07:57 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
You passed 3 to the script, but none to the function which has its own copy of $#
 
Old 11-26-2016, 09:18 AM   #3
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
Old 11-26-2016, 09:29 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,778

Rep: Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567
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.
Old 11-26-2016, 09:54 AM   #5
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
Code:
check_args "$@"

thanks!!
 
Old 11-26-2016, 10:03 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,778

Rep: Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567
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.
 
Old 11-26-2016, 10:49 AM   #7
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
For some reason it still doesn't accept 3 arguments

https://github.com/kerzol81/Bash-and...r_and_arranger
 
Old 11-26-2016, 11:11 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,778

Rep: Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567
because main is a function too and you need to pass arguments to main too.
 
1 members found this post helpful.
Old 11-26-2016, 11:34 AM   #9
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 207

Original Poster
Rep: Reputation: Disabled
Thanks! I thought I only need to pass in the arguments only when I define main.
 
Old 11-26-2016, 12:18 PM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,778

Rep: Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567Reputation: 7567
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.
Old 11-26-2016, 01:34 PM   #11
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950

Rep: Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513Reputation: 513
But $# is the number of arguments passed.
 
Old 11-26-2016, 01:49 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,021

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
Quote:
Originally Posted by JeremyBoden View Post
But $# is the number of arguments passed.
Not sure what the purpose of this post was? Are you simply stating the fact?
 
  


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
[SOLVED] GCC; See what arguments are passed to the linker. Zssfssz Linux - Software 1 03-04-2012 11:08 AM
[SOLVED] Value gets changed when passed to a function in C++ Aquarius_Girl Programming 7 06-10-2010 03:38 AM
How are arguments passed to a bash script? jiml8 Programming 1 04-09-2009 05:28 PM
Array structs - passed to function cdog Programming 4 02-02-2006 03:07 PM
php not taking arguments when passed through URLs BrianK Linux - Software 2 03-29-2004 11:38 AM

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

All times are GMT -5. The time now is 02:40 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