Thursday, February 11, 2016

Personalized Bash Shell, VIM, and other settings...

I want to capture my very simple, yet I think clean, settings I like to use.

Here is my .bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc

# User specific aliases and functions
LS_COLORS='di=1;33' ; export LS_COLORS


PS1="[\u \w]\$"


This sets some colors and makes the prompt show the directories in a way that I find useful because I am always deep into the directory structure.

[user1 ~/.../opt/solr-5.4.1/example]$

Here is my .vimrc

colorscheme desert

BASH Script to remove trailing back slash

I was working with a script that was called in an RPM build (in the SPEC file). I passed a directory as a the first variable to the script. The directory ended with a back slash. I needed to remove that backslash since all of my variables started with a backslash.

if [ -n "$1" ]

So, the bash script checks to see if we have a parameter in the first position and if so remove the trailing backslash.

If the directory string was "/usr/local/bin/" the results are:

I searched for examples to do this and they all seemed overly complex. Maybe this one is too simple for some situations, but it works for my needs.

Thursday, February 04, 2016

BASH Script to Manipulate IP Addresses and Port Numbers

This post is just a place to store some bash script for reference. I don't work in bash often enough to remember how things work so I need to make little snippets and such for reference. I do the same thing for regex and sql.

Here are some of the things I do to manipulate ip addresses in bash scripts. Since I work a lot with Solr, specifying zookeeper machines and lists of servers for SolrCloud I am always manipulating IP addresses.

#set -x
echo -----------------BEGIN------------------------
declare -a serversArray=("" "" "")

echo servers:"      "${servers}
echo serversArray:" "${serversArray[@]}

echo ----------------------------------------------

#use for loop to convert the serversArray to a comma seperated string
#Since arrays can be sparse, that is their indices do not have to be sequential
#get the indices as an array and iterate over the indices.
for index in ${arrayIndices[*]}
   if [ $index != ${arrayIndices[*]: -1} ]   #This gets the last element of an array
      result1=$result1, #only append comma if this isn't the last entry
echo Array to comma seperated string using for loop:" "${result1}

#convert array to comma delimited string using character substitution
cds=${cds// /,}
echo Array to comma seperated string using character substitution:"   "${cds}

#convert the comma delimited string into an array
atemp=($(IFS=','; x=($servers); echo ${x[@]}))
echo Comma delimited string to array:"   "${atemp[@]}
for a in "${atemp[@]}"
   echo []"${a}"
echo ----------------------------------------------

#remove the port from the servers, only works for port numbers of 4 digits
echo Remove Port Numbers from IP Addresses
echo ${servers2}

#now use sed to remove port numbers from servers
echo ${servers} | sed -e "s/:[0-9]*//g"

echo ----------------------------------------------
#create an ip address with incrementing port numbers
echo Creat ip addresses with incrementing port numbers
#capture echo into var and strip the trailing comma
echo ${ips}

Here is the output:
Array to comma seperated string using for loop:,,
Array to comma seperated string using character substitution:,,
Comma delimited string to array:
Remove Port Numbers from IP Addresses,,,,
Creat ip addresses with incrementing port numbers,,,,,,,