• Navigation
Results 1 to 5 of 5
  1. #1
    Ridill
    Join Date
    Jul 2008
    Posts
    11,249
    BG Level
    9

    Shell script to mimic python Dictionary Lists

    So rewriting all of this applescript to shell script just to make it easier. I'm at a part where I need to run a loop for the macaddress of the machine and give it a computername based on the mac. An if/elif loop works, but thats a lot of iterations (about 40-50 machines).

    Is there anyway to accomplish this via a for loop? I know python has dictionary lists, where I can basically get a value from the index in the list and apply it. So I could list it as {MacaddressA: [ComputernameA], MacaddressB: [ComputernameB]} and just grab w/e name was related to w/e mac address. Havn't been able to think of a viable way to do this though in bash.

  2. #2
    Sea Torques
    Join Date
    Oct 2006
    Posts
    704
    BG Level
    5
    FFXI Server
    Carbuncle

    This may be an idea.
    If maclist.txt is a comma seperated list like:
    MacaddressA,ComputernameA
    MacaddressB,ComputernameB
    then the following should work.

    Code:
    LOCALMAC=`ifconfig |grep HWaddr |awk {'print $5'}`
    for i in `cat maclist.txt | awk -F , {'print $1'}`
    do
    if [ "$LOCALMAC" = "$i" ]
    then
    HOSTNAME=`cat maclist.txt |grep $i |awk {'print $2'}`
    fi
    done
    echo HOSTNAME is $HOSTNAME
    Edit: LOCALMAC variable will be determined differently based on OS type, but the rest of the script should be *nix independant

  3. #3
    Ridill
    Join Date
    Jul 2008
    Posts
    11,249
    BG Level
    9

    Thanks Bard, doesnt work as a comma seperated, I think awk looks for space breaks so if I put it as "MACADD HOSTNAME" it seems to work fine

    looks good. helps a lot.

  4. #4
    Sea Torques
    Join Date
    Oct 2006
    Posts
    704
    BG Level
    5
    FFXI Server
    Carbuncle

    The -F , changes awk's field separator from space to a ,. I added it in the first awk command, but forgot it in the HOSTNAME variable. It should have looked like this:
    Code:
    HOSTNAME=`cat maclist.txt |grep $i |awk -F , {'print $2'}`
    complete corrected code:
    Code:
    LOCALMAC=`ifconfig |grep HWaddr |awk {'print $5'}`
    for i in `cat maclist.txt | awk -F , {'print $1'}`
    do
    if [ "$LOCALMAC" = "$i" ]
    then
    HOSTNAME=`cat maclist.txt |grep $i |awk -F , {'print $2'}`
    fi
    done
    echo HOSTNAME is $HOSTNAME

  5. #5
    Ridill
    Join Date
    Jul 2008
    Posts
    11,249
    BG Level
    9

    Ahh ok that would be the reason. Either way works as i'm going to echo out the lists to the file within the script and then delete the file after the script runs through that part. Thanks for the help.

Similar Threads

  1. Shell scripting
    By #686578 in forum Tech
    Replies: 7
    Last Post: 2009-02-26, 19:52
  2. Shell Scripting
    By #686578 in forum Tech
    Replies: 7
    Last Post: 2009-02-14, 11:51