Function in one-linef and pass arguments in a pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function in one-linef and pass arguments in a pipe
# 1  
Old 04-29-2016
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:

Code:
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.
# 2  
Old 04-29-2016
You might get by with:
Code:
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-29-2016
sorry guys, what i meant was:

Code:
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 ----------

Quote:
Originally Posted by Don Cragun
You might get by with:
Code:
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:
Code:
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.
the solution should work on all unix systems (linux, aix, solaris, hpux). the OS on which i'm testing this on is Ubuntu.
# 4  
Old 04-29-2016
Quote:
Originally Posted by SkySmart
sorry guys, what i meant was:

Code:
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:
Code:
#!/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:
Code:
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

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cat a script, pipe it, then pass arguments to it?

suppose i have a perl script that is normally run this way: ./checkdisk.pl -H hostname -w 40 -c 80 but, for whatever reason, i cannot run the script directly as it should. But i can cat it through pipe. How can i pass the arguments "-H hostname -w 40 -c 80"? so this is what i'm doing,... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. Shell Programming and Scripting

Pass the value from pipe

am trying to pass the date calculated to variable so that i can use the same for my further reporting and tracking of the changes as per the variable value. System = SunOS Release = 5.9 KernelID = Generic_122300-61 date '+%m %d %Y' | { read MONTH DAY YEAR DAY=`expr "$DAY" - 1` case... (5 Replies)
Discussion started by: pradeep84in
5 Replies

3. Shell Programming and Scripting

How to pass arguments to an interactive script?

I've tried to find a solution to this, but I"m stumped. OS - RH Linux 5.9 I have a bash script <myProgram.sh> that gets executed manually and creates a small response file. I'm trying to automate the running of this script. When myProgram.sh runs, it prompts the user to enter 2 pieces of... (13 Replies)
Discussion started by: progkcp
13 Replies

4. UNIX for Dummies Questions & Answers

Pass arguments to the library .so

Hello, Please, how can i pass arguments to my lib.so ? my lib.so is written in c and i need some arguments in the code .. LD_PRELOAD=lib.so ./program Thank you. (1 Reply)
Discussion started by: chercheur857
1 Replies

5. Shell Programming and Scripting

To pass arguments to makefile using script

Hi, I want to run a target of makfile using script by passing different arguments to it again n again. I i need to grep certain things from the log file. eg make abc KAB=8 BAC=8 >& KAB_BAC.log grep "timeA" KAB_BAC.log grep "timeB" KAB_BAC.log (i want to store the difference of the two time... (0 Replies)
Discussion started by: vdhingra123
0 Replies

6. Shell Programming and Scripting

pass arguments unchanged

Hi, I have to use ksh on HP-UX for some scripting. I usually use "set -e -u" in scripts to stop if errors occur or a typo is in a variable name. Now I try to use "$@" to pass the arguments unchanged to another function, which works without problems - unless I try to call the script without... (7 Replies)
Discussion started by: michas
7 Replies

7. Shell Programming and Scripting

need help to pass arguments in script

Hi, I have a my script here-- print "The Perl Script does the User health check and system health check...\n"; print "---------------------------------------------------------------------\n"; # use strict; my($OS); $OS = $^O; # need to test @ARGV before GetOptions shifts it if (@ARGV... (1 Reply)
Discussion started by: namishtiwari
1 Replies

8. UNIX for Advanced & Expert Users

Pass Kill with arguments

Dude, I want to kill a process, but the processid is in a text file. I have to read the text file for the process id and pass it as parameter to the kill command. Example $ cat prcid.txt 18650 I want to pass the value 18650 as a process id to kill command. $ kill -9 <value read from... (4 Replies)
Discussion started by: aksmuralee
4 Replies

9. Shell Programming and Scripting

pass arguments to called program

Thank you very much. (2 Replies)
Discussion started by: ShellUser
2 Replies

10. Shell Programming and Scripting

How to pass arguments to a function in a shell script?

Hi, I have two shell variables $t1 and $t2 which I need to pass to a function in a shell script. The function will do some computation with those two variables and echo the resultant. But I do not know how to pass teh arguments. The function written is f1() {...... ........ } What should... (3 Replies)
Discussion started by: preetikate
3 Replies
Login or Register to Ask a Question