ambiguous redirect
Linuxmisc » Unix shell
using bash shell unix
this code gives me ambiguous redirect error
IFS=\n
file=filename
exec<$file
while read -r line
do
echo -"$line"-
done
but if i remove the
exec<$file
and replace it with
exec<filename
it works
but doesnt detect spaces before and after words
eg
" sapce before"
"space after "
thanks
Try this:
IFS='
'; # IFS now = \n
--
http://ftp.opensysmon.com is a shell script archive site with an
open source system monitoring and network monitoring software package.
Many platforms are supplied already compiled.
Whats the point with 'exec'? This doesnt make me any wiser either:
bash$ help exec
exec: exec [-cl] [-a name] file [redirection ...]
Exec FILE, replacing this shell with the specified program.
If FILE is not specified, the redirections take effect in this
shell. If the first argument is `-l', then place a dash in the
zeroth arg passed to FILE, as login does. If the `-c' option
is supplied, FILE is executed with a null environment. The `-a'
option means to make set argv[0] of the executed process to NAME.
If the file cannot be executed and the shell is not interactive,
then the shell exits, unless the shell option `execfail' is set.
bash$
Are you sure this will not do what you want?
while read line ;do
echo -${line}-
done < "$file"
//Mats
"exec" is overloaded in the sh-ish shells. It has two very distinct
meanings (purists will write in to point how they are, distantly, related).
The first is to exec a program (see "man -s2 exec"); the other is to change
the file descriptors of the currently running shell (see "man -s2 dup" and
"man dup2")
Indeed it will - but as with the original post, only if the variable "file"
is assigned a value beforehand.
BTW, since what this thread is really about is the (homework) problem of
"printing out all the words in a file" (the problem has been posted to many
different groups, with several different thread titles), I feel I should
point out that to really print out all the words in a file, you need to
invoke the "spell" program at some point. None of the "solutions" I've
seen so far have done so.
Well your clue was in the help exec statement "If FILE is not
specified, the redirections take effect in this shell." It's not an
especially helpful sentence though, is it? The exec man page is
worded slightly better - "Input/output arguments may appear and, if no
other arguments are given, cause the shell input/output to be
modified."
In otherwords, all commands that read from stdin following an
exec<path will actually take their input from path. All commands that
write to stdout following an exec>path will actually send their output
to path. Follow?
Mark.
No, i dont get it :(
To me 'exec' is used to replace a running process with another program.
Thus the script will never continue after the 'exec' statement, so when
the program dies it is the end of it all. For example i have 'exec twm'
on the last line in my '.xsession' file.
But if you say that it can do other things depending on options given
i have to believe you, although i dont understand.
You say it provides redirection. Is there any difference between
exec-redirection and ordinary redirection? Perhaps someone can come up
with an example to clarify. Thanks for trying anyway :)
//Mats
That's only true when exec has a command argument, like 'exec twm'.
When exec is invoked with no arguments, it changes the I/O redirection of
the current shell process, so it affects all the commands that follow it.
Here's an example:
exec < inputfile
read var1
read var2
This puts the first line of inputfile in $var1, and the second line in
$var2.
It's often used when you want to redirect the input of a "while read"
loop. If you just write:
while read line
do
...
done < inputfile
then any variable assignments performed in the body of the loop will be
lost when the loop finishes, because the redirection causes the loop to run
in a subshell. To solve this, you can write:
exec < inputfile
while read line
do
...
done
Another time when it might be used is if you want all the output from your
script to be put in a file rather than being sent to the original stdout.
You could write:
echo ... > logfile
...
echo ... >> logfile
...
echo ... >> logfile
But if you write it that way, it's very easy to forget to put ">> logfile"
on some of the lines. Instead, you can write:
exec > logfile
echo ...
...
echo ...
...
echo ...
Now there's nothing to remember.
--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
Thanks for demystifying this. Nice!
However. I think you are wrong when stating that you loose
variable-assignments inside the while-loop with the above construct
( ie, the redirection '<' directly to while-loop ).
Before this newsgroup tought me this i used to do 'cat file | while ... '.
Then the while-loop is executed in a subshell and assignments is lost
just as you said.
Example:
bash$ cat file
1
2
3
bash$ cat file |while read r ;do
> ((var+=r))
> echo $var
> done
1
3
6
bash$ echo $var
bash$ while read r ;do
> ((var+=r))
> echo $var
> done <file
1
3
6
bash$ echo $var
6
bash$
This is bash. Does it work like you say in pure sh?
//Mats
Yes. Here's /bin/sh on Solaris 2.6:
$ var=1
$ while read r
2
3
4
$ echo $var
1
$
However, Korn shell works like bash.
--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
bm> It's often used when you want to redirect the input of a "while read"
bm> loop. If you just write:
bm> while read line
bm> do
bm> ...
bm> done < inputfile
bm> then any variable assignments performed in the body of the loop
bm> will be lost when the loop finishes, because the redirection
bm> causes the loop to run in a subshell.
Not so. This is only true of pipes, not redirection.
$ echo $foo
$ while read x; do foo=y; done < /etc/passwd
$ echo $foo
y
--
-------------------------------------------------------------------------------
Paul D. Smith <pausm...@nortelnetworks.com> HASMAT--HA Software Mthds & Tools
"Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
These are my opinions---Nortel Networks takes no responsibility for them.