Use one loop get the variable assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use one loop get the variable assignment
# 1  
Old 12-20-2012
Use one loop get the variable assignment

Code:
        for var in {1..3}
        do
                lspci -nn |awk -v i=$var '/8086:10a7/{split($1,a,"[:.]"); print "0x"a[i]}'
        done

it will output
Code:
0x01
0x00
0x1

I want to let bus=0x01 slot=0x00 fun=0x1, How to modify this loop?


I know it can be get by


Code:
bus=`lspci -nn |awk '/8086:10a7/{split($1,a,"[:.]"); print "0x"a[1]}'`
slot=`lspci -nn |awk '/8086:10a7/{split($1,a,"[:.]"); print "0x"a[2]}'`
fuc=`lspci -nn |awk  '/8086:10a7/{split($1,a,"[:.]"); print "0x"a[3]}'`

But Can we get it in one loop? or use one line like python (a,b,c) ={something"

Thanks

Last edited by yanglei_fage; 12-21-2012 at 01:28 AM..
# 2  
Old 12-20-2012
give some samples of command:

Code:
lspci -nn|awk '/8086:10a7/'

# 3  
Old 12-21-2012
Quote:
Originally Posted by rdcwayx
give some samples of command:

Code:
lspci -nn|awk '/8086:10a7/'

No,this doesn't work for me

I thought with below code,but it doesn't work Smilie



Code:
lspci -nn |awk -v i=$var '/01:00.1/{split($1,a,"[:.]"); print "0x"a[1] " " "0x"a[2] " ""0x"a[3]}' |xargs -n 3 |read bus slot func

# 4  
Old 12-21-2012
The previous post only works in ksh (last pipe element is in current context).
"eval" works in all sh derivates:
Code:
bus=slot=fuc=""
eval `
lspci -nn |
awk '
/8086:10a7/{split($1,a,"[:.]"); print "bus=0x"a[1],"slot=0x"a[2],"fuc=0x"a[3]}
'`

# 5  
Old 12-22-2012
Try this while loop:
Code:
lspci -nn |awk '/1002:438/ {split($1,a,"[:.]"); for (i=1;i<=3;i++) printf "0X%02X ",a[i]; printf "\n"}' |while read bus slot fuc; do echo $bus, $slot, $fuc; done
0X00, 0X0C, 0X00
0X00, 0X0D, 0X00

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

3. Shell Programming and Scripting

'eval' used in variable assignment

pattern1=book { x=1 eval echo \$pattern$x } book (this is the output) But when I assign a variable to the output of the eval it doesn't work unless I prefix 2 times backslash before $ as shown below. { a=`eval echo \\$pattern$x` echo $a } book Why here twice "\" has to be... (3 Replies)
Discussion started by: ravisingh
3 Replies

4. Shell Programming and Scripting

Complex variable assignment

Hi, I have a requirement as follows, need to call the format of ${$var} form. For example, i am taking a variable. count=1, ((LIMIT_$count=$count + 1)) Now i have to echo this variable LIMIT_$count. (This is in a loop..) echo ${LIMIT_$count} - displays as a syntax... (3 Replies)
Discussion started by: abhisheksunkari
3 Replies

5. Shell Programming and Scripting

Automatic variable assignment inside a for loop

cs1=`echo "scale=8;($css1/$css0)*100"|bc` cs2=`echo "scale=8;($css2/$css0)*100"|bc` cs3=`echo "scale=8;($css3/$css0)*100"|bc` cs4=`echo "scale=8;($css4/$css0)*100"|bc` cs5=`echo "scale=8;($css5/$css0)*100"|bc` cs6=`echo "scale=8;($css6/$css0)*100"|bc` cs7=`echo "scale=8;($css7/$css0)*100"|bc`... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

6. Shell Programming and Scripting

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

7. Shell Programming and Scripting

substituted variable assignment

I try to run this script, however, it gives an exception in line 3. How do I do an assignment to a substituted variable? #!/bin/bash name=fruit ext_$(eval echo ${name})=apple tmp=ext_$(eval echo ${name}) if ]; then echo "apple" elif ]; then echo "orange" fi echo ${!tmp} Error... (2 Replies)
Discussion started by: angelokh
2 Replies

8. Shell Programming and Scripting

eval and variable assignment

Hi, i have an issue with eval and variable assignment. 1) i have a date value in a variable and that date is part of a filename, var1=20100331 file1=${var1}-D1-0092.xml.zip file2=${var2}-D2-0092.xml.zip file3=${var3}-D3-0092.xml.zip i am passing the above variables to a script via... (11 Replies)
Discussion started by: mohanpadamata
11 Replies

9. Shell Programming and Scripting

Variable assignment from a for loop values.

Guys I am trying to assignthe values to variables from a for loop. s1:/tmp> for i in `cat test` > do > echo $i > done Sdosanjh 2 6 Now, I want is NAME=Sdosanjh CURRENT=2 SPECIFIED=6 there are multiple lines in the "test" file. So next time when for loop picks values from next... (1 Reply)
Discussion started by: sdosanjh
1 Replies

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies
Login or Register to Ask a Question