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 05-12-2015, 10:39 AM   #1
WannaBeProg
LQ Newbie
 
Registered: May 2015
Posts: 2

Rep: Reputation: Disabled
Parameter $1 isn't returned after a case (bash scripting)


Hello everyone,

I recently started using "function" and "case" in linux but I came across a slight problem

My code:

Code:
function Return
{
echo $1
}

case "$1" in 
 ""       ) echo "give parameters please";;
 [0-9]*   ) echo "No numbers please";;
 *        ) Return;;


So whenever the first parameter is a simple word I would like it returned, but it seems like there is no returned string (empty space)

Also I know I can just write "echo $1" instead of Return, but I'd like to use a function for this.

Can anyone tell me why the function doesn't return $1? Thanks!
 
Old 05-12-2015, 11:22 AM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,433

Rep: Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353
From 'man bash'
Quote:
Shell Function Definitions
A shell function is an object that is called like a simple command and executes a compound command with a new set of positional parameters.
The $1 positional parameter does not apply within the function. Perhaps use a variable instead.
 
Old 05-12-2015, 02:01 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,110
Blog Entries: 10

Rep: Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525
You can use the "return" statement in a script function and this will return the value to the higher part of the script which called the function and then the calling part should use $? to determine what was returned. Example:
Code:
    return 0
Also you can use the "exit" statement in a function, or anywhere, but the result there will be the script gets exited, and if you give this a value, then the caller of the entire script will receive a result code matching your exit value:
Code:
    exit 1
Next when you call a function in a script, you can space separate the parameters:
Code:
    Function 1 5
And so the function named Function gets two arguments, "1" and "5". Further you can pass $@ to the function and what this does is passes the original script arguments to the function, for instance if the script was called with "1" and "5" and you invoke Function with $@ then Function will receive those same arguments:
Code:
$ ./myscript a b
    Function $@
 
Old 05-12-2015, 02:09 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,260

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
Two things

1) "Return" is a bad name for a function, because there is already a return function that returns to the caller

2) Any BASH function is going to have its own set of parameters. $1 does exist, but it's the first argument that YOU pass to the function, not the first argument that the function's caller received from its caller. If you want to pass an argument to your function, then you'll have to pass it in: "Return $1" would then pass the calling routine's "$1" into the function's "$1", so that it could be accessed inside.
 
1 members found this post helpful.
Old 05-19-2015, 06:09 AM   #5
WannaBeProg
LQ Newbie
 
Registered: May 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks all for your answers,

@suicidaleggroll your solution was exactly what I needed, thanks again
 
  


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] Bash Shell Scripting - DiskSpace Case - Select Statment gdizzle Programming 2 04-30-2015 09:59 PM
Bash scripting using case? millscourtney47 Linux - Newbie 12 11-02-2012 09:45 AM
How to change Case of a string(BASH Scripting)? pinga123 Linux - Newbie 4 07-09-2010 01:31 AM
case problems when scripting in bash camilla_darling Programming 2 03-19-2009 01:37 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 05:54 AM


All times are GMT -5. The time now is 05:49 PM.

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