Is there a way to add two execs to one script? For instance, I need to redirect the stdout and stderr to two separate directories. I want to do this:
But obviously only the last exec is being used. Any tricks?
Actually there are - but not using exec. Let me first explain the use of exec, then what you can do to fulfill your requirement.
The keyword exec is used for two very different purposes: the first is to replace (instead of fork) the current process. Consider this script part:
What happens when the shell executes this is:
1) create a subprocess and start command1 in this
2) wait until this subprocess has finished
3) create another subprocess and start command2 in it
etc..
As long as it takes i.e. command1 to run you would see the process of your script (say, yourscript.sh) in the process list as parent process and command1 as child process. exec instead will replace the running script (effectively stopping its execution) with the command in question, which will take over the process number and everything else of the - up to now - running script. That means:
would be pointless because after the first line being executed the script is not running any more and hence command2 will never be executed. So, in this sense, two exec in one script would not make sense - except, of course, if there is some condition attached to it:
The second purpose - without any command but a redirection instead - of exec is to redirect I/O as you have done. The problem regarding your goal is not so much exec itself but a property of UNIX processes: processes have (per default) one input - <stdin> - and two outputs: <stdout> and <stderr>. Any of these "I/O descriptors", as the correct wording is, can point to any source providing a data stream: a file, a device, a pipeline, ... exec > /some/where is just a fancy way of directing the <stdout> I/O descriptor to that certain file.
You can use several such execs to re-redirect these streams to various sources/targets, but each such redirection will overwrite the previous one. Not because of the way exec works, but because each I/O-descriptor can either point this way or that way, but not both. Picture a UNIX process as a sort-of garden hose: you fill something in at one end and something comes out on the other. You can use what comes out to fill this bucket or that bucket, but not both at the same time.
For such a thing - to fill both buckets at the same time - you would need a "T-piece", as plumbers call it: something to divide the water stream into two water streams. Exactly this analogy inspired the makers of UNIX to create the tee utility. You can use it to duplicate an output stream so that it will go to two different targets at the same time. You cannot use that inside the script but you can use it outside with a redirection:
will write <stdout> of yourscript.sh to both /some/file and /other/file. I suggest you read the man page of tee to get acquainted. If you still need help just ask.
That worked great!!! I do have one additional follow up. I also use a trap command to remove the error file and error directory (if empty) at the end as with exec, they need to be created first.
My current trap is only working for what I have speicfic. How would I add in the a second trap statement? Do I need to do this in a function?
Moderator's Comments:
Please use CODE tags as required by forum rules!
Last edited by RudiC; 02-12-2019 at 02:15 PM..
Reason: Changed QUOTE to CODE tags.
I also use a trap command to remove the error file and error directory (if empty) at the end as with exec, they need to be created first.
My current trap is only working for what I have speicfic. How would I add in the a second trap statement?
A trap is code which is executed when a script receives a certain signal (like, via the kill command or via the keyboard, i.e. pressing <CTRL>-<C>). Since each signal can only have one reaction attached to it you cannot specify two traps for the same signal although you can specify traps for different signals.
Notice that - depending on your shell - there might also be a "pseudo-signal" raised when leaving a script via the normal exit. This is no real signal at all but it can be trapped the same way any other signal can. In bash this is done like this:
The function myexit() will be executed when the script is left through the exit command.
I have to create two instances of jBoss 5.1.0 GA. In order to do that I have to execute the following in start-jboss.sh:
find . -exec /opt/novell/idm/jboss/bin/run.sh -Djboss.service.binding.set=ports-01 -c IDMProv -b 0.0.0.0 \; -exec /opt/novell/idm/jboss/bin/run.sh... (4 Replies)
Hi all,
Am wanting to do a ls -l of the files and do a cat of it at the same time, ideally, I am hoping that the following work but obvisouly it is not working to what I am wanting it to ... hu hu hu :wall:
find . -name "BACKUP_TIMESTAMP.log" -exec "ls -l basename {} ; cat {}" \;
... (1 Reply)
I have the following bash script lines in a file named test.sh.
#!/bin/bash
#
# Write Date to cron.log
#
echo "Begin SSI Load $(date +%d%b%y_%T)"
#
# Get the latest rates file for processing.
#
d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1)
filename=$d
export filename... (3 Replies)
Hi Gurues,
I need to modify an existing script that uses find to search a folder, and then move its contents to a folder. What I need to do is run gzip on each file after it's moved.
So, I ran this little test:
Put a ls.tar file on my $HOME, mkdir tmp, and then:
virtuo@tnpmprd01: find .... (3 Replies)
I can't get this to work. Running a single command works fine:
find . -name "*.dat" -exec wc -l '{}' \;
gives me the file name and number of lines in each .dat file in the directory.
But what if I want to pipe commands, e.g. to grep something and get the number of lines with that pattern... (3 Replies)
Hi,
I have to run two commands one after another from a c program. How can i do this with exec system calls. i tried giving them as argument to execv but it is not working.please help
thanks (3 Replies)
Hi ,
I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell.
So scenario is that
- I need to have one shell script which is ran as a part of crontab
- in this shell script I need to do a... (2 Replies)