Bash Shell: Find Nano or Pico May 2, 2008
It’s amazing how little things make all the difference when doing your day-to-day sysadmin tasks. I tend to use the same .profile on lots of *NIX boxes, but have always found it frustrating to find a sane editor in different environments. My editor of choice in Nano/Pico (I’m not ashamed), but this is not always in the path. I finally got around to adding functionality in my default bash .profile that looks in different places for Nano or Pico and adds an alias to the full-path binary, sets my EDITOR and VISUAL env variables and aliases one to the other if it can find one. I’ve tested this script on Solaris, Darwin and Linux platforms. If you like Nano/Pico and are frustrated with not having a consistent editor on different machines, you may find this useful to add to your .profile.
You should just be able to cut-n-paste this into your .profile. The only thing you may need to change is the EDITOR_LOCATIONS array, to add locations to where these binaries are commonly found on different systems that you work with.
####### ####### ### Begin: Nano/Pico search: Look for NANO or PICO in common locations, add a full path location as an alias to both or one-of them. ### Also, make my environment variable EDITOR and VISUAL equal to the pico/nano binary if one exists ####### ####### PROFILE_VERSION=0.1 PROFILE_STRING="[Bash Profile v$PROFILE_VERSION]" NANO_LOCATION=`which nano 2>&1` PICO_LOCATION=`which pico 2>&1` NANO_LOC_FIRSTCHAR=${NANO_LOCATION:0:1} PICO_LOC_FIRSTCHAR=${PICO_LOCATION:0:1} if [[ "$NANO_LOC_FIRSTCHAR" != "/" && "$PICO_LOC_FIRSTCHAR" != "/" ]]; then EDITOR_LOCATIONS=(/bin/ /var/ /usr/bin/ /usr/local/bin/ /opt/bin/ /opt/local/bin/ /sbin/) EDITOR_DIR="" for ((i=0;i<${#EDITOR_LOCATIONS};i++)); do if [ -x ${EDITOR_LOCATIONS[${i}]}/nano ]; then NANO_LOCATION=${EDITOR_LOCATIONS[${i}]}nano NANO_LOC_FIRSTCHAR=${NANO_LOCATION:0:1} EDITOR_DIR=${EDITOR_LOCATIONS[${i}]} echo "$PROFILE_STRING Found nano in ${EDITOR_DIR} !" >&2 fi done for ((i=0;i<${#EDITOR_LOCATIONS};i++)); do if [ -x ${EDITOR_LOCATIONS[${i}]}/pico ]; then PICO_LOCATION=${EDITOR_LOCATIONS[${i}]}pico PICO_LOC_FIRSTCHAR=${PICO_LOCATION:0:1} EDITOR_DIR=${EDITOR_LOCATIONS[${i}]} echo "$PROFILE_STRING Found pico in ${EDITOR_DIR} !" >&2 fi done fi if [[ "$NANO_LOC_FIRSTCHAR" != "/" && "$PICO_LOC_FIRSTCHAR" == "/" ]]; then echo "$PROFILE_STRING Only pico found ($PICO_LOCATION), adding alias for nano." alias pico=$PICO_LOCATION alias nano=$PICO_LOCATION export EDITOR=$PICO_LOCATION export VISUAL=$PICO_LOCATION fi if [[ "$NANO_LOC_FIRSTCHAR" == "/" && "$PICO_LOC_FIRSTCHAR" != "/" ]]; then echo "$PROFILE_STRING Only nano found ($NANO_LOCATION), adding alias for pico." alias pico=$NANO_LOCATION alias nano=$NANO_LOCATION export EDITOR=$NANO_LOCATION export VISUAL=$NANO_LOCATION fi if [[ "$NANO_LOC_FIRSTCHAR" == "/" && "$PICO_LOC_FIRSTCHAR" == "/" ]]; then echo "$PROFILE_STRING Nano and Pico already in path (nano in $NANO_LOCATION; pico in $PICO_LOCATION)." alias pico=$NANO_LOCATION alias nano=$PICO_LOCATION export EDITOR=$NANO_LOCATION export VISUAL=$NANO_LOCATION fi if [[ "$NANO_LOC_FIRSTCHAR" != "/" && "$PICO_LOC_FIRSTCHAR" != "/" ]]; then echo "Nano or pico not found. Yuck." export EDITOR=vi export VISUAL=vi fi ####### ####### ### End: Nano/pico search ####### #######
Output
[0931][rob@machine:~]$ source ./.profile [Bash Profile v0.1] Found pico in /opt/bin/ ! [Bash Profile v0.1] Only pico found (/opt/bin/pico), adding alias for nano. [0931][rob@machine:~]$
Leave a Reply