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:
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:
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:
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:
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:
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:
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.
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...
$MUP=${@}: You can't assign a value to a variable expansion. Maybe you meant MUP="$@"
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???
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:
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:
This User Gave Thanks to Don Cragun For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)