For loop returning more values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers For loop returning more values
# 1  
Old 09-07-2011
For loop returning more values

Hi All,

Thanks all of you for the help you provide to me. Well, I have one more problem, where I am trying to pull file system information in the loop and display the filesystem percentege. I am using following code to achive this, nut it's giving the weired output.

Code:
My file system is 

/dev/sg09/sv1         140471312 27446992 113024320    20%   /db/ffin1/data9
/dev/sg08/sv2         140462032 26272704 114189328    19%   /db/ffin1/data8
/dev/sg02/sv1         141274704 130280096 10994608    93%   /db/ffin1/data1
/dev/sg03/sv1         141307536 134482064  6825472    96%   /db/ffin1/data2
/dev/sg04/sv1         141300976 133642288  7658688    95%   /db/ffin1/data3
/dev/sg05/sv1         141291472 132425616  8865856    94%   /db/ffin1/data4
/dev/sg06/sv1         141346672 139491808  1854864    99%   /db/ffin1/data5
/dev/sg07/sv1         141315312 135477184  5838128    96%   /db/ffin1/data6
/dev/sg08/sv1         141235696 125286240 15949456    89%   /db/ffin1/data7

#!/sbin/sh
fsystem=$(bdf | grep data |grep ffin1 | awk '{print $6}')
for x in `bdf | grep data |grep ffin1 | awk '{print $5}'|grep -v u | cut -d "%" -f1 -`
do
echo "the $fsystem is $x"
done

after executing , it returns following:-

the /db/ffin1/data7
/db/ffin1/data6
/db/ffin1/data5
/db/ffin1/data4
/db/ffin1/data3
/db/ffin1/data2
/db/ffin1/data1
/db/ffin1/data8
/db/ffin1/data9 is 89
the /db/ffin1/data7
/db/ffin1/data6
/db/ffin1/data5
/db/ffin1/data4
/db/ffin1/data3
/db/ffin1/data2
/db/ffin1/data1
/db/ffin1/data8
/db/ffin1/data9 is 96
the /db/ffin1/data7
/db/ffin1/data6
/db/ffin1/data5
/db/ffin1/data4
/db/ffin1/data3
/db/ffin1/data2
/db/ffin1/data1
/db/ffin1/data8
/db/ffin1/data9 is 99
the /db/ffin1/data7
/db/ffin1/data6
/db/ffin1/data5
/db/ffin1/data4
/db/ffin1/data3
/db/ffin1/data2
/db/ffin1/data1
/db/ffin1/data8
/db/ffin1/data9 is 94
the /db/ffin1/data7
/db/ffin1/data6
/db/ffin1/data5
/db/ffin1/data4
/db/ffin1/data3
/db/ffin1/data2
/db/ffin1/data1
/db/ffin1/data8
/db/ffin1/data9 is 95
the /db/ffin1/data7
/db/ffin1/data6
/db/ffin1/data5
/db/ffin1/data4
/db/ffin1/data3
/db/ffin1/data2
/db/ffin1/data1
/db/ffin1/data8
/db/ffin1/data9 is 95
the /db/ffin1/data7
/db/ffin1/data6
/db/ffin1/data5
/db/ffin1/data4
/db/ffin1/data3
/db/ffin1/data2
/db/ffin1/data1
/db/ffin1/data8
/db/ffin1/data9 is 92
the /db/ffin1/data7
/db/ffin1/data6
/db/ffin1/data5
/db/ffin1/data4
/db/ffin1/data3
/db/ffin1/data2
/db/ffin1/data1
/db/ffin1/data8
/db/ffin1/data9 is 19
the /db/ffin1/data7
/db/ffin1/data6
/db/ffin1/data5
/db/ffin1/data4
/db/ffin1/data3
/db/ffin1/data2
/db/ffin1/data1
/db/ffin1/data8
/db/ffin1/data9 is 20

# 2  
Old 09-07-2011
Instead of cramming everything into backticks, you can do

Code:
command | while read TOKEN1 TOKEN2 TOKEN3 ...
do
        echo "First field is $TOKEN1"
        ...
done

no need to run STUFF=`awk | grep | sed | cut | foo | kitchen | sink`.

It would help to know what the output of 'bdf' looks like to tell why your program's doing what it is. (Unless that's the unlabelled output given earlier?) It's partly because 'for x in `junk`' splits on spaces, not just newlines. (the while read loop reads line-by-line.) It's also because fsystem stores every possible result in one variable, causing it to print every result every loop.

If I grasp your intent correctly now, how about:

Code:
bdf | grep "ffin1/data" | while read DEV A B C PERCENT FOLDER
do
        echo "$DEV = $FOLDER"
done


Last edited by Corona688; 09-07-2011 at 03:28 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl subroutine returning different values in HPUX

HI , I am running a program on hpux in perl. I am encountering a strange issue where when i print a variable in the sub which is returning it , it prints a different value but when i call it and store value in a variable it gives a different o/p. the sub is sub CheckConfigFilePattern ... (4 Replies)
Discussion started by: Jcpratap
4 Replies

2. Shell Programming and Scripting

Returning multiple values in Shell

Hi I have a code as the following #!/usr/bin/ksh set -x row() { a=$1 b=$2 c=$(($a + $b)) d=$(($a * $b)) echo $a $b } e=`row 2 3` set $e echo "The value of c is $c" echo "The value of d is $d" My requirement is I need to pass two arguments to a function and return two values... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

3. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Output breaking when returning multiple values

I've been trying to write a command-line function to grab a website's MX records and their ip addresses. The code below works with domains that only have one MX record: function kmx { mx=`host -t MX $1 | awk '{ print $7 }'`; ip=`host $mx | sed '/IPv6/d;/handled/d' | awk '{ print $4 }'`; ... (8 Replies)
Discussion started by: Azrael
8 Replies

5. Shell Programming and Scripting

how to capture oracle function returning 2 values in unix

i have an oracle function which returns two values, one is the error message if the function encounters anything and another one which returns a number i need to capture both and pass it on to unix shell script how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies

6. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies

7. Shell Programming and Scripting

awk/nawk returning decimal values?

Hi Running a specific nawk statement over a 17m lines files returns the following: /bin/nawk: not enough args in ..... input record number 1,25955e+06, file test.1 source line number 1 I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out. ... (1 Reply)
Discussion started by: moutaye
1 Replies

8. Solaris

awk/nawk returning decimal values?

Hi Running a specific nawk statement over a 17m lines files returns the following: /bin/nawk: not enough args in ..... input record number 1,25955e+06, file test.1 source line number 1 I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out. ... (1 Reply)
Discussion started by: moutaye
1 Replies

9. Shell Programming and Scripting

Returning values from child to parent shell

I need to send the status from child shell failure to parent shell. I would like to know how could we accomplish this. My parent.sh is as below: #!/bin/ksh set -x echo "I am in parent shell now..." child.sh ret_stat=$? echo "rest_stat=$ret_stat" echo "I am below parent shell end..." ... (4 Replies)
Discussion started by: acheepi
4 Replies

10. Shell Programming and Scripting

Returning Values (shell Script)

I have an application on Informix 4GL, and I am invoking the shell script from the program, but I need to know if the shell script work fine or not, in order to continue the process. I know that we can use $? to get the final status but this is on unix command. How can I return a value from the... (3 Replies)
Discussion started by: jennifer01
3 Replies
Login or Register to Ask a Question