Sponsored Content
Top Forums Shell Programming and Scripting For loop or while loop from a text file Post 303013301 by nms on Monday 19th of February 2018 10:42:32 AM
Old 02-19-2018
For loop or while loop from a text file

Hi all,

i developed a script to measure the uptime of a process in a Solaris 10/11 environments.
All is well, but i came across a situation where there are multiple processes of the same name. Basically i have the following result file:

Code:
beVWARS 13357 19592122
beVWARS 14329 19591910
beVWARS 6371 20436149
beVWARS 6368 20436149
beVWARS 6374 20436149
beVWARS 13361 19592118
beVWARS 13988 19591989
beVWARS 6367 20436149
beVWARS 6365 20436149
beVWARS 6355 20436149
beVWARS 6363 20436149
beVWARS 14055 19591982
beVWARS 13267 19592147
beVWARS 13562 19592097
beVWARS 6361 20436149
beVWARS 14228 19591944

Where first column is the process name, followed by it's pid and the uptime in seconds.

i am trying to find a way to get the uptime vs. an if condition (by a loop?) where if the uptime is less then say 3600 seconds it outputs the process and it's pid on the same line.

Bascially i tried:

Code:
while read line; do
if [[ $line -lt $CRITICAL_SIZE ]]; then
echo -n `grep $line $CRONOUTFILE | awk '{print$2,$3}'`
fi
done <<< "$TIME"

Where $TIME contains only the 3rd column, i.e. the uptime, but it's not working as it should.
$CRONOUTFILE is the output file as shown above.
What i am trying to achieve is to output only the process and pid in a similar output:

Critical - the following processes $PROCESS are less then $CRITICAL_SIZE - $PID:$TIME, $PID:$TIME...,$PID:$TIME

Where the $PROCESS can be easily set an argument in the script.

I am totally confused in using the for loops or the while loop. May you kindly give me some hints?

Rgds,

Matthew
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get data from two text file in one loop

Hi all I have two file that contain different data.I want to get each file data line by line in one loop.i try with cat but cat works ok against one file File a 23 34 45 File b abc xyz cgh now i want like this in a loop.Below is just example.i am doing some thing smiler.can any... (3 Replies)
Discussion started by: aliahsan81
3 Replies

2. Shell Programming and Scripting

Storing the values in text file using while loop in shell script

Hi Frdz while read line do name=`echo $line | cut -d' ' -f 1 ` password=`echo $line | cut -d`-` -f 2` name > logfile.txt password > logfile.txt done < list.txt When it is run, am getting last values in list.txt file only,it is not storing lall the list entry values. How can i... (5 Replies)
Discussion started by: KiranKumarKarre
5 Replies

3. Shell Programming and Scripting

while loop to add text to the end of a file

Hi all, I've got 2 files. File 1 has a list say a b c d e f File 2 got start= What I want is to create File 3 which look like this start=a,b,c,d,e,f So is it possible to loop throught File1 to echo it into File3 in one line? (3 Replies)
Discussion started by: stinkefisch
3 Replies

4. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

5. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

6. Shell Programming and Scripting

matching user input to a text file loop?

until do read -p "Invalid cars. Try againa" cars1 done Ok i have the above code, im getting users input and if it doesnt match in the file the user has to try again untill its correct But when i run this it gives me an error saying ./Cars.bash: line 43: (2 Replies)
Discussion started by: gangsta
2 Replies

7. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

8. Shell Programming and Scripting

How to loop a text file to remove the old files?

Hi , I'm using Ksh 88 version. I've a text file which contains the directory names DIR1 DIR2 ---- DIR10 I've to remove the data which has more that 30 days from the above directories . The data has to be removed from the following path cd /home/etc/DIR1/hist cd /home/etc/DIR2/hist... (2 Replies)
Discussion started by: smile689
2 Replies

9. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

10. Shell Programming and Scripting

Problem with while loop reading every line of a text file

Hello, I'm using RHEL 5.1 with bash. How to handle "read" inside while loop reading every line? Please see below: # cat /tmp/passwd_sample CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash HERCOT01:NK/3j2ZB4ZC7Q:20022:125:TOM HERCOCK:/home/everyone:/bin/bash... (4 Replies)
Discussion started by: reddyr
4 Replies
UPTIME(1)							   User Commands							 UPTIME(1)

NAME
uptime - Tell how long the system has been running. SYNOPSIS
uptime [options] DESCRIPTION
uptime gives a one line display of the following information. The current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes. This is the same information contained in the header line displayed by w(1). System load averages is the average number of processes that are either in a runnable or uninterruptable state. A process in a runnable state is either using the CPU or waiting to use the CPU. A process in uninterruptable state is waiting for some I/O access, eg waiting for disk. The averages are taken over the three time intervals. Load averages are not normalized for the number of CPUs in a system, so a load average of 1 means a single CPU system is loaded all the time while on a 4 CPU system it means it was idle 75% of the time. OPTIONS
-p, --pretty show uptime in pretty format -h, --help display this help text -s, --since system up since, in yyyy-mm-dd HH:MM:SS format -V, --version display version information and exit FILES
/var/run/utmp information about who is currently logged on /proc process information AUTHORS
uptime was written by Larry Greenfield <greenfie@gauss.rutgers.edu> and Michael K. Johnson <johnsonm@sunsite.unc.edu> SEE ALSO
ps(1), top(1), utmp(5), w(1) REPORTING BUGS
Please send bug reports to <procps@freelists.org> procps-ng December 2012 UPTIME(1)
All times are GMT -4. The time now is 01:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy