2

A set of servers that I SSH into use a switch statement in the default .bashrc to make system-specific configurations:

case "$HOSTNAME" in:
    systemA*|systemB|)
        # some local definitions here
        ;;
    systemC*|systemD|)
        # some local definitions here
        ;;
    *)
        echo "No bashrc definitions for $HOSTNAME"
   esac

But I also use bash on my Mac locally, so I decided to be a smartarse and extend this statement to include a case for when I'm not SSHing into another statement.

However, the Mac often changes my preferred hostname based on my network environment (usually when I'm wired in at work, rather than on Wi-Fi at home, but I assume this is down to the DHCP configuration). I'm aware that I can override it, but is the best idea? Is there a better solution for having bashrc settings that are only in effect when I'm not in a remote shell?

  • 1
    Put your code in an if under the *) and use something other than host name as the signature of your mac. – Stephen Rauch Apr 6 '17 at 3:07
  • @StephenRauch do you have a preferred other indicator? This answer seems like it might be the way to go. – rensa Apr 6 '17 at 3:18
  • 1
    Yeah that answer looks like it would meet your use case... – Stephen Rauch Apr 6 '17 at 3:22
  • 2
    Use the MAC address of your Mac. – ivanivan Apr 6 '17 at 4:30
2

You could do this:

case "$HOSTNAME" in: 
    systemA*|systemB)
        # some local definitions here 
        ;;
    systemC*|systemD)
        # some local definitions here 
        ;; 
    *) 
        if [ "$(uname)" = "Darwin" ]; then
            echo "I'm on a Mac!"
        else
            echo "No bashrc definitions for $HOSTNAME" 
        fi
esac

uname will give you a whole bunch of basic info about the system you're on. uname --help for options.

Update:

Alternatively (and this is what I do, which you can see here at the end of my .bashrc) what you can do is create .bashrc_local_Darwin for Mac specific configuration and a .bashrc_local_Linux for Linux specific stuff. These can go into version control as they are still general to all macs and all linux boxes you connect to. So what you do then is create a .bashrc_local on each box you want custom configuration only for that box which you don't put into version control. You then source these files if they exist, et volià, you have a pretty neat system with out the cludgy and hard-to-maintain case...esac statement.

  • This seems like a great idea! I assume it'd be problematic if I was connecting to a macOS Server, but that's not the case for me (they're all Linux). – rensa Apr 6 '17 at 3:52
  • I've updated my answer with another solution which I think is heaps better. – Jacob Degeling Apr 6 '17 at 4:46
  • This is wrong, -eq is numeric comparison. Use = instead. The two empty conditions |) in case also look fishy. – Satō Katsura Apr 6 '17 at 5:54
  • Updated the equality test and the case statements... – Jacob Degeling Apr 6 '17 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.