You are not logged in.

#1 2021-11-15 15:41:23

Joel
Member
From: Tijuana, BC, México
Registered: 2011-12-21
Posts: 121

Shell script: passing function return value to exit status

Hi, there. I'm executing a function and its return value is based on testing value:

Main() {
	ret_val=0
	if [ -z "$1" ]
	then
		ret_val=1
		echo "Nor arguments"
	else
		echo "run stuff"
	fi
	return $ret_val
}

Main $1

exit $?

Above works as expected, but I'm trying to code in one line something like this:

exit ( Main($1) )

But I get syntax error, is it possible to do what I trying to accomplish?

Last edited by Joel (2021-11-15 15:48:00)


* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.
* AUR contributor.

Offline

#2 2021-11-15 15:54:40

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,410

Re: Shell script: passing function return value to exit status

just remove the exit. If the function is the last thing running anyway, the exit code will implicitly be whatever the last command/function ended with.

Last edited by V1del (2021-11-15 15:55:16)

Online

#3 2021-11-15 16:22:02

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: Shell script: passing function return value to exit status

The above is the best answer, but if you really wanted you could keep the exit, but you have to use shell syntax - I have no idea where that attempt came from, but you are getting a syntax error because it's completely nonsensical as written.  It would be:

exit $(Main $1)

But this would exit with a return code echoed by the function, not it's return code, so you'd have to use a bit more:

exit $(Main $1; echo $?)

Or if you really just wanted it on one line:

Main $1; exit $?

Of course, it should then become apparent that the exit call is completely superfluous, which leads to the advise above: just have the call to Main as the last command in the script.

You should also drastically simplify your Main function:

Main() {
   [ -z "$1" ] && echo "Nor arguments" && return 1
   echo "run stuff"
}

But then if you are just going to exit with the return value from the Main function it's even simpler:

Main() {
   [ -z "$1" ] && echo "Nor arguments" && exit 1
   echo "run stuff"
}

This makes your original question a moot point: if a parameter isn't passed to Main, the script exits with the selected exit code.

This all, however, sidesteps another question: why have a Main function at all?  The only purpose for this in a shell script is so that the main body can be near the top of the script with other functions defined below it - this can be useful in a long and complicated script.  But why are you writing a long and complicated script if you're struggling with simple elements of it?

Last edited by Trilby (2021-11-15 16:29:51)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2021-11-15 16:55:21

lambdarch
Member
Registered: 2021-01-10
Posts: 67

Re: Shell script: passing function return value to exit status

Trilby wrote:

The only purpose for this in a shell script is so that the main body can be near the top of the script with other functions defined below it - this can be useful in a long and complicated script.

There may be another reason to do this: to be able to declare global variables as read-only while being able to override them locally.
This can happen, even though one might consider that global variables should not be overridden, whether they are read-only or not.

Offline

#5 2021-11-16 14:21:03

Joel
Member
From: Tijuana, BC, México
Registered: 2011-12-21
Posts: 121

Re: Shell script: passing function return value to exit status

Thanks all for you tips.
@Inspector Parrot:

But why are you writing a long and complicated script if you're struggling with simple elements of it?

Still learning the syntax sad


* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.
* AUR contributor.

Offline

#6 2021-11-16 14:59:23

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: Shell script: passing function return value to exit status

Sorry - I didn't mean you shouldn't be pursuing these goals.  We're all still learning.  But your best learning will come from tackling appropriately scaled projects for your current understanding.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB