Bash statement equivalent


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash statement equivalent
# 1  
Old 10-16-2014
Bash statement equivalent

Hi all,

i need a equivalent for the statement i run in bash, so it would also run in other shells. Specially i need it for ksh to run on AIX.
Here the statements:

exec > >(tee -a $log)
exec 2> >(tee -a $log >&2)


Thanks.
# 2  
Old 10-16-2014
The 'script' command may be what you are looking for. It's usually used from outside a script.
# 3  
Old 10-16-2014
You are right, but that's not what i'm looking for. The statement i posted i use inside the script, actually this are the first lines of the script.
# 4  
Old 10-16-2014
I'm not sure this ever did quite what you thought. It could certainly change the perceived order of stdout/stderr. The two different, separate tees are liable to stomp on each other's lines. And this may interfere with redirections run outside the script, too. It's only a strange concidence of how bash works that gives this the appearance of working under some circumstances, but it also did weird things like catching prompts...

Potentially changing the order stdout/stderr come in order is inevitable when doing this, and doing this reliably is always cumbersome since it's supposed to be done from the outside.

This is the shortest "reliable" one I've yet seen managed:

Code:
#!/bin/ksh

log=ksh.log

rm -f $log /tmp/$$-e /tmp/$$-o /tmp/$$-m
mkfifo /tmp/$$-o /tmp/$$-e /tmp/$$-m || exit 1

(       tee /tmp/$$-m /dev/tty < /tmp/$$-o & # will wait for exec >
        tee /tmp/$$-m /dev/tty < /tmp/$$-e & # will wait for exec 2>
        cat < /tmp/$$-m > $log &             # will wait for $$-m
        ) >/dev/null

exec > /tmp/$$-o        # First tee will start running
exec 2>/tmp/$$-e        # Second tee will start running
while [ ! -e "$log" ] ; do true ; done
rm -f /tmp/$$-o /tmp/$$-e /tmp/$$-m

echo "stdout stuff"
echo "stderr stuff">&2

There's shorter/simpler versions using tail -f, to just print the output file instead of catching it midway, but it's possible for them to skip output and hang, not to mention all your output ends up going to stdout. The worst this one will do is reorder stderr/stdout and possibly print a line on your prompt after it quits -- unfortunately inevitable unless you're prepared to make a lot of guarantees about what your code will and won't be doing with traps and stdin/stdout/stderr. Right now this shouldn't interfere with those.

Last edited by Corona688; 10-16-2014 at 03:56 PM..
# 5  
Old 10-16-2014
You can trade off one less fifo for one more subshell:

Code:
#!/bin/ksh

log=ksh.log

rm -f $log /tmp/$$-e /tmp/$$-o
mkfifo /tmp/$$-o /tmp/$$-e || exit 1

(       (       cat < /tmp/$$-o & # will wait for exec >
                cat < /tmp/$$-e & # will wait for exec 2>
                wait        ) | tee $log ) >/dev/tty &

exec > /tmp/$$-o        # First cat will start running
exec 2>/tmp/$$-e        # Second cat will start running
rm -f /tmp/$$-o /tmp/$$-e # Both ends guaranteed open now, so delete as early as possible

echo "stdout stuff"
echo "stderr stuff">&2


Last edited by Corona688; 10-16-2014 at 04:16 PM..
# 6  
Old 10-17-2014
Thanks, nice sugestions. I decided to go the real simple way and just wrap my code in () and add 2>&1 | tee $log at the very end.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] If statement in bash

I have the following code in bash, however "set red frmt" is not displayed. echo "iarg_rd = $iarg_rd" iarg_rd="2" if ; then echo "Hello World" fi if ; then frmt="${gap}${!frmt_titl_yl}" elif ; then frmt="${gap}${!frmt_titl_bk}" elif ; then echo... (2 Replies)
Discussion started by: kristinu
2 Replies

2. Shell Programming and Scripting

BASH - case statement

Hi Gurus, I have the below BASH code which does not works for upper case alphabets except Z (upper case Z). What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working. case $var in ) echo "Lower case alphabet" ;; ... (7 Replies)
Discussion started by: GaneshAnanth
7 Replies

3. UNIX for Dummies Questions & Answers

Bash - OR within an IF statement

Hey guys, Currently trying to write a wee script that runs only when logged in as one of two users. The rest of the script is working fine, but no matter what user I try to run it as, it always fails! This is the puzzling part:if ]; then echo "Run script as admin " exit 1 else... (6 Replies)
Discussion started by: jimbob01
6 Replies

4. Shell Programming and Scripting

bash if statement help needed

Hi I need a script with an if statement that goes. I need it to search through all files within a directory with the extension .test if it finds the string '71502FSC1206' then do sed 's/71502FSC1206/\n&/g' > send.test If it finds the string '715MCH' or '715JAC' then I need it to move the... (1 Reply)
Discussion started by: firefox2k2
1 Replies

5. UNIX for Dummies Questions & Answers

Conditional statement in bash

I want to combine 2 conditional statements by using -o in bash, but it won't work. if ; then echo "The number needs to be between 0 and $nr" fi Each time i execute the file it says: ./selectCitaat: line 10: syntax error near unexpected token `$1' (3 Replies)
Discussion started by: doc.arne
3 Replies

6. Shell Programming and Scripting

Regular expression matching in BASH (equivalent of =~ in Perl)

In Perl I can write a condition that evaluates a match expression like this: if ($foo =~ /^bar/) { do blah blah blah } How do I write this in shell? What I need to know is what operator do I use? The '=~' doesn't seem to fit. I've tried different operators, I browsed the man page for... (3 Replies)
Discussion started by: indiana_tas
3 Replies

7. Shell Programming and Scripting

[tcsh2bash] Tab completion - 'enhanced' equivalent in bash?

I really like tcsh's: set completion='enhanced' because it treats underscores and dash signs the same. I don't have to reach for the shift key when trying to complete files that have underscores. As far as I know, there is nothing like this in bash. Does such a thing exist in bash? If... (2 Replies)
Discussion started by: sarnobat
2 Replies

8. Shell Programming and Scripting

Bash equivalent of perl's pack function ?

Is there an equivalent of perl's pack function in bash ? Or in other words, how can I achieve the same thing in bash ? Much appreciated. (1 Reply)
Discussion started by: NewDeb
1 Replies

9. Shell Programming and Scripting

what is ksh equivalent of bash echo -n ?

Hi folks, I need to stop printing a new line after echoing a string in KSH. i know bash provides echo -n "string" what is the ksh equivalent for this ? (3 Replies)
Discussion started by: mudhireddy
3 Replies

10. Shell Programming and Scripting

Perl equivalent of ksh if / echo statement

Is there an equivalent perl statement for the following ksh statement ? example if then ... else ... fi (2 Replies)
Discussion started by: gefa
2 Replies
Login or Register to Ask a Question