obtain a new variable out of "for statment"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting obtain a new variable out of "for statment"
# 1  
Old 08-08-2006
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  
Old 08-08-2006
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  
Old 08-09-2006
could you help to explain what does it mean ??

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

thanks you !
# 4  
Old 08-09-2006
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  
Old 08-09-2006
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 06:47 AM..
# 6  
Old 08-09-2006
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.
# 7  
Old 08-10-2006
Quote:
Originally Posted by tmarikle
Why did you specify three periods in the IFS assignment? Only one is necessary.
I was'nt sure the way ksh use the IFS.
IFS is the list of internal field separators, so only one period is necessary.


Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

How to implement the logical express --- A(B+C) within single "if" statment in shell script?

On Linux OS, bash environment, I want implement the following code: if && ( || ) A,B,C represents some compare conditions. How to realize it ? Thanks! (1 Reply)
Discussion started by: qcmao
1 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

if [ "variable" = "numerical-range" ]; then

been a while so i'm a bit rusty and need a little help. writing a script that needs to compare $EXECHOST(a number) against a numerical range and then set a value. below isn't working but should give you folks an idea of my goal: if ; then echo "This is a 32B machine, exiting..." if ;... (4 Replies)
Discussion started by: crimso
4 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Advanced & Expert Users

"Unable to obtain clipboard ownership"

Help! I work on a PC running Windows XP. I have installed the Hummingbird Suite Exceed 10.0. I launch 4 Xconfig sessions based on different *.cfg files along with the standard Xserver Xconfig (Exceed.cfg). I do this because I need to run graphical apps in separate windows. In any event,... (0 Replies)
Discussion started by: richmeeker
0 Replies
Login or Register to Ask a Question