Berkeley CSUA MOTD:Entry 50557
Berkeley CSUA MOTD
2018/10/20 [General] UID:1000 Activity:popular

2008/7/14-16 [Computer/SW/Languages/Perl, Computer/SW/Unix] UID:50557 Activity:moderate
7/14    Shell Programming question: I want to call a script with 1 arg
        and have it figure out whether $1 is a MAC address or an IP address
        and then do call the appropriate function.  What is the best way
        to do this, given that sh/bash/ksh do not have something like
        the =~ in perl.  Check for exit status of grep, or is there a
        a better way?  For the moment, let's just say the two tests are:
          echo $1 | fgrep : > /dev/null && echo isMAC || echo notMAC
          echo $1 | fgrep . > /dev/null && echo isIP || echo notIP
          and if neither, then Usage()
        I want to avoid requiring the user to either do "-i IP" or "-m MAC".
        Note: this doesn't have to do extensive syntax checking ... it's
        not a security application.  Just trying to save typing.
        \_ You may want to recheck your assumption of bash not having =~.
           \_ Thanks for that information.  I didn't know know about that.
              Although right now it is in sh.  Any portable pure sh ideas?
              I'm really interested in what is the "right" way to do this,
              rather than just coming up with something which works.
              \_ "right" depends on your situation. If you actually need
                 to support old systems, or be portable across many systems,
                 then maybe you should stick with sh. If you know your
                 audience is all going to have bash or whatever (and that
                 is not at all unlikely these days), then I see no good
                 reason to suffer under older, less capable tools.
              \_ Dude, it's a sh script.  If it works, it works.  Seriously.
                 Stick it in a function if it looks too ugly and then forget
                 about it.  If you need something to be maintainable or
                 it's a called enough there is any concern about it being
                 too slow you are going to have to bite the bullet and use
                 a reasonable scripting language.  Otherwise just make it work
                 and move on to something that is worth spending time on.
              \_ I think "right" in this case, means readable and maintainable
                 by the next poor guy that has to come along. Just document
                 your code and you will be fine.
              \- I think one way to distill the question is "how do i get =~
                 in sh" ... would you do "echo $string | egrep <regexp>" + $?
                 to get exit status of the grep ... I think the case approach
                 below is nice and clean as long as you are ok with something
                 like *:*:*:*:*:* for MACs and *.*.*.* for IPs, and dont need
                 "true" regexps.
                 "true" regexps. I wouldnt use awk to test, but maybe if you
                 need to mangle.
        \_ Why would you do this in a shell progam?
        \_ use case:

        case "$1" in
             *:*)    echo "isMAC"; found="YES";;
             *.*)    echo "isIP"; found="YES";;
             *)      if [ "$found" = "NO" ] ; then
                        echo "NOT MAC" ; echo "NOT IP";
2018/10/20 [General] UID:1000 Activity:popular

You may also be interested in these entries...
2012/9/20-11/7 [Finance/Investment, Computer/SW/Unix] UID:54482 Activity:nil
9/20    How do I change my shell? chsh says "Cannot change ID to root."
        \_ /usr/bin/chsh does not have the SUID permission set. Without
           being set, it does not successfully change a user's shell.
           Typical newbie sys admin (on soda)
           \_ Actually, it does: -rwsr-xr-x 1 root root 37552 Feb 15  2011 /usr/bin/chsh
2012/9/24-11/7 [Computer/SW/Languages, Computer/SW/Unix] UID:54484 Activity:nil
9/24    How come changing my shell using ldapmodify (chsh doesn't work) doesn't
        work either? ldapsearch and getent show the new shell but I still get
        the old shell on login.
        \_ Scratch that, it magically took my new shell now. WTF?
           \_ probably nscd(8)
2012/4/27-6/4 [Computer/SW/Languages/Misc, Computer/SW/Unix] UID:54372 Activity:nil
4/27    I wrote a little shell script to collect iostat data:
        DATE=`date +%m%d`
        while true
2011/10/26-12/6 [Computer/SW/Unix] UID:54202 Activity:nil
10/24  What's an easy way to see if say column 3 of a file matches a list of
       expressions in a file? Basically I want to combine "grep -f <file>"
       to store the patterns and awk's $3 ~ /(AAA|BBB|CCC)/ ... I realize
       I can do this with "egrep -f " and use regexp instead of strings, but
       was wondering if there was some magic way to do this.
       \_ UNIX has no magic. Make a shell script to produce the ask or egrep
2011/5/19-7/13 [Computer/SW/Languages/Misc] UID:54115 Activity:nil
5/19    If script A runs, and calls script B ..... is it possible for me to exit\
        script A based on results of script B and not continue?
        \_ assume any shell
        \_ Yes.
           \_ without passing the result to some stupid temp file?
              \_ It sounds like you want "scriptb || exit", which will run
2011/3/12-4/20 [Consumer/CellPhone, Computer/HW/Laptop] UID:54057 Activity:nil
3/12    I am curious what others think of tablets like iPad. They don't seem
        useful to me, but I use my computer for more than web browsing,
        Facebook, and Twitter. Why would I buy one instead of a laptop?
        They seem like a disabled laptop to me, but at a higher price.
        \_ You are most likely a coder.  iPad is not for coders.  They are
           what you get your non-technical friends.  Or musicians.  Look at
2011/2/6-19 [Computer/Networking] UID:54028 Activity:nil
2/5     hmm.
$netstat -at | grep LISTEN
tcp        0      0 *:43300                 *:*                     LISTEN
        \_ this is an sshd
tcp        0      0 *:49416                 *:*                     LISTEN
tcp        0      0 *:36201                 *:*                     LISTEN