Sponsored Content
Full Discussion: Bash statement equivalent
Top Forums UNIX for Dummies Questions & Answers Bash statement equivalent Post 302921358 by Corona688 on Thursday 16th of October 2014 02:47:45 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
RBASH(1)						      General Commands Manual							  RBASH(1)

NAME
rbash - restricted bash, see bash(1) RESTRICTED SHELL
If bash is started with the name rbash, or the -r option is supplied at invocation, the shell becomes restricted. A restricted shell is used to set up an environment more controlled than the standard shell. It behaves identically to bash with the exception that the follow- ing are disallowed or not performed: o changing directories with cd o setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV o specifying command names containing / o specifying a filename containing a / as an argument to the . builtin command o specifying a filename containing a slash as an argument to the -p option to the hash builtin command o importing function definitions from the shell environment at startup o parsing the value of SHELLOPTS from the shell environment at startup o redirecting output using the >, >|, <>, >&, &>, and >> redirection operators o using the exec builtin command to replace the shell with another command o adding or deleting builtin commands with the -f and -d options to the enable builtin command o using the enable builtin command to enable disabled shell builtins o specifying the -p option to the command builtin command o turning off restricted mode with set +r or set +o restricted. These restrictions are enforced after any startup files are read. When a command that is found to be a shell script is executed, rbash turns off any restrictions in the shell spawned to execute the script. SEE ALSO
bash(1) GNU Bash-4.0 2004 Apr 20 RBASH(1)
All times are GMT -4. The time now is 09:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy