I am not getting value outside the loop
Hi This is my script like..
while read line do myval="DAKS" done echo $myval i am not getting the value output outside the while loop help me out if u havr some others |
Quote:
if you want to do more complex processing, why not try awk. Code:
awk '{ |
loop
hi,
I am trying to do a loop, the main puporse of the loop is that it will search in a multiple text files stored on tmp2 a pattern /remote-host so it would output the pattern into tmp3. The code that I have is the following: for i in $( tmp2 ) ; do sed -n '/remote-host/p' tmp2 >> tmp3 done and obviously is not working, I would appreciate any help. regards, mike |
A simple solution to the subject line:
cat file | ( while read line do echo process $line done ) |
you are digging up an old thread. check the date of post next time. also, no need to use cat. its useless
|
Quote:
Using cat rather than a redirect here (again IMO) makes the answer crystal clear and hopefully will educate rather than confuse :) Code:
( If you are after an optimised solution feel free to use exec with a fd numeric redirect and read -u too : Code:
exec 9<file ;) |
Code:
#!/bin/bash |
Check bellow link, there I posted 5 methods to parse a file line by line
Process a file line by line |
Quote:
Nice blog :) Regards request for correction ... Regards "Bash can sometimes start a subshell in a PIPED "while-read" loop", what are the circumstances under which bash does not do so? Regards Code:
FILENAME=$1 Regards "exec 3<&0 Now all of the keyboard and mouse input is going to our new file descriptor 3" it is only the keyboard input, not the mouse input. Regards "while read LINE Using File Descriptor" an alternative, that does not require saving fd 0 and restoring it is to assign the file to, say, fd 3 and use read's -u option to read from fd3. Regards typos, "The file descriptors for stdin,stdout, and stderr are 0,1, and 2, respectively" it is more correctly "The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively", that is with a space after the fist commas in the list. The test timings are very useful showing how much faster awk is at processing a large file but it would be nice to see the other case -- the case when processing a single line. You would have to process the same line many times to get the timing and of course this would buffer both the input file and awk in RAM but it would still be interesting. I understand that the shell's fork-exec to run awk uses a lot of resource which will more than offset awk's much greater file IO and string handling efficiency. |
I have posted 5 different methods to process a file line by line in a shell script. check bellow link
Different ways to process a file line by line |
Besides the quoting issue that catkin mentioned, you should also use read -r in all the examples. You almost never want to not use -r. Also, use "printf" instead of "echo -e". The exact behavior of "echo -e" can change a lot from system to system.
|
Why is everyone responding to this >6 year old thread?
|
array within function
hello everyone!
i am passing an array in function. I want to read the first content again and again until it is empty so function calltoarray { read $1[0] while [ ! -n "$1[0]" ] do echo empty read $1[0] done } but the code is not working. please help me out. |
howto read file shell
Okay first off I realize this thread is old... very old. However the complexity that these guys are going about it is driving me nuts so I have to post this to enlighten them (and anyone who would be searching for this solution). First off reading a file in shell is extremely simple. lets make a simple file called mycat and let it's contents be... Code:
#!/bin/bash Code:
chmod 755 ./mycat Quote:
Code:
find /path/to/tmp2 -type f -not -name 'tmp3' -print0 | xargs -0 grep -ih 'remote-host' > tmp3 SAM |
Hi, i test my script with a file and is excellent!!
for (( i = 0; i < `awk '{x++}END{ print x}' test.out`; i++)) do echo `head -n $i test.out | tail -n 1` #return the line done where test.out is the file to scann!! |
All times are GMT -5. The time now is 10:04 PM. |