Read input and output redirection filename within a script
Hello everyone,
My requirement is that within a script I need to construct the command line exactly that it was invoked with. For example :
Now within a.sh, I construct a file which has these contents "
With positional parameters, I can build the argument list but the input and output redirection filenames get lost.
Essentially, a.sh can be invoked with or without "<" , ">". I remember I was able to do it once in the past using read command. Struggling to recollect it now.
I need this because I want to overload a.sh. Our scripts call it umpteen number of times. So, I need to avoid changing those calls and still make a.sh behave differently.
Any help whall be greatly appreciated.
Last edited by Scott; 09-18-2010 at 03:34 PM..
Reason: Please use CODE tags
Judging from the number of times this post has been read without response. I am not alone in not understanding the problem.
Please expand your post, explaining carefully what you are trying to do, and where you need help. An example or two would help. Please make it clear which parameters are variable and which parameters are fixed.
Btw. We would normally assume that multiple references to say "a.sh" means the same script. A recursive call to the same script will eventually fail. How deep you can go with a recursive call will depend on the Operating System and the Shell which you are using.
Thanks for the replies. Ok. So the requirement is, I need to write a shell script such that it builds the parameter list with which it was invoked including any input or/and output redirection files, if any. So let's say a.sh is invoked as "sh a.sh arg1 arg2 < inp.dat", it should be able to build this argument list and store it in an usable variable or write it to another file. The contents being simply the invocation line - "sh a.sh arg1 arg2 < inp.dat". If say its invoked as "sh a.sh arg1 arg2", (the simplest case), it should be able to write this invocation line ("sh a.sh arg1 arg2") into a file.
Lets assume that we always want the "output" in another file. So, if the script is invoked as "sh a.sh arg1 arg2 < inp.dat > out.log", let it produce a file result.txt with contents "sh a.sh arg1 arg2 < inp.dat > out.log".
Hope am clear this time. I can understand the requirement is kind of weird!
Thanks again.
---------- Post updated at 01:25 PM ---------- Previous update was at 01:20 PM ----------
Missed adding in the last post that my only challenge it is to "read" the input and output redirection file name within the script. Otherwise, its simply looping through the parameter list. Pardon my ignorance!
Why I need it is, I have an automation framework that uses a.sh. Now, I do not want to change the original script but come up with a duplicate that does what I have asked for and enables me to make it behave in a different way.
Am sure the best way would be to change the original a.sh and make it handle all the cases. But that's a constraint for me.
Hi All
Please Help
Read input write multply output with creteria
Exemple i have file
abc 111 444
abc 111 444
def 111 444
def111 444
bbb 111 444
bbb 111 444
i would need write 3 files pos 1-3 is the Criteria
output would be file1 contains abc file2 def file3 bbb
... (3 Replies)
I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix.
I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice.
Files:... (3 Replies)
Hi,
when I try to redirect input and the command is described as a string within an array redirection does not work. why?
#!/bin/bash
dir=("tail < ./hello.txt")
tail < ./hello.txt #works
${dir} #does not work (2 Replies)
Hi,
#!/bin/bash
while ;
do
rm -f /tmp/pipe
mkfifo /tmp/pipe
./yuv4mpeg_to_v4l2 < /tmp/pipe &
mplayer tom_and_jerry.mp4 -vf scale=480:360 -vo yuv4mpeg:file=/tmp/pipe
sleep 65;
done
When I run this - after mplayer finishes playing video it says - Exiting... (End of... (2 Replies)
I have a directory where i have *.sas; *.pl;*.sh and *.c scripts
I need to find out what are the child scripts and input output files for each script:
say I have a shell script which calls a perl script and a sas script:
In my first line I want
I a) the parent script name;
b) the... (1 Reply)
I know this is a simple matter, but I'm new to this.
I have a shell script that calls a sed script from within it. I want the output of the shell script to be based on the input file I pass as an argument to the original script. In other words...
./script.sh file.txt
(script.sh calls sed... (2 Replies)
All,
I have a requirement to write a script where I check for Input redirection when the script was executed, based on which I handle my logic. Below is the example:
my.script
#! /bin/ksh
# Not sure how to frame the if condition below
if ; then
echo "Input Redirected from a file"
... (7 Replies)
Can you help me ?
I want to write a program ,which can open a input file (input.txt) and run as child process ,then write to output file (output.txt).......
char inFile="input.txt";
char outFile="output.txt";
int main(int argc, char **argv)
{
pid_t pid=1;
int no=0; // no. of... (5 Replies)