Sponsored Content
Top Forums Shell Programming and Scripting [bash] wanted: function with a clean way for multiple return values Post 302977134 by bakunin on Wednesday 13th of July 2016 05:43:59 AM
Old 07-13-2016
OK, some "theory of programming 101" seems to be in order:

Subroutines in general come in two forms: procedures and functions. PASCAL had this difference laid down in keywords, while C and all its children blurred this fact by only having functions.

The difference is: functions have one (exactly one!) return value, procedures have none. So one could treat procedures as special functions with a return value of <void> (this in fact is what C does).

In "normal" programming languages the return value of a function could be anything: a number, a string or any other data type the language is capable of to define. For instance:

Code:
mylog=calc_natural_log( 5 )

Will pass the number (constant) 5 as argument to the function "calc_natural_log" and the function will return something (perhaps a floating point number), which in turn gets stored in the variable "mylog".

In shell programming function can only have one sort of return values: an unsigned short short integer, equivalent to the "return code" (or "error level") the OS knows. In fact this is (treated as) the same as this shows:

Code:
if myfunc "arg1" "arg2" ; then
     echo "myfunc() returned 0"
else
     echo "myfunc() returned non-zero"
fi

The if-keyword treats the return code of the function the same way it would treat the RC of any external command:

Code:
if /some/command ; then
     echo "command returned 0"
else
     echo "command returned non-zero"
fi

That leaves the question: how to manipulate data across functions/procedures? First we need to differentiate between data the function only needs to know and data the function needs to change.

Data the function needs to know can be set as globals. It is good practice to use - as a rule of thumb - globals only for constants. You can manipulate such constants via the dot-execution method, but in most cases this is a bad idea because it indeed introduces a side effect of the execution of the function.

In general it is a good idea to pass all the information a function needs to know as a parameter to it. I do usually create a "function outline" before i even write it by simply asking what the function needs to know (the parameter set) and what it needs to give back. This way, when i start actually writing the function, i already have the "interface" of what it has to look like to the outside ready.

Here is an example: i once wrote a function for distributed execution of commands (via ssh-sessions). Now i thought: what do i need to know?

1) the host on which to execute
2) the command to execute
3) the use which to use for the connection
4) the user under which to execute

What do i need to give back to the calling routine?

1) The error level of the command executed
2) any output it might produce (stdout and stderr)
3) a return value of the function itself in case something goes wrong (host refused connection, etc.)

So i could write a function outline like that:

Code:
function fExecRemote
typeset chHost="$1"
typeset chConnUser="$2"
typeset chRemUser"$3"
typeset chCommand="$4"

ssh -nqo 'BatchMode = yes' "$chConnUser"@"$chHost" "sudo su - $chRemUser $chCommand"

return $?
}

# main()

fExecRemote "host1" "myself" "myself" "ls -l"
echo $?

exit 0

Things to do (obviously): catch the output, check if a connection is possible (system can be pinged, ssh-keys are exchanged, ...) and so on ... but this is implementation, not design.

For things a function has to manipulate (instead of just know) there are two ways: first, you can catch the <stdout> of a function:

Code:
function fSub
{

echo "bla"
echo "foo"
echo "bar"

return 0
}

# main()
typeset OUTPUT=""

fSub | while read OUTPUT ; do
  ....
done

Second, there is the process substitution:

Code:
function fSub
{
echo "bla"

return 0
}

# main()
typeset var=$(fSub)

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. Programming

I need idas how to clean up this function.

OpenBSD complains when it sees this function in my program /*This function takes the string "test\n" and returns the string "test\n\test\ntest\n" ENTROPY = 1024 */ void *build_string(int count, char **strarr) { int k; char *new;; size_t max; if(count == 0) { ... (2 Replies)
Discussion started by: frequency8
2 Replies

2. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies

3. Shell Programming and Scripting

Can $? return multiple values?

Hi, I have a script which does something like the below: execute_some_script.sh $arg1 $arg2 `exec-some-cmd` if then; do something else do something else fi However, during some cases, there is an error saying: line xxx: [: too many arguments at the line number which has... (5 Replies)
Discussion started by: laloo
5 Replies

4. Shell Programming and Scripting

BASH: extracting values from multiple lines after a match

Hi there, I have the following output, # raidctl -l RAID Volume RAID RAID Disk Volume Type Status Disk Status ------------------------------------------------------ c0t1d0 IM OK c0t1d0 OK ... (4 Replies)
Discussion started by: rethink
4 Replies

5. Shell Programming and Scripting

Need Multiple Return Values

Hi, I need to retrun multiple values function errorFileCreation { echo "Before" return -1 "Siva"; echo "Aftyer" } echo ${?} - This can be used to getting first value. how can i get second one. Advance Thanks... Shiv (3 Replies)
Discussion started by: rsivasan
3 Replies

6. Programming

Passing multiple values from a function in C

I know multiple values can be returned from a function in C like this: char **read_file ( char * , unsigned long int * );//this is the function prototypeunsigned long int number_of_words = 0;//variable defined in main() and initialized to 0words_from_dictionary = read_file ( "dictionary.dit" ,... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

7. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

8. Shell Programming and Scripting

Bash - multiple line carriage return

Hello! I have one strange question - let's say I have a long, multiple-line string displayed on the terminal using echo, and I would like to make a carriage return to the beginning of this string, no to the beginning of the last line - is something like that possible? I would like to be able to... (1 Reply)
Discussion started by: xqwzts
1 Replies

9. Shell Programming and Scripting

Return multiple values using for loop in perl

I am using a for loop to copy files from say DIR1 and DIR2 to DIR3.I have to check whether files are copied from DIR1 and DIR2 and print the respective message. @path=("$DIR1","$DIR2"); foreach (@path) { $rc=system("cp $_/*xml $DIR3"); if ($rc == 0) { print "Files were copied... (1 Reply)
Discussion started by: liyakathali
1 Replies

10. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies
All times are GMT -4. The time now is 01:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy