How to get redirected filename inside unix script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to get redirected filename inside unix script
# 1  
Old 09-08-2010
How to get redirected filename inside unix script

Hi All,

I am having a script which calculate checks the input feed and perform some function. When i am executing this script i am redirecting this to a output file. I want to know the redirected output file name inside my scripts. Is there is any way to get that .

like the same way we used to get the argument as $1 and so on


Thanks,
Arun
# 2  
Old 09-08-2010
Probably no good portable way across *ix. But if you have lsof you can use the pid of the current running process (stored as $$) and do something like:

Code:
lsof -p $$ -a -d 1

That says find open files for the current process and (-a) only those associated with file descriptor 1.

Just one soln (maybe)...

Last edited by Scott; 09-08-2010 at 07:40 PM.. Reason: Added code tags
# 3  
Old 09-08-2010
that really does not make sense. handle I/O inside the script if you need to(see exec). Don't mess with output files handled outside your script or strange things can occur.
# 4  
Old 09-08-2010
Or perhaps:

Code:
lsof -p 12220 -a -d 1 -F n

To get something easily parsed (grab the line beginning with "n")

Code:
lsof -p 12220 -a -d 1 -F n | sed -n 's/^n//p'


Last edited by Scott; 09-11-2010 at 08:41 AM.. Reason: Please use code tags
# 5  
Old 09-08-2010
I don't suggest using lsof. It might not even be available on the system. If you need the file name pass it as an arugment but I would not write/read to that file inside the script.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX script to display the filename

Hi All How to answer the below interview question.. With a path and filename of "/mydir1/mydir2/mydir3/myfilenane.dat" write a UNIX script to display the filename (2 Replies)
Discussion started by: shumail
2 Replies

2. UNIX for Dummies Questions & Answers

Call a UNIX script inside another and dont wait for it

Hi I have two scripts script1.sh and script2.sh(say this script is a long running). I want to call script2.sh inside and script1.sh,but when i call script2.sh i dont want to wait for script2 to complete and want this to run in back ground and go on next commands in script 1.sh and finally at the... (2 Replies)
Discussion started by: lijjumathew
2 Replies

3. UNIX for Advanced & Expert Users

Using PHP , call a sql inside a unix script

I am running the xampp on WINDOWS, and my php script is connecting to a unix script on a different server (ssh2_connect("11.31.138.56", 22). I am running the unix script and inside this script I am calling the .sql file . The SQL is connecting to oracle db on the unix server. But the sqlplus... (2 Replies)
Discussion started by: madfox
2 Replies

4. Shell Programming and Scripting

passing arguments to unix command or script inside tclsh

hi everobody kindly consider the following in tclsh I understand that we can do the following %exec UnixCmd arg1 arg2 but if I assinged the arguments to a list insde tclsh how can I use them back i.e %set ArgList %exec UnixCmd %exec Unixcmd $list %exec all the... (1 Reply)
Discussion started by: Blue_shadow
1 Replies

5. Shell Programming and Scripting

Replace Filename and text inside of directory

I have a directory that has directories that contain Dir-20111114-xyz and I want to change them to Dir-20111121-xyz. Inside of Dir-20111114-xyz, I have a config.xml file that also contains the date that I need changed from 20111114 to 20111121 I have used sed to replace inside of file not... (4 Replies)
Discussion started by: icculus99
4 Replies

6. Shell Programming and Scripting

keep mail command active inside a script on unix

well, I have a script with this code inside: nohup /usr/sbin/auditstream | /usr/sbin/auditselect -m -e "event== USER_Create || event== USER_Remove || event== USER_Change || event== GROUP_Create || event== GROUP_Remove || event== GROUP_Change || event== PASSWORD_Change " | /usr/sbin/auditpr -h... (4 Replies)
Discussion started by: iga3725
4 Replies

7. Shell Programming and Scripting

Running a unix script(which is calling another script inside that) in background

Hi all, I am having a script ScriptA which is calling a script ScriptB in the same server and copying files to second server and have to execute one script ScriptC in the second server. THis First script ScriptA is the main script and i have to execute this process continously. for Keeping... (2 Replies)
Discussion started by: rohithji
2 Replies

8. Shell Programming and Scripting

Print filename inside loop

Im want to print filename inside loop .. the code im using :- Filename_1=abc_20090623_2.csv.lk Filename_2=def_20090623_2.csv.lk i want to extract filename till .csv eg Filename_1=abc_20090623_2 Filename_2=def_20090623_2 How can i do this inside the for loop ... (3 Replies)
Discussion started by: r_t_1601
3 Replies

9. Shell Programming and Scripting

Unix commands failing inside the shell script

When my script deals with large input files like 22Gb or 18 GB the basic commands like sort or join fails when run from inside the shell scripts. Can there be any specific reason for this? For e.g. sort -u -t "," -k1,1 a.csv > a.csv.uniq" sort -u -t "," -k1,1 b.csv > b.csv.uniq" The... (3 Replies)
Discussion started by: esha
3 Replies

10. UNIX for Dummies Questions & Answers

Accessing redirected file inside script

hi, Is there a way to access the redirected file inside the script. Here is what the command line looks like: $ shar * > archive_file.arc I know I can't access the name of archive_file.arc with positional parameters like $1, $2.. Is there any way to figure out what file the output of the... (3 Replies)
Discussion started by: milhan
3 Replies
Login or Register to Ask a Question