Sponsored Content
Top Forums Shell Programming and Scripting [bash] wanted: function with a clean way for multiple return values Post 302977118 by stomp on Tuesday 12th of July 2016 08:00:51 PM
Old 07-12-2016
Quote:
Without knowing what arguments you intend to pass to _exec and what output you are hoping to produce, we can only make wild guesses about what might or might not work for you....Is there a limited set of commands that will be passed to your _exec function as the 1st operand, or can the user specify any command available on your system?
_exec is my own wrapper to call any command. It's called only from within a larger bash script from other functions. I'm clear that it's my solely responsibility to check what I feed into that _exec function and I'm taking care that any data from outside has to be securely examined - which needs extra care when done within bash.

Quote:
Will your _exec function be invoked with a command and parameters in a quoted string passed as the 1st operand; or just a command name that will be executed with no parameters?
Always complete command as first argument as shown in the _delete_compatible example. (No Redirect within the given command line. No variables to be substituted. Ready to run command lines).

Quote:
How many terabytes of output might be produced by the command given as the 1st operand to your _exec function?
It should be generic, but I assume I'll never process more than 50 Kilobytes.

Quote:
Where is the variable ESHELLERR assigned a value (or are you expecting _fatal to be called with three operands instead of four)?
That _fatal thing is working completely fine. But if you're curious: ESHELLERR is a global variable containing an integer which itself represents an index to an associative array with a descriptive message(format string) regarding ESHELLERR. _fatal can be called with variable args. Here are some supplements to _fatal:

Code:
ESHELLERR=64
declare -A ERRMSG
ERRMSG[$ESHELLERR]="Shell execution error. EXITCODE: %s ERRMSG: %s COMMAND: %s"
export ESHELLERR ERRMSG

function _fatal {
        ERROR_CODE=$1
        if [[ -n "$ERROR_CODE" && -n "${ERRMSG[$ERROR_CODE]}" ]] ;then
                if [ -n "$2" ] ; then
                        shift
                        MSG="$(printf "${ERRMSG[$ERROR_CODE]}" "$@")"
                else
                        MSG="${ERRMSG[$ERROR_CODE]}"
                fi
                _log "FATAL: $MSG"
                echo "LC_SYSTEM:ERROR $MSG"
                exit $ERROR_CODE
        else
                ERROR_CODE=${ERROR_CODE:-$ERRUNKNWN}
                _log "FATAL: Unkown Error occurred"
                echo "LC_SYSTEM:ERROR Unkonwn Error occurred"
                exit $ERROR_CODE
        fi
}


Last edited by stomp; 07-12-2016 at 09:15 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
All times are GMT -4. The time now is 04:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy