The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-08-2006
3Gmobile 3Gmobile is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 18
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 !
  #2 (permalink)  
Old 08-08-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
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
}
Output:
Code:
A=192.168.202.1
B=192.168.32.1
C=192.168.64.1
This can also be done without using awk.
  #3 (permalink)  
Old 08-08-2006
3Gmobile 3Gmobile is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 18
could you help to explain what does it mean ??

${i%%=*}=$(echo "${i##*=}

thanks you !
  #4 (permalink)  
Old 08-09-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Quote:
Originally Posted by 3Gmobile
could you help to explain what does it mean ??

${i%%=*}=$(echo "${i##*=}

thanks you !
${parameter%%pattern} and ${parameter##pattern} work in the same way but difference is that '%%' matches the end of the value of parameter and ## matches the begining of the value of pattern. Lets explain it with example.

In the first iteration, value of i is A=192.168.16.1, ${i%%=*} will match from the end of the value of i, until it finds '=' and will delete the matched pattern, so in this case it'll match '=192.168.16.1' and will delete it, yeilding 'A'.

${i##*=} will match from the begining of the value of i, until it finds '=' and will delete the matched pattern, so in this case it'll match 'A=' and will delete it, yeilding '192.168.16.1'

So ${i%%=*} will output 'A' and ${i##*=} will output '192.168.16.1'

Regards,
Tayyab
  #5 (permalink)  
Old 08-09-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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..
  #6 (permalink)  
Old 08-09-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Quote:
Originally Posted by aigles
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.
Yep, that will run faster (4x on my system) for a single line, I wasn't sure what the OP's original goal was for including awk but the general flow of his post's code indicates single lines of input.

Why did you specify three periods in the IFS assignment? Only one is necessary.
Closed Thread

Bookmarks

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0