Ideas for while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ideas for while loop
# 8  
Old 10-06-2014
Assuming that your shells supports it, you can get away from calling an external program like egrep for such a simple match
Code:
while read -r line
do
  [[ $line =~ error ]] && echo $line
done < /var/log/syslog

Again, this is all assuming that the echo $line is nothing more that a place holder or test for what it would be a real `do something meaningful', otherwise is silly.

Last edited by Aia; 10-06-2014 at 02:46 AM.. Reason: clarifying again.
This User Gave Thanks to Aia For This Post:
# 9  
Old 10-06-2014
Quote:
Originally Posted by SkySmart
i tried this. got the same error.
Hello SkySmart,

We can follow simple way here too by redirecting the egrep output to a file and then we can take input from that file as follows. Offcourse finding other ways is good too for our learning.

Code:
egrep error /var/log/syslog > Error_Details.txt
while read line
do
     echo "$line"
done < Error_Details.txt

Or we can simply use egrep error /var/log/syslog in script itself Smilie.

Thanks,
R. Singh

Last edited by rbatte1; 10-06-2014 at 09:42 AM.. Reason: Lower case for commands in the shell script.
This User Gave Thanks to RavinderSingh13 For This Post:
# 10  
Old 10-06-2014
I'm still confused as to what the end requirement is. If you want to extract records to display on the screen, the egrep by itself will do that. If you need to use a loop to do something else with the records, then save the IO cost, the disk space and the time with:-
Code:
egrep error /var/log/syslog | while read line
do
     echo "Processing line \"$line\""
     # Do whatever else here......
done

You will need to be careful that any nested loops don't gobble up your input to this loop.


Anyway, what are you trying to achieve?



Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 11  
Old 10-06-2014
Quote:
Originally Posted by rbatte1
I'm still confused as to what the end requirement is. If you want to extract records to display on the screen, the egrep by itself will do that. If you need to use a loop to do something else with the records, then save the IO cost, the disk space and the time with:-
Code:
egrep error /var/log/syslog | while read line
do
     echo "Processing line \"$line\""
     # Do whatever else here......
done

You will need to be careful that any nested loops don't gobble up your input to this loop.


Anyway, what are you trying to achieve?



Kind regards,
Robin
i have to run a while loop and it is very important to me that i find the method that is the quickest.

The methods below appear to be the fastest, but, while they loop through the file, they error out at the end, complaining about file name too long:

Code:
while read line
do
    echo $line
done < <(egrep error /var/log/syslog)

Code:
while read line
do
    echo $line
done < "$(egrep error /var/log/syslog)"

# 12  
Old 10-07-2014
I don't understand. The first method should work (if the shell supports it) and cannot complain that the filename is too long (unless this is literally somewhere in the text of your syslog file Smilie )
If the shell does not support it, it will give a syntax error.


The second method is wrong and will not work and will produce that error.


--
BTW. it is best to quote $line ( echo "$line" )
# 13  
Old 10-07-2014
Hi,
Another way:
Code:
while read line
do
    echo "$line"
done <<_EOF
$(egrep error /var/log/syslog)
_EOF

Regards.
This User Gave Thanks to disedorgue For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help me get some ideas

Hello, I have been given a scripting project, but have not learned any scripting. I need to get some ideas on how to start. Attached is part of the project: I have no idea how to parse the arguments. What I had in mind was to get the arguments ($1, $2, ... ) and have if statements for different... (1 Reply)
Discussion started by: facepalm
1 Replies

2. UNIX and Linux Applications

Need ideas for graduation project based on unix or linux Need ideas for graduation project based on

Dear all, i am in last year of electronics department in engineering faculty i need suggestions for a graduation project based on unix or free bsd or linux and electronics "embedded linux " i think about embedded unix for example or device drivers please i need helps (1 Reply)
Discussion started by: MOHA-1
1 Replies

3. UNIX for Advanced & Expert Users

Sendmail - Any Ideas?

I have solaris 9 and am using sendmail to pickup requests and forward them to a bulk mail server on a different port. Now for the fun...In sendmail, I need to find a way to place a default address in the Mail From: and Rcpt To: or remove them as required - These will be picked up by the bulk mail... (3 Replies)
Discussion started by: mikey2003ma
3 Replies

4. Shell Programming and Scripting

Scripting ideas?

Hi All, How can I script the following logic? Step 1: Check if the file xyz.txt exists under direcotry test and if the size of the file xyz.txt is greater than 32MB. Step 2: If the above conditions are true(file exists and size >32 MB), then step 3, otherwise step 4 (file does not exist or... (2 Replies)
Discussion started by: Sueyoung88
2 Replies

5. Shell Programming and Scripting

any ideas?

i need to compare to dates/times given in the format MMDDhhmmYY. That is month, day, hour, minute, year. It is a 24 hour clock. I need to compare two dates to check that they are, say, less than 900 seconds apart. I have got to a point where it checks the time, turns the values into seconds and... (5 Replies)
Discussion started by: fwabbly
5 Replies

6. Cybersecurity

Any Ideas !!!!!!!!!!

Hi, I installed sybase server on a LINUX server. I assigen port 2025 whilst installation for sybase , later i uninstalled sybase and when i try to reinstall sybase and use port 2025 it throw up error saying that it is already in use, use other port number. How can I re-use the same port number... (2 Replies)
Discussion started by: suda
2 Replies

7. UNIX for Dummies Questions & Answers

The history of UNIX and the ideas behind it

Hi. I am new here, and this is my first post at the UNIX.com forums. I have read the book Fire in the Valley: The Making of the Personal Computer, and I noticed that neither UNIX nor Linux was mentioned once in the book. Why is this? What was UNIX's place in the early days of personal computers? ... (6 Replies)
Discussion started by: elendil
6 Replies

8. Shell Programming and Scripting

Any Ideas?

I have several staging directories on my UNIX server. /usr2/data1 /usr2/data2 /usr2/data3 /usr2/data4 /usr2/data5 /usr2/data6 In these directories a file is transferred from different PC's connected to the network via TCP/IP. The File name is constant for all directories. Transfers... (1 Reply)
Discussion started by: Docboyeee
1 Replies
Login or Register to Ask a Question