0

Yesterday I asked also this question. Thanks for all the answers. But I still don't get it.

I did

$ exec 6<&0 0</tmp/lines.txt

I thought: My standard input is redirected now. But the lines in the file are also executed! So is there a default action when you only do this? This is my question.

Let's say

$ cat /tmp/lines.txt 
read i j
read i j
echo "i:$i,j:$j">/tmp/res.txt

I do the exec, and after that I do

$ cat /tmp/res.txt 
i:read,j:i j

But when I do

$ exec 6<&0 0</tmp/lines.txt;exec 0<&6 6<&-

nothing is read or executed. I can read the lines but now I have to give the action myself,

$ exec 6<&0 0</tmp/lines.txt ;while read i j;do echo "i:$i,j:$j";done

I see in my terminal,

i:read,j:i j
i:read,j:i j
i:echo,j:"i:$i,j:$j">/tmp/res.txt

Thanks again, I hope it is not too long,

Eric J.

ericj
  • 2,138
  • 27
  • 44
  • In interactive mode, bash reads commands from standard input. Is that what you mean by a default action? What are you trying to achieve? – cdarke Sep 10 '13 at 11:02

1 Answers1

1
exec 6<&0 0</tmp/lines.txt; exec 0<&6 6<&-

Turns out that bash still executes what remains in the command specified or script before it reads input for commands again that's why your file /tmp/lines.txt is not read since exec 0<&6 6<&- is run before it.

Please refer back to the previous solution if you want to execute commands in /tmp/lines.txt before exec 0<&6- is executed.

Also it might have not been obvious but you could use . or source to achieve same output:

. /tmp/lines.txt
source /tmp/lines.txt
Community
  • 1
  • 1
konsolebox
  • 72,135
  • 12
  • 99
  • 105