Simple variables

Discussion in 'Coding, Scripting & Programming' started by mitgib, Oct 15, 2015.

  1. mitgib

    mitgib New Member Verified Provider

    284
    219
    May 15, 2013
    I admit I am not the best at writing scripts, I need to write a single file with 2 lines repeated 100 times, 200 lines total, and set 3 variables 

    $vmid=vm{1..100}

    $ip= a single IP from a list (./ip.txt)

    $mac= a single MAC from a list (./mac-address.txt)

    set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping  $vmid ip-address $ip
    set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping  $vmid mac-address $mac

    So far I can get $vmid set and write the output to a file, but how do I get the other 2 set is where I am hitting a wall

    This is what I have tried, but it is putting the entire contents of each list into each line

    Code:
    #!/bin/sh
    ip=`cat ./ip.txt`
    mac=`cat ./mac-address.txt`
     for i in vm{1..100}; do
    echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i ip-address $ip >> /root/vyos2
    echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i mac-address $mac >> /root/vyos2
    done;
    
     
  2. tonyg

    tonyg New Member

    379
    134
    Aug 28, 2013
    Maybe try it using the Bourne shell (sh) way of "arrays":

    Code:
    #!/bin/sh
    for list in "ford mustang pinto galaxy" "chevy volt camaro corvette" "nissan 300ZX sentra maxima"
    do
       set $list
       echo "$1 $2 $3"
    done
     
  3. HalfEatenPie

    HalfEatenPie The Irrational One Retired Staff

    2,890
    1,385
    Mar 25, 2013
    HalfEatenPie
    It's been a while since I've last written a script, so I don't know how helpful this would be.

    Anyways it seems you're having an issue with importing the values of the text files as an array into the variable.  I'd check out this link: http://unix.stackexchange.com/questions/70934/bash-reading-txt-file-and-storing-in-array

    or http://stackoverflow.com/questions/11393817/bash-read-lines-in-file-into-an-array

    This is of course assuming your file (IPs and MACs) are all just one entry per new line and not using something else like a comma or some other form.
     
  4. sv01

    sv01 Slow but sure

    426
    87
    May 17, 2013
    #!/bin/sh
    ip=( $(cat ip.txt) )
    mac=( $(cat  mac-address.txt) )
    vm=3;
    for((i=0;i<vm;i++)); do
        echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i ip-address ${ip[$i]}
        echo set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping $i mac-address ${mac[$i]}
    doneresult
     

    Code:
    set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 0 ip-address IP1
    set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 0 mac-address MAC1
    set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 1 ip-address IP2
    set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 1 mac-address MAC2
    set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 2 ip-address IP3
    set service dhcp-server shared-network-name ETH1_POOL subnet 10.10.10.128/25 static-mapping 2 mac-address MAC3
     
    Last edited by a moderator: Oct 16, 2015
    mitgib and HalfEatenPie like this.
  5. mitgib

    mitgib New Member Verified Provider

    284
    219
    May 15, 2013
    Worked perfect, only change I made was setting i=1 so it didn't start at 0 and then vm=100 

    The way you are doing ip= and mac= was where I was stuck, so thanks!