![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" | iBot | UNIX and Linux RSS News | 0 | 01-04-2008 03:00 PM |
| Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" | Lokesha | UNIX for Dummies Questions & Answers | 4 | 12-20-2007 01:52 AM |
| "Unable to obtain clipboard ownership" | richmeeker | UNIX for Advanced & Expert Users | 0 | 07-26-2007 09:38 AM |
| how could i make a program mixed with many "|", "<" and ">" | strugglingman | High Level Programming | 2 | 04-29-2006 08:11 AM |
| No utpmx entry: you must exec "login" from lowest level "shell" | peterpan | UNIX for Dummies Questions & Answers | 0 | 01-18-2006 04:15 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
obtain a new variable out of "for statment"
first i have pre-define some variable, then input those into a for statement for some process, finally, i want to get a new value out of it.
A=192.168.16.1 B=192.168.32.1 C=192.168.64.1 for i in A B C do echo $i | nawk -F. '{ if ($3 > 16 and < 32) ....something like that, then ( $i= 192.168.202.1) , if ($3>16 and < 64) , then $i =192.168.203.1)....}' done echo $A <-- now how can i get the new value for other part of the shell to use. echo $B echo $C sorry for the middle process, because i still trying to figure it out how to write the condition statement inside it.... But first i need to find out whether i am in a wrong direction. Many thanks ! |
|
||||
|
Maybe not what you are expecting but it meets the requirement as I read it:
Note: this works with ksh and bash Code:
{
unset A B C
A=192.168.16.1
B=192.168.32.1
C=192.168.64.1
# Add "var=" so that the code has something to work with following nawk's work
for i in A=$A B=$B C=$C
do
# Use eval to make assignemnt
# ...this will translate into var=nawk's output
eval ${i%%=*}=$(echo "${i##*=}" | nawk -F. '{
if ($3 >= 16 && $3 < 32)
print "192.168.202.1"
else
print
}')
done
echo A=$A
echo B=$B
echo C=$C
}
Code:
A=192.168.202.1 B=192.168.32.1 C=192.168.64.1 |
|
|||||
|
Another way to do the work :
Code:
{
unset A B C
A=192.168.16.1
B=192.168.32.1
C=192.168.64.1
for i in A=$A B=$B C=$C
do
echo $i | IFS="=..." read Var f1 f2 f3 f4
if [ $f3 -gt 16 -a $f3 -lt 32 ] ; then
eval $Var=192.168.202.1
elif [ $f3 -gt 16 -a $f3 -lt 64 ] ; then
eval $Var=192.168.203.1
fi
done
echo A=$A
echo B=$B
echo C=$C
}
Jean-Pierre. Last edited by aigles; 08-09-2006 at 05:47 AM.. |
|
||||
|
Quote:
Why did you specify three periods in the IFS assignment? Only one is necessary. |
![]() |
| Bookmarks |
| Tags |
| bash, bash eval, eval |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|