While loop to read values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop to read values
# 1  
Old 11-21-2013
While loop to read values

Hi Everyone,

I am currently tasked with some reporting on various Unix based OSes. I have a script deployed that runs and grabs the information I am looking for, and has a bit of logic to output the desired result into a text file.

Example of my text file:

Code:
multiUsers=yes
availableDiskSpace=40
homeFolderSize=10G

So, the script that runs basically pulls info from a system, parses it to my desired output, then echoes it to a text file where I can later read it and report or take action on it. My idea was to use a while loop to read these values, then build in if then statements to do a task.

example script:
Code:
#!/bin/bash

# while loop example of reading a file

file=/var/reports.txt

cat ${file} | while read line ; do
    echo -e ${line};
      if [[ multiUsers == "yes" ]]
          then echo "has multiple human users"
      fi
      if [[ availableDiskSpace -lt 20 ]]
        then echo "the boot volume has less than 20% free space"
      fi
done

exit 0

I have tried numerous ways of doing this, and I honestly hardly ever use while loops so I know there are other ways to do this. Perhaps even better ways, but now that I cannot get it work I am pretty much forcing myself to learn why it is not working.

In my script I would take actions, but the echoes are there as place holders for other commands I may run, as I am in the testing phase.

Thanks,
ZB
# 2  
Old 11-21-2013
Quote:
Originally Posted by Zaphod_B
Hi Everyone,

I am currently tasked with some reporting on various Unix based OSes. I have a script deployed that runs and grabs the information I am looking for, and has a bit of logic to output the desired result into a text file.

Example of my text file:

Code:
multiUsers=yes
availableDiskSpace=40
homeFolderSize=10G

So, the script that runs basically pulls info from a system, parses it to my desired output, then echoes it to a text file where I can later read it and report or take action on it. My idea was to use a while loop to read these values, then build in if then statements to do a task.

example script:
Code:
#!/bin/bash

# while loop example of reading a file

file=/var/reports.txt

cat ${file} | while read line ; do
    echo -e ${line};
      if [[ multiUsers == "yes" ]]
          then echo "has multiple human users"
      fi
      if [[ availableDiskSpace -lt 20 ]]
        then echo "the boot volume has less than 20% free space"
      fi
done

exit 0

I have tried numerous ways of doing this, and I honestly hardly ever use while loops so I know there are other ways to do this. Perhaps even better ways, but now that I cannot get it work I am pretty much forcing myself to learn why it is not working.

In my script I would take actions, but the echoes are there as place holders for other commands I may run, as I am in the testing phase.

Thanks,
ZB
In your loop you have a variable named line; you do not define the variables multiUsers and availableDiskSpace. So, your if statements will never match what you're looking for.

Try replacing:
Code:
cat ${file} | while read line ; do
    echo -e ${line};
      if [[ multiUsers == "yes" ]]
          then echo "has multiple human users"
      fi
      if [[ availableDiskSpace -lt 20 ]]
        then echo "the boot volume has less than 20% free space"
      fi
done

with something like:
Code:
while IFS='=' read var val
do      printf "%s=%s\n" "$var" "$val"
        if [ "$var" == "multiUsers" ] && [ "$val" == "yes" ]
        then    echo "has multiple human users"
        fi
        if [ "$var" == "availableDiskSpace" ] && [ "$val" -lt 20 ]
        then    echo "the boot volume has less than 20% free space"
        fi
done < "$file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

4. Shell Programming and Scripting

Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space Input file server1 server2 server3 server4 server5 server6 When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values while read a b do echo "$a" echo... (5 Replies)
Discussion started by: chidori
5 Replies

5. Shell Programming and Scripting

[SOLVED] UNIX FOR loop to read a variable with multiple values

Hi, I have a variable which stores file names as a result of find command. I need to delete all these files one by one, i.e. by a loop. Can anyone tell me how can it be done? The variable f2d has the file names like these abc.txt bcd.txt fff.txt gef.txt Now I have used a loop as... (12 Replies)
Discussion started by: jhilmil
12 Replies

6. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

7. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

8. 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

9. 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

10. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies
Login or Register to Ask a Question