Sponsored Content
Top Forums Shell Programming and Scripting [bash] wanted: function with a clean way for multiple return values Post 302977229 by stomp on Thursday 14th of July 2016 04:31:30 PM
Old 07-14-2016
Quote:
OK, some "theory of programming 101" seems to be in order.
Thanks for you effort to help me. I'm not a programmer, but a senior system administrator with experience in a dozen different scripting languages and some mainly school-only experience with C/C++/Java. I read your thread in full but did not discover any additional knowledge, which I do not already have. But again, thanks for your kindness to write such extensive explanations for me.

Quote:
pattern is a filename matching pattern; not a regular expression. To get rid of the last || followed by any string of characters from the end of the variable var , you just need:

Code:
Code:
output=${out%||*}

Of course that is correct. I wrote ${out%||[0-9]*}, because I wanted a numerical value(Exit code) to be matched. That is not meant as a regex. Since it never should be zero chars long, which would be the meaning of the regex. It should be a number followed by something.

The reason was because of the not very likely case, the program output contains ||. I'm realizing now that this is not possible, because ${..%||*} matches only the last occurrance of the the pattern, which must be the one I appended myself. So my construction does not add any extra value.

Code:
OUT="${OUT:-<EMPTY>}"

Quote:
But, I have no idea why that is something you would want to do??? And, if that is something you want to do, that is not the way to do it.
Setting OUT to the string "<EMPTY>" is exactly what I'm accomplishing here. The reason is to explicitly point out in the logfile that the command did not output any result. I want it that way because it's a clearer message than just an empty string, which may have other reasons to occur.

---

Thanks for all hints so far. Any hints on the main question asked? ...which is: Ideas and hints to getting nicer, more easy to use/read bash-code on the calling side outside of the _exec function.

Last edited by stomp; 07-15-2016 at 05:43 PM..
 

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
LIBBASH(7)							  libbash Manual							LIBBASH(7)

NAME
libbash -- A bash shared libraries package. DESCRIPTION
libbash is a package that enables bash dynamic-like shared libraries. Actually its a tool for managing bash scripts whose functions you may want to load and use in scripts of your own. It contains a 'dynamic loader' for the shared libraries ( ldbash(1)), a configuration tool (ldbashconfig(8)), and some libraries. Using ldbash(1) you are able to load loadable bash libraries, such as getopts(1) and hashstash(1). A bash shared library that can be loaded using ldbash(1) must answer 4 requirments: 1. It must be installed in $LIBBASH_PREFIX/lib/bash (default is /usr/lib/bash). 2. It must contain a line that begins with '#EXPORT='. That line will contain (after the '=') a list of functions that the library exports. I.e. all the function that will be usable after loading that library will be listed in that line. 3. It must contain a line that begins with '#REQUIRE='. That line will contain (after the '=') a list of bash libraries that are required for our library. I.e. every bash library that is in use in our bash library must be listed there. 4. The library must be listed (For more information, see ldbashconfig(8)). Basic guidelines for writing library of your own: 1. Be aware, that your library will be actually sourced. So, basically, it should contain (i.e define) only functions. 2. Try to declare all variables intended for internal use as local. 3. Global variables and functions that are intended for internal use (i.e are not defined in '#EXPORT=') should begin with: __<library_name>_ For example, internal function myfoosort of hashstash library should be named as __hashstash_myfoosort This helps to avoid conflicts in global name space when using libraries that come from different vendors. 4. See html manual for full version of this guide. AUTHORS
Hai Zaar <haizaar@haizaar.com> Gil Ran <ril@ran4.net> SEE ALSO
ldbash(1), ldbashconfig(8), getopts(1), hashstash(1) colors(1) messages(1) urlcoding(1) locks(1) Linux Epoch Linux
All times are GMT -4. The time now is 06:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy