ksh bug?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users ksh bug?
# 1  
Old 11-20-2013
ksh bug?

I think I discovered a bug in ksh.
Code:
$ cat test.sh
x=`echo hello | cat 2>&1 >/dev/null`
echo $x
y=`echo hello | cat >/dev/null 2>&1`
echo $y

works with all shells, including ksh in normal mode.
But does not work with ksh -x:
Code:
$ ksh -x test.sh
+ + echo hello
+ cat
+ 2>& 1 x=1> /dev/null
+ echo 1> /dev/null
1> /dev/null
+ + echo hello
+ cat
+ 1> /dev/null 2>& 1
y=
+ echo

sh -x or bash -x or zsh -x do it correctly:
Code:
$ bash -x test.sh
++ echo hello
++ cat
+ x=
+ echo

++ echo hello
++ cat
+ y=
+ echo

# 2  
Old 11-20-2013
Quote:
Originally Posted by MadeInGermany
I think I discovered a bug in ksh.
Code:
$ cat test.sh
x=`echo hello | cat 2>&1 >/dev/null`
echo $x
y=`echo hello | cat >/dev/null 2>&1`
echo $y

works with all shells, including ksh in normal mode.
But does not work with ksh -x:
Code:
$ ksh -x test.sh
+ + echo hello
+ cat
+ 2>& 1 x=1> /dev/null
+ echo 1> /dev/null
1> /dev/null
+ + echo hello
+ cat
+ 1> /dev/null 2>& 1
y=
+ echo

sh -x or bash -x or zsh -x do it correctly:
Code:
$ bash -x test.sh
++ echo hello
++ cat
+ x=
+ echo

++ echo hello
++ cat
+ y=
+ echo

I guess I don't see what's wrong.

It looks like ksh is running the parts of the pipelines in different threads (so you see jumbled output in the trace), but both x and y are assigned empty values and the echo $x and echo $y seem to have done the right thing. What am I missing?

If you mean that the trace output from bash and zsh don't show redirection operations performed by the shell, I don't think that is a ksh bug.

Last edited by Don Cragun; 11-20-2013 at 05:53 PM.. Reason: Add final comment on redirections.
# 3  
Old 11-20-2013
x definitely gets the value "1> /dev/null" assigned when run with "ksh -x".
The lines without a + are actually printed on the screen.

I had worked on a script where a "ksh -x" run produced a total different result, compared to a normal run with "ksh".
Here is another variant of the above script:
Code:
$ cat test.sh
x=`echo hello | cat 2>&1 >/dev/null`
if [ -n "$x" ]; then
  echo WRONG
else
  echo RIGHT
fi
y=`echo hello | cat >/dev/null 2>&1`
if [ -n "$y" ]; then
  echo WRONG
else
  echo RIGHT
fi

Code:
$ ksh test.sh
RIGHT
RIGHT

Code:
$ ksh -x test.sh
+ + echo hello
+ cat
+ 2>& 1 x=1> /dev/null
+ [ -n 1> /dev/null ]
+ echo WRONG
WRONG
+ + echo hello
+ cat
+ 1> /dev/null 2>& 1
y=
+ [ -n  ]
+ echo RIGHT
RIGHT

# 4  
Old 11-20-2013
OK. With this example, I'm seeking the same problem when using ksh version:
Code:
  version         sh (AT&T Research) 1993-12-28 s+

on OS/X. With this version the trace looks like:
Code:
+ cat
+ echo hello
+ 2>& 1 + x='1> /dev/null'
+ [ -n '1> /dev/null' ]
+ echo WRONG
WRONG
+ cat
+ echo hello
+ 1> /dev/null 2>& 1
+ y=''
+ [ -n '' ]
+ echo RIGHT
RIGHT

# 5  
Old 11-20-2013
I can confirm the results from post #3 using 93u+ 2012-08-01.

Regards,
Alister

---------- Post updated at 05:43 PM ---------- Previous update was at 04:48 PM ----------

Taking a closer look at this, and experimenting with various oneliners, I don't think this is a bug.

ksh writes the trace to stderr and stderr is redirected by the scripts above.

Code:
$ bash -xc 'echo foo 2>&1 >/dev/null bar' > stdout
+ echo foo bar
$ cat stdout
$
$ ksh -xc 'echo foo 2>&1 >/dev/null bar' > stdout
+ echo foo bar
+ 2>& 1 $ cat stdout
1> /dev/null

bash doesn't trace redirections. ksh does. Redirections are set aside during parsing and are processed after the other command steps. Just before processing the 2>&1 redirection, ksh prints it to stderr. Then stderr is redirected. Further redirections appear on stdout. In my example, that's the file named stdout; in MadeInGermany's script, it is the pipe leading from the command substitution to the parent shell.

Regards,
Alister

Last edited by alister; 11-20-2013 at 08:03 PM..
These 5 Users Gave Thanks to alister For This Post:
# 6  
Old 11-21-2013
Then let's call it a shortcoming.
It looks like -x mode uses stderr, and can conflict with redirection in the shell code.
Then, why stderr and not another file descriptor?
Maybe that was not available on all Unix system 1988?
# 7  
Old 11-21-2013
Quote:
Originally Posted by MadeInGermany
Then let's call it a shortcoming.
It looks like -x mode uses stderr, and can conflict with redirection in the shell code.
Then, why stderr and not another file descriptor?
Probably because that's exactly what stderr is there for. Being 'clever' about it would make obvious things, like redirecting a trace's output into a file, difficult. A shell can't make too many assumptions about what you intend to do with it.

You, the programmer however, are allowed to make more assumptions. Why were you using the same file descriptor as the debugger?

Last edited by Corona688; 11-21-2013 at 03:30 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

is this a bug of g++?

Hello, Im using the g++(g++ Ubuntu/Linaro 4.4.4-14ubuntu5 4.4.5) and im trying to compile a small snippet code and got into an endless loop.I recompiled that in VS2010 under Windows 7 and the answer is as expected.so i wonder is this a bug of g++?here is my code. #include<iostream> using... (5 Replies)
Discussion started by: homeboy
5 Replies

2. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

3. UNIX for Dummies Questions & Answers

where's the bug?

#!/bin/bash if then #echo "infinite loop" exit 0 fi when I run this file I get the following error: ./test_infinite_loop: line 5: syntax error near unexpected token `fi' ./test_infinite_loop: line 5: `fi' :confused: (4 Replies)
Discussion started by: jon80
4 Replies

4. AIX

bug in 43 ???

xxxxserver# lsattr -El inet0 | grep 255.240.0.0,32.224.0.0,32.78.120.254 | grep '.40' route net,-hopcount,1,-netmask,255.240.0.0,32.224.0.0,32.78.120.254 How this is possible? (1 Reply)
Discussion started by: itik
1 Replies

5. Shell Programming and Scripting

Is it a bug ..?

Hi All, I am using Red Hat Linux on my servers. The problem that I am facing is, sometimes the /opt usage on the server shows used percentage as 100% , when actually it is simply 20%. When I reboot the system, it comes back to 20%.Is this a bug in the system or my settings have gone wrong... (1 Reply)
Discussion started by: nua7
1 Replies

6. UNIX for Advanced & Expert Users

ksh read bug in HPUX 11i

Is this a bug in ksh on HPUX 11i or is read impromperly documented? INPUT Thu Jan 18 09:14:52 PST : CIFS: Virus Detected - File ONTAP_ADMIN$\vol\vol0\DDD\Ventana\Strattoni\Race Stuff\Rumor.exe in share DDD accessed by client CLIENT (111.11.11.111) running as user USER is infected. The filer... (3 Replies)
Discussion started by: Jackaroe
3 Replies

7. UNIX for Dummies Questions & Answers

Sed bug in OS X?

I've been playing around with sed for a few days now and find that regular expressions never work inside sed s/// on Mac OS 10.2.5. Has anyone else bumped up against this problem? For example, the "sed" segment of the following always fails no matter how items are escaped: cd... (12 Replies)
Discussion started by: ktoz
12 Replies
Login or Register to Ask a Question