Find the answer to your Linux question:
Results 1 to 9 of 9
Hi. I'm new to this forum but not new to linux. I have been using redhat for my home server since enigma 7.2. I am self taught and can get ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2005
    Location
    Toronto
    Posts
    9

    Is there an easier way to set these variables?


    Hi. I'm new to this forum but not new to linux. I have been using redhat for my home server since enigma 7.2. I am self taught and can get the OS to do most anything I want but sometimes I wonder if there are simpler/better ways.

    Now to the question... I have a custom iptables firewall that assigns different variables from the leases file that dhclient creates. For example, this is the script command I am using to assign the DHCP servers address:

    DHCP_SERVER=$(tac $FILE | grep '-m' '1' 'dhcp-server-identifier' | awk '{print $3}' | sed -e 's/;//')

    For those of you who do not use dhclient and have not seen the format of the leases file, here is that line:

    option dhcp-server-identifier aaa.bbb.ccc.ddd;

    The leases file contains a few of the most recent leases given where the last one in the file is the most current. I just checked and my leases file now has 4 leases in it. The above line works but I was wondering if there is an easier way? It needs to grab the aaa.bbb.ccc.ddd from the last lease in the file.

    Just curious if there is an easier way?
    Thanks.

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    the only different way of doing it is substituting
    Code:
    cut -f3 -d ' '
    instead of awk, but awk is quicker

  3. #3
    Just Joined!
    Join Date
    May 2005
    Location
    Toronto
    Posts
    9

    Thanks

    Thanks for the reply.

    Sometimes there are hundreds of ways to do something and you wonder if there are much simpler methods. Good to know I'm not completely out to lunch.

    Thanks again.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    You can try
    Code:
    awk ' $0 ~ /dhcp-server-identifier/  {a=$3} END { print $a } ' $FILE | cut -d\; -f1

  6. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Quote Originally Posted by lakerdonald
    the only different way of doing it is substituting
    Code:
    cut -f3 -d ' '
    instead of awk, but awk is quicker
    I would imagine that cut is faster than awk
    because it's a much simpler command.

  7. #6
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Code:
    awk '{print $3}'
    is less to type than:
    Code:
    cvs -f3 -d ' '

  8. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Cut down on the process hoggery:
    Code:
    DHCP_SERVER=$&#40;sed -n 's/.*dhcp-server-identifier \&#40;&#91;^;&#93;*\&#41;;/\1/p' <$FILE&#41;

  9. #8
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    That's a kludge if i've ever seen one :P

  10. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by lakerdonald
    That's a kludge if i've ever seen one :P
    Why, thank you, sir!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •