Saturday 5 March 2016

How to debug shell / bash scripts

While executing the bash scripts, if you want to debug them i.e you want to trace the exact sequence of steps that get executed, continue reading this post below.

Just below the shebang line on top of every script, type this :

set -x

That's it.

Let's take the example of killing a process / application using shell script that we saw in our previous post. Assuming you have opened the application - TextWrangler and want to close it using a bash script.

#!/bin/bash
set -x


kill -9 `ps -ef | grep TextWrangler | awk '{print $2}'`;

Now here's the output for the same :

++ ps -ef
++ grep TextWrangler
++ awk '{print $2}'

+ kill -9 24677 24678 24682

Note that 24677 24678 and 24682 are all PID's of all processes associated with TextWrangler. In this way you can track the flow of the script in which the tasks get executed. Debugging can be of great help if you have a complex flow of program and need to identify the sequence of steps in which they are executed.

Friday 4 March 2016

Kill process or application using Apache Ant

Hello friends. In our last post we saw how to execute a shell script from Apache Ant. In today's post lets see if we could use Ant to kill some running processes.

For example lets open the TextWrangler app. Now lets try to close it using Ant build.

Consider a shell script kill-wrangler.sh as below :

kill -9 `ps -ef | grep TextWrangler | awk '{print $2}'`;

Now we'll execute the above bash script and kill the corresponding PID (process ID) for the TextWrangler app using the Ant build. Consider the following build.xml

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="kill-process" basedir=".">

<target name="kill-process">
<exec executable="/bin/bash">
<arg value="kill-wrangler.sh"/>
</exec>
</target>


</project>

In this we execute the above bash script kill-wrangler.sh and close the application. You could grep (search) the running processes for any other program/string. For instance you could kill some Java process or maybe stop some application. Okay guys, signing out for now. Will post some more cool stuff in the forthcoming days. Stay tuned...

Wednesday 2 March 2016

How to open Terminal from within Eclipse on Mac OS X

In Eclipse, from the Help menu -> Eclipse Marketplace, lookup for Terminal. Install the plugin TM Terminal that you'll come across as below:


Once installed, simply mention the shell interpreter location in the Preferences for e.g. /bin/bash and also choose the initial working directory as either your User Home or Eclipse Workspace.


Now go to Window -> Show View and choose Terminal to show it within Eclipse and click the following blue button to open a new session.


This way you can run shell scripts from within Eclipse itself. Cheers!

Tuesday 1 March 2016

Call shell script from Apache Ant build on Mac OS X

Guys, you might come across some situations where you need to invoke shell and execute a bash script while building a project using Apache Ant. In today's example let's see how to run a shell script from within an Ant build.

For illustration, lets create a simple bash script to list the disk usage of our system. Let's name this file as "DiskUsage.sh"

#!/bin/bash

# Sample shell script to be invoked from an Ant build

echo "Disk usage is as follows : \n"
echo "=========================== \n"


df -h

Now let's create a simple build-test.xml using the exec command to invoke the shell as follows :

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="call-shell" basedir=".">

<target name="call-shell">
<exec executable="/bin/bash">
<arg value="DiskUsage.sh"/>
</exec>
</target>


</project>

Run the ant build from command line as ant -f build-test.xml. Note that the shell script and build.xml happen to be at the same location/hierarchy within the project. If your shell script lies elsewhere, you may perhaps want to refer to its path in the build.properties instead.

You could also invoke the "expect" scripting prompt in a similar fashion to execute an expect script that we discussed in our earlier post.

More information on the exec can be read at the official Ant manual.

Sunday 28 February 2016

Ant - Build.xml Updates - 28 Feb

Guys, in my earlier posts we learnt how to write a simple basic build.xml and also learnt about the build.properties. Today we'll modify, update our build.xml to include some more functionality.

Let's add the following 2 features :
  • add a MANIFEST.MF file to the jar package which will contain the packaging information
  • create documentation using javadoc.
Check out the updated build.xml with the tasks for the same below :

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="generate-docs" basedir=".">

<property name="build.home" value="${basedir}" />
<property file="build.properties"/>

<tstamp>
<format property="timestamp" pattern="dd-MM-yyyy"/>
</tstamp>

<target name="clean">
<delete dir="${build.home}/src" />
<delete dir="${build.home}/bin" />
<delete dir="${build.home}/lib" />
<delete dir="${build.home}/test" />
<delete dir="${build.home}/dist" />
<delete dir="${build.home}/docs" />
<delete dir="${build.home}/logs" />
</target>

<target name="createdir" depends="clean">
<mkdir dir="${build.home}/src" />
<mkdir dir="${build.home}/bin" />
<mkdir dir="${build.home}/lib" />
<mkdir dir="${build.home}/test" />
<mkdir dir="${build.home}/dist" />
<mkdir dir="${build.home}/docs" />
<mkdir dir="${build.home}/logs" />
</target>

<path id="classpath">
<pathelement location="${build.home}/bin"></pathelement>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

<target name="copydir" depends="createdir">
<copydir src="${workspace.src}"
dest="${build.home}/src"></copydir>
</target>

<target name="compile" depends="copydir, clean">
<javac classpathref="classpath" includeantruntime="false"
srcdir="${build.home}/src"
destdir="${build.home}/bin"
includes="**/*.java"></javac>
</target>

<target name="create-jar" depends="compile">
<jar basedir="${build.home}/bin" destfile="${build.home}/dist/${timestamp}-TestJar-${versionnum}.jar"
includes="**/*.class">
<manifest>
<attribute name="Main-class" value="com.ironcladzone.FileSize"/>
</manifest>
</jar>
</target>
<target name="generate-docs" depends="create-jar">
<javadoc sourcepath="${build.home}/${src.dir}" destdir="docs"/>
</target>

</project>

Notice the "create-jar" target where we have added the <manifest></manifest> tags in which we have mentioned the main class for instance. This will create a MANIFEST.MF file within the jar. I'll write a new post on the Manifest file soon.

Also for the javadoc, we have a new target named "create-documentation". If you check the folder structure, you'll find the index.html within the docs folder. Try looking into the index.html, you'll see the class and package details in a sample webpage decorated with the default javadoc css stylesheet.

Recover and restore deleted files from Eclipse Navigator

Have you come across a situation where you accidentally deleted source code from the Eclipse Navigator. Just don't panic in such a case. There's a way to recover and restore these deleted files.

Simply right click the project from the Navigator pane and choose the following option :


In the next screen, you'll see the list of all files that you deleted and wish to restore back. Just choose the necessary files, click Restore and relax!


View Jar contents without opening on Mac OS X

Hello readers, today's topic is a fairly basic interview question wherein you are asked to view the jar package contents without exploding/opening it.

In order to do so, type the following in terminal for example :

unzip -l 28-02-2016-TestJar-1.8.jar 

Output : 

Archive:  28-02-2016-TestJar-1.8.jar
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  02-28-16 13:34   META-INF/
      142  02-28-16 13:33   META-INF/MANIFEST.MF
        0  02-28-16 13:33   com/
        0  02-28-16 13:33   com/ironcladzone/
      571  02-28-16 13:33   com/ironcladzone/Create_Directory.class
      755  02-28-16 13:33   com/ironcladzone/FileSize.class
      367  02-28-16 13:33   com/ironcladzone/HelloICZ.class
      463  02-28-16 13:33   com/ironcladzone/String_mani1.class
      534  02-28-16 13:33   com/ironcladzone/String_manip2.class
      601  02-28-16 13:33   com/ironcladzone/String_manip3.class
      407  02-28-16 13:33   com/ironcladzone/String_manip4.class
      474  02-28-16 13:33   com/ironcladzone/String_manip5.class
      935  02-28-16 13:33   com/ironcladzone/User_Input.class
      201  02-28-16 13:33   com/ironcladzone/callMacApp.class
      909  02-28-16 13:33   com/ironcladzone/operator1.class
 --------                   -------
     6359                   15 files

Note the -l switch is for listing the jar contents. You can use the same technique for any kind of package - jar / war / tar / ear etc.
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker