Read input and output redirection filename within a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read input and output redirection filename within a script
# 1  
Old 09-18-2010
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 :
Code:
sh a.sh arg1 arg2 arg3 < input.txt > output.txt

Now within a.sh, I construct a file which has these contents "
Code:
sh a.sh arg1 arg2 arg3 < input.txt > output.txt"

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
# 2  
Old 09-18-2010
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.
# 3  
Old 09-18-2010
a.sh will not read them as parameters. these are I/O operators that the shell interprets before a.sh is executed.
# 4  
Old 09-18-2010
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!
# 5  
Old 09-18-2010
it's clear but what your asking for does not make sense. it does not work that way.
# 6  
Old 09-18-2010
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.
# 7  
Old 09-18-2010
Shell scripting still doesn't work that way.

If you're not allowed to fix a.sh, replace it, or make some newfangled b.sh.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input write multply output with creteria

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)
Discussion started by: tonyk334
3 Replies

2. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

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)
Discussion started by: pathunkathunk
3 Replies

3. Shell Programming and Scripting

Input redirection within bash script

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)
Discussion started by: heinzel
2 Replies

4. Shell Programming and Scripting

Input redirection script

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)
Discussion started by: ashokvpp
2 Replies

5. Shell Programming and Scripting

Print output and read input on same line

How do I print output and read input on the same line in ksh? echo Hello, what is your name? read name (1 Reply)
Discussion started by: robin_simple
1 Replies

6. Shell Programming and Scripting

Script to read input and output files and child scripts

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)
Discussion started by: ramky79
1 Replies

7. Shell Programming and Scripting

use input filename as an argument to name output file

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)
Discussion started by: estebandido
2 Replies

8. Shell Programming and Scripting

How to check for Input Redirection in my script?

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)
Discussion started by: bharath.gct
7 Replies

9. Programming

read input-process-output

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)
Discussion started by: cupid1575
5 Replies

10. Shell Programming and Scripting

Asking about shell script input output redirection

Hi, Can anyone please tell me what these lines do? ls >& outfile ls outfile 2>&1 Thanks. (1 Reply)
Discussion started by: trivektor
1 Replies
Login or Register to Ask a Question