Archive for the ‘Scripts’ Category

Functions in bash/sh scripts

March 23, 2012

Its pretty simple to work with functions of commandline bash/sh scripts.

Define function

This is how you define a function
func_name(){     
  <bash command_1>     
  ....     
  <bash command_n>
}
eg:
say_hello(){  
  echo "Hello World"
}

Calling the function

In order to call the function the function needs to be defined as above before you actually call it. Then you just use the name of the function
eg:
say_hello(){
  echo "Hello World"
}

say_hello

Passing parameters to the function

Passing parameters to the function is pretty weird. Its the same as parameters being passed to a script. The function will have its own set of $* variable where the 1st parameter is $1, the next is $2 and so on.
eg:
say_hello(){
  echo "Hello $1 $2"
}

say_hello "Saminda" "Wijeratne"

Defining variables inside a function

Defining variables inside function is same as how you’d do usually in a script. Only difference is that if you want the variable to be local to the function you need to use the keyword “local” when defining it.
eg:
say_hello(){
  local first_name=$1
  last_name=$2           #becomes a global variable
  echo "Hello $first_name $middle_name $last_name"
}

middle_name="Saminda"               #global variable
say_hello "Kushan" "Wijeratne"
echo $firt_name
echo $last_name
Passing a string as a variable is easy. But passing an array to a function is pretty tricky. Here’s an example on how to do it.
eg:
say_hello(){
 eval "declare -A bio_data_array="${1#*=}
 #now bio_data_array is the array that was passed as a parameter
 echo "Hello ${bio_data_array['name']}"
}

declare -A bio_data
bio_data["name"]="Saminda"
bio_data["age"]=28
say_hello "$(declare -p bio_data)"

Returning values from the function

You’ll be surprised to know that bash functions never return anything. However you can capture what was printed by the function in the form of a string using the cool feature in bash where you can execute commands within “`” characters which the result is the output given by that command.
eg:
output_string=`say_hello $firt_name`
echo "This is what the say_hello function had to say: $output_string"
There are several other ways like using global variables or pass a variable name as a parameter to save the result of the function inside the function itself. But I prefer the above method since the function doesn’t have to worry about whether or not its a function or not.

Search & Replace Strings Considering the whole file (non line-based) using ‘sed’ command

August 15, 2009

This was really stupid of ‘sed’ command. I had a file which had 2 occurances of the same string in 2 different lines and i wanted only the first to be replace by some other string. Took me a while to figure out why the conventional sed command doesnt work. (Incase you are wondering its because sed is a line-based command. i.e. reads a line from input, apply the whole sed logic and the mentioned operations in it, print the output and repeat the whole process for the rest of the input lines).

so anyway what you have to do is pretty simple. either you have to format your input to remove the new line characters and feed that as the input and somehow reintroduce the newline character after sed was applied, or feed the whole input to sed in a non-line-based form. Anyway found this on the net. http://linux.dsplabs.com.au/rmnl-remove-new-line-characters-tr-awk-perl-sed-c-cpp-bash-python-xargs-ghc-ghci-haskell-sam-ssam-p65/.

So coming back to what I wanted to do,

$ cat myfile | sed ‘:a;N;$!ba;s/originalstring/replacestring/’

notice the bolded characters added in the sed command. They are the extra addition to the conventional sed command we use for seach and replace strings.

How to trim a string using linux bash script

July 3, 2009

This is pretty simply done using the sed command. All you have to know is regular expressions.

To remove leading white spaces

sed 's/^ *//g'

To remove trailing white spaces

sed 's/ *$//g'

So as an example,

$ echo "  aaaa   bbbb      " | sed 's/^ *//g' | sed 's/ *$//g'
aaaa   bbbb

Thats it.

patch command in linux to apply a svn patch

June 3, 2009

First of all how to create a patch if you dont know…
$ svn diff mychangedfile1 dir1/dir2/mychangeresource2 > mypatch.txt

To apply this patch it needs to be in the same svn location as the patch was taken. Then using the patch command u apply the patch
$ patch -p0 < patch.txt

.bashrc is not started automatically

March 27, 2009

This is a common misconception that users think .bashrc is called each time login shell is created etc. There is some truth behind this but need to look at this more closely. For now what i found out is that bashrc in the /etc folder is definitely called also ~/bash_profile  also plays a major role.  But not the .bashrc.

So all you have to do it seems create the ~/.bash_profile if it is not already there

add the following line to it.

if [ -f ~/.bashrc ]; then source ~/.bashrc ; fi

and thats it.

Create and access shell variable having a name created by another string

February 26, 2009

This took sometime to find. Apparently this was not something possible with any direct way. I wanted to have 2 dimentional array but in the end i decided to use dynamic strings to create variables which would simulate the 2d array.

Say i want n variables like “test1,test2,test3 …… testn” where n is decided at the time the script it running. So what I should do to create the variable is pretty simple. I will show you how to create the nth variable.

say,

$ n=123
$ let test$n=$n*2

at this point there is a variable now ‘test123’ and it have the value 246. to get the value is a little tricky. This is what took me a lot of time to find. In the end i found the solution in http://www.usenet-forums.com/linux-general/78879-shell-script-variable-variable-name.html.

First you have to create a variable which contains our dynamic variable name.

$ varName=test$n

Now the variable “varName” contains our  dynamic variable name. If you print the value of this variable

$ echo ${varName}
test123

but if you specify “!” mark infront of our variable then the value denoted by the variable whos name is the value of “varName” is shown.

$ echo ${!varName}
246

well thats it actually. Hope this saved some time for someone.

How to save the output of a shell command to a variable

February 25, 2009

This is fairly easy to do compared to the amount of trouble i had to go through to find it. All you have to do is to surround the shell command with the “`” character (this is not the single quote character) and assign the whole string to the variable name.

eg:
$ n=`ls`
$ echo $n
pom.xml src target

Its as simple as that. Have fun.

How to findout what ports are free using a linux shell script

February 25, 2009

Well it seems there is no one single command to do this. But what we can find out is already open ports. Using this we can find free ports by looping through port numbers.

Well the command is this:

$ lsof -iTCP:32000

The above command will search for the open port 32000 running the TCP protocol. If the port 32000 is open then there will be an output with the relevant details, else there will be no output. To use this information programatically we can do the following.

n=`lsof -iTCP:32000`

if [ "$n" != "" ]
then
    echo "Port is being used"
else
    echo "Port is free"
fi

The variable takes the output and if it is a empty string then the port is free else it is being used. (might be a better way of doing this check). All you have to do is to wrap this code around a loop to test the required port.

Include a shell script file in to another schell script like in #include in C

February 24, 2009

This took a little time to find. But apparently this is very easy. I wanted to share some data in a shell script among some other shell script. So the obvious this to do is to include that shell script inside other shell scripts. To do this we have to use the “source” command.

eg:
!/bin/bash
echo "This is my script"
....
source otherscript.sh
....
echo "Called the other script"

What source command does is just execute the shell script and sharing the execution environment of the currently executing script. If you just call the script as ./otherscript.sh it will just execute with its own stack.

Search files inside an archive (zip/jar/aar) using command line

November 6, 2008

Many times we need to find some file inside a compressed file. In my case this is when I want to find which jar file consists of a particular class. If you are in linux this can be done very easily. All you have to do is write yourself a small script to do the job for you.

Checkout the following in findjar.sh file

find . -name ‘*.jar’ -exec grep $1 {} \; -print

after saving the file we can find any class inside a jar file from the current directory.

saminda@saminda-laptop:~/.m2/repository$ findjar org.apache.axis2.engine.AxisEngine
Binary file ./org/wso2/carbon/org.wso2.carbon.axis2/1.4/org.wso2.carbon.axis2-1.4.jar matches
./org/wso2/carbon/org.wso2.carbon.axis2/1.4/org.wso2.carbon.axis2-1.4.jar
Binary file ./org/wso2/carbon/org.wso2.carbon.axis2/1.4.1-RC1/org.wso2.carbon.axis2-1.4.1-RC1.jar matches
./org/wso2/carbon/org.wso2.carbon.axis2/1.4.1-RC1/org.wso2.carbon.axis2-1.4.1-RC1.jar
Binary file ./org/wso2/carbon/org.wso2.carbon.axis2/1.4.1/org.wso2.carbon.axis2-1.4.1.jar matches
./org/wso2/carbon/org.wso2.carbon.axis2/1.4.1/org.wso2.carbon.axis2-1.4.1.jar
Binary file ./org/wso2/carbon/org.wso2.carbon.axis2/1.401/org.wso2.carbon.axis2-1.401.jar matches
./org/wso2/carbon/org.wso2.carbon.axis2/1.401/org.wso2.carbon.axis2-1.401.jar
...
...

Ofcourse the above script is only for searching jar files. If you want to search for zip files all you have to do is the change the value of the name parameter specified for the find command (i.e. find -name ‘*.zip’ …….). Simple.

Take note that due to the fact that we are using the find command it will traverse through the whole directory and output the results.