Operating System - HP-UX
1827375 Members
4029 Online
109963 Solutions
New Discussion

Re: Echo and foreach loop problem

 
SOLVED
Go to solution
Aminur Rizal
Advisor

Echo and foreach loop problem

Hi all,
I have the following script

#!/bin/csh
set imsi=`grep IMSI test.out | awk '{print $3}'`
foreach ki (sed -n 'var_out/,p' test.out | sed '1,2d' | awk '{print $7}'`)
echo "ZMAC:IMSI=${imsi}":KI="$ki",UV=50"
end

The first line while produce
imsi1

and
for 2nd line will produde :=
ki1
ki2
ki3
ki4

and i was try to get the output using echo as :=
ZMAC:IMSI=imsi1":KI=ki1,UV=50;
ZMAC:IMSI=imsi1":KI=ki2,UV=50;
ZMAC:IMSI=imsi1":KI=ki3,UV=50;

But the above script only give me :=
:KI=ki1,UV=50;
:KI=ki2,UV=50;
:KI=ki3,UV=50;
with the missing 1st parameter.
Where did i go wrong?
Thanks in advance.
You guys are great!



Aminur Rizal Afip
11 REPLIES 11
Thierry Poels_1
Honored Contributor

Re: Echo and foreach loop problem

hi,

too many quotes
try
echo "ZMAC:IMSI=${imsi}:KI=${ki},UV=50"
instead of
echo "ZMAC:IMSI=${imsi}":KI="$ki",UV=50"

regards,
Thierry
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
B. Hulst
Trusted Contributor

Re: Echo and foreach loop problem

Hi,

Or you can use:

echo "ZMAC:IMSI="${imsi}":KI="$ki",UV=50"

to create new subscribers.

Regards,
Bob
Aminur Rizal
Advisor

Re: Echo and foreach loop problem

Hi,
Thanks but both of the solution seems not working for me.
The output is still
:KI=ki1,UV=50;
:KI=ki2,UV=50;
:KI=ki3,UV=50;

Hmm..
Aminur Rizal Afip
Hein van den Heuvel
Honored Contributor

Re: Echo and foreach loop problem

There appear to be some typo's in you script.
Please do not retype... just cut & paste!
It would help to have a small 'test.out' attache to try on.

This works on my SuSe 9 box:

#!/bin/csh
set imsi=`grep IMSI test.out | awk '{print $3}'`
foreach ki (`sed -n "/var_out/p" test.out | sed "1,2d" | awk '{print $7}'`)
echo "ZMAC:IMSI=${imsi}":KI="$ki",UV=50
end

----- input ----

IMSI xxx imsi1
a var_out c d e f ki5 x x
a var_out c d e f ki6 x x
a var_out c d e f ki7 x x
a var_out c d e f ki8 x x

a b c d e f ki1 x x
a var_out c d e f ki2 x x
a b var_out d e f ki3 x x
a b c var_out e f ki4 x x
a b c d e f ki5 x x

------ output -------
ZMAC:IMSI=imsi1:KI=ki7,UV=50
ZMAC:IMSI=imsi1:KI=ki8,UV=50
ZMAC:IMSI=imsi1:KI=ki2,UV=50
ZMAC:IMSI=imsi1:KI=ki3,UV=50
ZMAC:IMSI=imsi1:KI=ki4,UV=50

The following perl 'one-liner' gives the same output. The only assumption is the the line with IMSI comes before var_out. Does it?

perl -ne '$imsi=(split)[2] if /IMSI/; print "ZMAC:IMSI=${imsi}:KI=".(split)[6].",UV=50\n" if (/var_out/ && ($i++ > 1))' est.out

fwiw,
Hein.



Aminur Rizal
Advisor

Re: Echo and foreach loop problem

Hi Hein,
This is a sample of test.out

----------
Transport_key: 2
A3_Type: 04
PPD_srn :
Quantity: 80
Type: Plug-in
Profile:
Batch: SIMT101
SIM_Reference: USIM_TEST
HLR: 1
Location: WM
013/019: 013
Graph_ref:
var_in_list:
Ser_nb: 896017363050170155364
IMSI : 5021313872800000096

var_out: PUK/PIN/PIN2/PUK2/KI/ADM1

8960130501701553643F 502131300000096 11111111 1234 1234 22222222 11111111111111111111111111111111 12345678
8960130501701553650F 502131300000097 11111111 1234 1234 22222222 11111111111111111111111111111111 12345678
8960130501701553668F 502131300000098 11111111 1234 1234 22222222 11111111111111111111111111111111 12345678
-------------------------------

I have tried with this
#!/bin/csh
set imsi=`grep IMSI test.out | awk '{print $3}'`
foreach ki (sed -n 'var_out/,p' test.out | sed '1,2d' | awk '{print $7}'`)
echo "ZMAC:IMSI=${imsi}:KI=$ki,UV=50"
end
------------------
But the result was half of the command in multiple line - first half missing.

:KI=11111111111111111111111111111111,UV=50
:KI=11111111111111111111111111111111,UV=50
:KI=11111111111111111111111111111111,UV=50
:KI=11111111111111111111111111111111,UV=50
:KI=11111111111111111111111111111111,UV=50

But say I replace ${imsi} with a UNIX command such as `date` it work as it supposed to.
Where did I go wrong ?

Thanks.
Aminur Rizal Afip
B. Hulst
Trusted Contributor

Re: Echo and foreach loop problem

Hi,

Try not to use the /bin/csh but the /bin/sh.

csh has to many options, versions and implementations.

use

!#/bin/sh

Regards,
Bob
Aminur Rizal
Advisor

Re: Echo and foreach loop problem

OK Bob,
I will try that very soon. Its a long holiday here for the Chinese New Year- so no access to my system yet.

Thanks :)
Aminur Rizal Afip
B. Hulst
Trusted Contributor
Solution

Re: Echo and foreach loop problem

Hi,

I try always to take the very simple shell /bin/sh or C code or just perl script.

The sh shell is 99% the same in every OS.

Regards,
Bob
Hein van den Heuvel
Honored Contributor

Re: Echo and foreach loop problem


The script you post is riddled with typos.
To avoid confusion please only cut&paste. Do not re-type.

There is a missing open back-tick before 'sed'
There is a missing open slash for the sed pattern
There is a missing closing match ($) on that pattern.

This works for me:

set imsi=`grep IMSI test.out | awk '{print $3}'`
foreach ki (`sed -n '/var_out/,$p' test.out | sed '1,2d' | awk '{print $7}'`)
echo ZMAC:IMSI=${imsi}:KI=$ki,UV=50
end

Now just cut & past that!
This perl one line also works:

perl -ne '$imsi=(split)[2] if /IMSI/; $data=1 if /var_out/; print "ZMAC:IMSI=${imsi}:KI=".(split)[6].",UV=50\n" if ($data && ($i++ > 1))' test.out


But if you wrote it in perl you'd probably want to add more security for example:

$field = "KI";
while (<>) {
last if ($field_index);
$imsi=$1 if (/IMSI\s*:\s*(\S+)/);
if (/var_out/) {
@words = split("/");
while ($x = $words[$i++]) {
if ($x eq $field) {
$field_index = ++$i;
last;
}
}
}
}
die "No field $field found on line with var_out" unless $field_index;
while (<>) {
print "ZMAC:IMSI=$imsi:$field=".(split)[$field_index].",UV=50\n";
}


This will pick up the 'imsi' value with or without spaces around that colon.
And it will determine the right column for the select field from the var_out line.
(Admittedly, the way is skips the blank line is a hack :-).

Cheers,
Hein.
Neritan Omari
Occasional Advisor

Re: Echo and foreach loop problem

Hello ,

i found error with "foreach" statement so i used "for"

#!/bin/csh -f
imsi=`grep IMSI test.out | awk '{print $3}'`
PI=`sed -n '/var_out/,/^$/p' test.out | awk '{ print $7}'`
for ki in $PI
do
echo "ZMAC:IMSI=${imsi}:KI=${ki},UV=50"
done

and the printout is :

ZMAC:IMSI=5021313872800000096:PI=11111111111111111111111111111111,UV=50
ZMAC:IMSI=5021313872800000096:PI=11111111111111111111111111111111,UV=50
ZMAC:IMSI=5021313872800000096:PI=11111111111111111111111111111111,UV=50
Aminur Rizal
Advisor

Re: Echo and foreach loop problem

Hi All,
I manage to get it works now. As advised by Hein, I cut and paste the script from here and it works now.

Thanks a lot.
You guys are really great!
Aminur Rizal Afip