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
fi

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

PROMPT_DIRTRIM=3
export PROMPT_DIRTRIM

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.

RPM_BUILD_DIR=""
if [ -n "$1" ]
then
   RPM_BUILD_DIR=${1%/} #REMOVE TRAILING /
fi

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:
"/usr/local/bin"

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.

#!/bin/bash
#set -x
echo -----------------BEGIN------------------------
servers="10.1.1.1:8080,10.1.1.2:8080,10.1.1.3:8080"
declare -a serversArray=("10.1.1.1:8080" "10.1.1.2:8080" "10.1.1.3:8080")

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.
result1=
arrayIndices=(${!serversArray[*]})
for index in ${arrayIndices[*]}
{
   result1=${result1}${serversArray[$index]}
   if [ $index != ${arrayIndices[*]: -1} ]   #This gets the last element of an array
   then
      result1=$result1, #only append comma if this isn't the last entry
   fi
}
echo Array to comma seperated string using for loop:" "${result1}

#convert array to comma delimited string using character substitution
cds=${serversArray[@]};
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
servers2=${servers//:????/}
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
echo 10.1.1.0:{10001,10002,10003,10004}
echo 10.2.2.2:{10001..10004}
echo 10.1.1.0:{10001,10002,10003,10004},
#capture echo into var and strip the trailing comma
ips=$(echo 10.1.1.0:{10001,10002,10003,10004},)
ips=${ips/%,/}
echo ${ips}







Here is the output:
-----------------BEGIN------------------------
servers:      10.1.1.1:8080,10.1.1.2:8080,10.1.1.3:8080
serversArray: 10.1.1.1:8080 10.1.1.2:8080 10.1.1.3:8080
----------------------------------------------
Array to comma seperated string using for loop: 10.1.1.1:8080,10.1.1.2:8080,10.1.1.3:8080
Array to comma seperated string using character substitution:   10.1.1.1:8080,10.1.1.2:8080,10.1.1.3:8080
Comma delimited string to array:   10.1.1.1:8080 10.1.1.2:8080 10.1.1.3:8080
[]10.1.1.1:8080
[]10.1.1.2:8080
[]10.1.1.3:8080
----------------------------------------------
Remove Port Numbers from IP Addresses
10.1.1.1,10.1.1.2,10.1.1.3
10.1.1.1,10.1.1.2,10.1.1.3
----------------------------------------------
Creat ip addresses with incrementing port numbers
10.1.1.0:10001 10.1.1.0:10002 10.1.1.0:10003 10.1.1.0:10004
10.2.2.2:10001 10.2.2.2:10002 10.2.2.2:10003 10.2.2.2:10004
10.1.1.0:10001, 10.1.1.0:10002, 10.1.1.0:10003, 10.1.1.0:10004,
10.1.1.0:10001, 10.1.1.0:10002, 10.1.1.0:10003, 10.1.1.0:10004