Insane redirection behavior


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insane redirection behavior
# 1  
Old 10-02-2012
Insane redirection behavior

Hi guys,

I know computers don't misbehave.
But I'm puzzled by what's happening right know in a script :

I simplified the example to point out what seems weird to me.
Don't try to find any sense to this stupid script.

There are 10 rows in /tmp/tmp.txt

Code:
i=0
tmpfile=/tmp/tmp.txt
while read -r; do
    echo "$REPLY" > /dev/null
    sed "1,$((++i))d" $tmpfile | wc -l
done < $tmpfile

Code:
i=0
tmpfile=/tmp/tmp.txt
while read -r; do
    echo "$REPLY" > /dev/null
    sed "1,$((++i))d" $tmpfile > /tmp/output; wc -l /tmp/output
done < $tmpfile

In the first script, I count line from the output of sed. In the second one, I first output sed to a file and then count the lines of that file.
I see no reason why the output would be different... YET:
Code:
root@debian:~# ./script1
9
9
9
9
9
9
9
9
9
9
root@debian:~# ./script2
9 /tmp/output
8 /tmp/output
7 /tmp/output
6 /tmp/output
5 /tmp/output
4 /tmp/output
3 /tmp/output
2 /tmp/output
1 /tmp/output
0 /tmp/output

This is driving me crazy.
Do you have any sane explanation?!?

Regards
Santiago
# 2  
Old 10-02-2012
It should be easier if you also post the input file and the desired output.
# 3  
Old 10-02-2012
The first script is running sed in a subshell environment; the second is not. The subshell is a byproduct of the shell's pipeline implementation. The difference is that when $i is incremented in a subshell, the new value is not available in the parent shell.

One workaround would be to increment the variable prior to calling sed.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 4  
Old 10-02-2012
Quote:
Originally Posted by Franklin52
It should be easier if you also post the input file and the desired output.
The input file is random. It just contains 10 rows but we don't care what they are.
The output should be counting down from 9 to 0.

---------- Post updated at 16:40 ---------- Previous update was at 16:23 ----------

Hi alister,

You were right.
Here are the two new scripts...
Code:
i=0
tmpfile=/tmp/tmp.txt
while read -r; do
    echo "$REPLY" > /dev/null
    ((++i))
   sed "1,${i}d" $tmpfile | wc -l
done < $tmpfile

Code:
i=0
tmpfile=/tmp/tmp.txt
while read -r; do
    echo "$REPLY" > /dev/null
    ((++i))
    sed "1,${i}d" $tmpfile > /tmp/output; wc -l /tmp/output
done < $tmpfile

... and their output...
Code:
root@debian:~# ./script1
9
8
7
6
5
4
3
2
1
0
root@debian:~# ./script2
9 /tmp/output
8 /tmp/output
7 /tmp/output
6 /tmp/output
5 /tmp/output
4 /tmp/output
3 /tmp/output
2 /tmp/output
1 /tmp/output
0 /tmp/output

Regards
Santiago
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Behavior

Linux Release Uname details Data file Ive been at the command line for some time. Back as far as SCO and Interactive Unix. I have always used this construct without issues. I want to isolate the ip / field 1. As you can see .. the first line is "skipped". This works as... (6 Replies)
Discussion started by: sumguy
6 Replies

2. Solaris

Installing software -going insane

I have Solaris 10 update 11 installed on a virtual machine which I use for my lunix class (I'm studying I.T. but have little unix experience). So I have root access* I'm also do C programming in my course and would love to do it on my Solaris machine. So I looked into installing GCC and... (5 Replies)
Discussion started by: goformickey
5 Replies

3. UNIX for Advanced & Expert Users

Different redirection behavior in BASH/Linux when run under cron vice login ???

run_xfs_fsr is a xfs filesystem maintenance script designed to run under cron. The system is a home theater personal computer running mythbuntu 10.10, and is accessed remotely for these tests. cron runs a script, (xfs_fsr.sh) at 02:30 that runs the subject script under BASH and sets the... (3 Replies)
Discussion started by: keepitsimpleeng
3 Replies

4. Programming

Strange behavior in C++

I have the following program: int main(int argc, char** argv){ unsigned long int mean=0; for(int i=1;i<10;i++){ mean+=poisson(12); cout<<mean<<endl; } cout<<"Sum of poisson: "<< mean; return 0; } when I run it, I get the... (4 Replies)
Discussion started by: santiagorf
4 Replies

5. UNIX for Dummies Questions & Answers

TTY Insane

Hi All, Long time reader, first time poster. I have a tip session to a v480 serial console running Solaris 9. Look at this mess I'm getting back... connected... (16 Replies)
Discussion started by: danny.hudson
16 Replies

6. Shell Programming and Scripting

Echo behavior

Echo is removing extra blank spaces. See the command. export INSTALLDIR=”First Second /Two Space” echo $INSTALLDIR out put: First Second /Two Space Here only on blnak space is present while with command Echo “$INSTALLDIR” Out put: ”First Second /Two Space” It's correct output... (2 Replies)
Discussion started by: Saurabh78
2 Replies

7. Shell Programming and Scripting

Very Strange Behavior for redirection

I have searched far and wide for an explanation for some odd behavior for output redirection and haven't come up with anything. A co-worker was working on old scripts which have run for years and embedded in their code were output redirects which worked for the script during execution and then... (5 Replies)
Discussion started by: cahook
5 Replies

8. Shell Programming and Scripting

sed behavior on hp-ux

the sed command: sed 's/^*//' file does not work on HP-UX :-( but it works fine on Linux, content of file: <tab><tab>hello output should be: hello Any ideas?? Thank you Andy (8 Replies)
Discussion started by: andy2000
8 Replies

9. UNIX for Dummies Questions & Answers

Insane question : Playing mp3 file from Sun Sparc

hi, i know this is crazy. but i can do this from linux in intel platform. just wonder can my Sun Solaris 8 sparc can do the same thing? (2 Replies)
Discussion started by: champion
2 Replies

10. Programming

ls behavior

I put this here because it is a 'behavior' type question.. I seem to remember doing ls .* and getting all the .-files, like .profile .login etc. But ls .* doesn't do that, it lsts the contents of every .*-type subdirectory. Is it supposed to? I should think that a -R should be given to... (10 Replies)
Discussion started by: AtleRamsli
10 Replies
Login or Register to Ask a Question