Function in one-linef and pass arguments in a pipe

I need to declare a function, this function will contain a script, this script cannot be in a file but must be piped. and then, for the script to run, i need to pass arguments to it.

everything has to be on one line. so i'm basically looking for a one-liner

here's what i'm doing:

myfunc () { $MUP=${@} ; set -- "${MUP}" ; MYSCRIPT=$(cat ~/test1.sh) ; echo "${MYSCRIPT}" ${MUP} ; } ; myfunc -I html -U msmith -H hostname

any ideas on how this can be fixed to do what i want?

the solution should work for any script. meaning, the script should be able to run as though it were in a file, even though it's piped/streamed.

You might get by with:

myfunc () { MYSCRIPT=$(cat ~/test1.sh) ; echo "${MYSCRIPT}" "$@" ; } ; myfunc -I html -U msmith -H hostname

depending on what shell you're using, what operating system you're using, and the contents of $HOME/test1.sh , but your 1-liner could be more reliably replaced with:

printf '%s' "$(cat ~test1.sh)";printf ' %s' -I html -U smith -H hostname;echo

Note that there is absolutely nothing in your 1-liner that is piped or streamed. You read text from a file and echo it and some additional arguments you added to the end of your 1-liner (after we get rid of a couple of syntax errors in your function definition).

Furthermore, if your goal is to create a 1-liner to define and execute a function; there is no reason to define the function. Just execute the body of the function.

1 Like

sorry guys, what i meant was:

myfunc () { $MUP=${@} ; set -- "${MUP}" ; echo "${MYSCRIPT}" ${MUP} ; } ; myfunc -I html -U msmith -H hostname

notice how i took removed the cat. so, the variable "MYSCRIPT" is being filled by some other program. my goal is to execute whatever script is in MYSCRIPT and be able to pass whatever arguments are necessary.

---------- Post updated at 04:48 PM ---------- Previous update was at 04:45 PM ----------

the solution should work on all unix systems (linux, aix, solaris, hpux). the OS on which i'm testing this on is Ubuntu.

Whether or not you removed the cat , there are so many things wrong here...

  1. $MUP=${@} : You can't assign a value to a variable expansion. Maybe you meant MUP="$@"
  2. set -- "${MUP}" : With the fix above, this takes all of the arguments that were passed to your function and turns them into a single operand consisting of what had been your command-line arguments with any quotes removed and single spaces inserted between what had been separate arguments. With this statement and the previous statement, it looks like you might be trying to set your positional parameters to the positional parameters used to invoke your function??? Why don't you just use the positional parameters directly in your script???
  3. echo "${MYSCRIPT}" ${MUP} : You said you wanted to execute the contents of MYSCRIPT . This prints the script on multiple lines (if there were multiple lines in the expansion of $MYSCRIPT with the command command-line arguments added to the output of the last line of the script. It makes no attempt to execute the contents of MYSCRIPT . And, depending on the shell and operating system, the echo could significantly change the script as it prints it if there are any escape sequences included or if the 1st character of the expansion of $MYSCRIPT is a minus-sign.

Perhaps you want something more like:

#!/bin/sh
MYSCRIPT='printf "1st line of MYSCRIPT\n"
printf "argument passed to MYSCRIPT: \"%s\"\n" "$@"
printf "last line of MYSCRIPT\n"'
myfunc(){ eval "$MYSCRIPT"; }; myfunc -I html -U msmith -H hostname

Note that the 1st line of the script will have to be changed if anything in the expansion of $MYSCRIPT depends on features of a particular shell that are not supported by /bin/sh on all of the systems where you want to run this script.

DO NOT USE THIS SCRIPT if anything in the expansion of $MYSCRIPT is user supplied. Using eval user-supplied commands gives whoever wrote those commands access to any private data owned by whoever is running this script.

If you run this script, it will produce the output:

1st line of MYSCRIPT
argument passed to MYSCRIPT: "-I"
argument passed to MYSCRIPT: "html"
argument passed to MYSCRIPT: "-U"
argument passed to MYSCRIPT: "msmith"
argument passed to MYSCRIPT: "-H"
argument passed to MYSCRIPT: "hostname"
last line of MYSCRIPT
1 Like