Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

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...

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

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.

Saturday 27 February 2016

Automate tasks using Expect scripts on Mac OS X

Expect is one of the hidden gems and quite esoteric scripting language for automating tasks on Mac OS X and on most unix flavors.

The idea behind expect is you can program the response to a programmed request string. It's much like a pre-programmed auto-complete feature, in which you have already told the system what the response should be, if it comes across a specific expected request.

Let's try out a basic simple script. In terminal create a new script file : vi expect_test.exp
Note the .exp extension, similar to .sh of bash scripts.

#!/usr/bin/expect

expect "hello"

send "Welcome to IroncladZone \n"

Now if you execute the script ./expect_test.exp and type the string "hello", it will automatically send the response "Welcome to IroncladZone".

Tuesday 23 February 2016

Change text Case using shell script

In today's post we cover a simple problem - change the case of text i.e either from lowercase to uppercase or vice versa. Let's look at a few examples.

Let's say I have a file SedText.rtf as follows :

1.Here is a cute little tutorial for
2.trying out different things using
3.the Stream Editor or Sed in Unix.
4.The author of this tutorial is
5.none other than IroncladWriter
6.himself. The tutorial aims to
7.teach folks and educate how
8.they could unlock the power
9.of Unix to the fullest. Am posting
10.this on the blog IronCladZone
11.The best blog on the whole of
12.internet, where you can learn
13.technical stuff as well enjoy 
14.entertaining tidbits like movies,
15.music, television, fashion, food,
16.shopping, travel, trends etc.
17.Just sit back, relax and

18.ENJOY THE RIDE :)

Now for converting the file into all Uppercase from the command line, use the following :

tr a-z A-Z < SedText.rtf 

Output : 

1.HERE IS A CUTE LITTLE TUTORIAL FOR\
2.TRYING OUT DIFFERENT THINGS USING\
3.THE STREAM EDITOR OR SED IN UNIX.\
4.THE AUTHOR OF THIS TUTORIAL IS\
5.NONE OTHER THAN IRONCLADWRITER\
6.HIMSELF. THE TUTORIAL AIMS TO\
7.TEACH FOLKS AND EDUCATE HOW\
8.THEY COULD UNLOCK THE POWER\
9.OF UNIX TO THE FULLEST. AM POSTING\
10.THIS ON THE BLOG IRONCLADZONE\
11.THE BEST BLOG ON THE WHOLE OF\
12.INTERNET, WHERE YOU CAN LEARN\
13.TECHNICAL STUFF AS WELL ENJOY \
14.ENTERTAINING TIDBITS LIKE MOVIES,\
15.MUSIC, TELEVISION, FASHION, FOOD,\
16.SHOPPING, TRAVEL, TRENDS ETC.\
17.JUST SIT BACK, RELAX AND\
18.ENJOY THE RIDE :)


Similarly for converting from uppercase to lowercase,  use the following command :

tr A-Z a-z < SedText.rtf 

Tuesday 16 February 2016

Add a character at a specific location within strings using Awk

Folks, in previous examples we saw how to add a prefix to strings in a file and how to add a suffix to strings in a file. In today's example, we'll see how to add a character somewhere in the middle of multiple strings in a file at the same time.

We'll use the same data as used in previous examples. Let's use a text file named Phones_unformatted.txt with the following phone numbers data :

2127841212
3458922345
7713398403
6461228847

Now we want to add a - sign after the first 3 digits to separate the area code. Notice that all phone numbers are of 10 digits. We want to add a - sign between the 3rd and 4th character.

For doing this lets use awk again :

awk '$0=substr($0,1,3) "-" substr($0,4,7)' Phones_unformatted.txt

Output :

212-7841212
345-8922345
771-3398403
646-1228847


Notice how we have used substr() function to split the entire 10 digit phone number into 2 parts. The first part is between 1-3rd character and the second part is from 4-10th character.

Similarly, lets say for e.g. we want to add the - sign twice within the string i.e once between 3rd and 4th character and the next - between 6th and 7th character. The awk code for doing this is as follows :

awk '$0=substr($0,1,3) "-" substr($0,4,3) "-" substr($0,7,4)' Phones_unformatted.txt

Output :

212-784-1212
345-892-2345
771-339-8403
646-122-8847

This way, you could append the string with the "-" character or anything like a dot "." or maybe just a whitespace " ".

awk '$0=substr($0,1,3) " " substr($0,4,3) " " substr($0,7,4)' Phones_unformatted.txt

Output :

212 784 1212
345 892 2345
771 339 8403
646 122 8847

Sunday 14 February 2016

Add suffix to strings in a file using Awk

Guys, in my previous post yesterday, we discussed how to add a prefix to strings in a file using Awk. Today lets work on the same example to add a suffix to the strings i.e append the strings with characters in the end.

We'll use the same set of phone numbers for formatting :

2127841212
3458922345
7713398403
6461228847

Let's add some characters to the these numbers in the end. How about something like " Extn : 7142". Here's the Awk code to do it below :

awk '{print $0 " Extn : 7142"}' Phones_unformatted.txt

Output :

2127841212 Extn : 7142
3458922345 Extn : 7142
7713398403 Extn : 7142

6461228847 Extn : 7142

Saturday 13 February 2016

Add a prefix to strings in a file using Awk

Suppose you have a file in Unix / Mac OS X which has phone numbers data. I mean lets say I have a text file named Phones_unformatted.txt which has some phone numbers as follows :

2127841212
3458922345
7713398403
6461228847

Now I want to format the phone numbers to have a prefix maybe like a country code +44- . something like :

+44-2127841212
+44-3458922345
+44-7713398403
+44-6461228847

If you observe I need to only append the phone number with some characters in the beginning. We can achieve the same using awk.

A simple awk code will do the trick as follows :

awk '{print "+44-" $0}' Phones_unformatted.txt

Output :

+44-2127841212
+44-3458922345
+44-7713398403
+44-6461228847

Viola!

Monday 8 February 2016

Transfer files between two servers using SCP

We can transfer files between 2 remote hosts using the scp command as follows :

scp Username@Server1:/path-to-file-to-be-copied/ Username@Server2:/destination-path-where-files-are-to-be-copied/

Note that you may have to enter the credentials manually after this to logon to both the hosts.

Eg :

scp user123@Unix_Host_1:/Users/user123/Documents/Textfile.txt user123@Unix_Host_2:/users/user123/Documents/

You may also use the -3 switch to transfer files from one machine to another through your local host.

Check out the following available switches on a Mac OS X machine :

usage: scp [-12346BCEpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]

           [[user@]host1:]file1 ... [[user@]host2:]file2



Sunday 7 February 2016

Compare files contents in Mac OS X from command line using Comm

Comm is the command in Mac OS X if you want to compare the contents of two files through the command line.

The usage is as follows :

comm -switch file1 file2

where -switch can be any of the following :

-1 : This will not display the lines which are UNIQUELY present only in File1
-2 : This will not display the lines which are UNIQUELY present only in File2
-3 : This will not display the lines which are COMMON in both the files. i.e it will only display the unique lines from File1 and the File2

-i : This will basically display 3 columns. The 1st column will display lines which are UNIQUELY present only in File1. The 2nd column will display lines which are UNIQUELY present only in File2. And the 3rd column will display lines which are COMMON in both the files.

For eg:

we have taken two Text files for comparison, the contents of which are as follows :

TextFile1.txt

ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZ

TextFile2.txt

ABCD
DEFG
GHI
JKL
MNO
PQR
STU
VWXYZ
YZ

------ Usage 1 ------

comm -1 TextFile1.txt TextFile2.txt
ABCD
DEFG
GHI
JKL
MNO
PQR
STU
VWXYZ

YZ

------ Usage 2 ------

comm -2 TextFile1.txt TextFile2.txt
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX

YZ

------ Usage 3 ------

comm -3 TextFile1.txt TextFile2.txt
ABC
ABCD
DEF
DEFG
VWX

VWXYZ

------ Usage 4 ------

comm -i TextFile1.txt TextFile2.txt 
ABC
ABCD
DEF
DEFG
GHI
JKL
MNO
PQR
STU
VWX

VWXYZ


Wednesday 3 February 2016

Basic print examples in Unix using Sed

I was long thinking to write a short tutorial on covering some basic Sed examples for easy and quick reference. So here we are. In this short post I'll try to include some basic print examples with some variety... the more the merrier right?

The sample Testfile.txt we'll be using in this example is as below :

1.Here is a cute little tutorial for
2.trying out printing things using
3.the Stream Editor or Sed in Unix.
4.The author of this tutorial is
5.none other than IroncladWriter
6.himself. The tutorial aims to
7.teach folks and educate how
8.they could unlock the power
9.of Unix to the fullest. Am posting
10.this on the blog IronCladZone
11.The best blog on the whole of
12.internet, where you can learn
13.technical stuff as well enjoy 
14.entertaining tidbits like movies,
15.music, television, fashion, food,
16.shopping, travel, trends etc.
17.Just sit back, relax and

18.ENJOY THE RIDE :)

* Print only the first line of a file

sed -n '1p' Testfile.txt

OR you could also use the following:

sed q Testfile.txt

* Print only the last line of a file

sed -n '$p' Testfile.txt

* Print only the lines between 8th and 11th line of a file

sed -n '8,11p' Testfile.txt

* Print only those lines having the word "Unix" from the file

sed -n '/Unix/'p Testfile.txt

* Print only those lines after the word/pattern "shopping" to the end of the file. Note this will include the line containing the word "shopping" as well.

sed -n '/shopping/,$p' Testfile.txt

In my next post, I'll try to cover some advanced printing examples using Sed. Stay tuned fellas.

Tuesday 4 March 2014

Using MD5 checksum utility on Mac OS X

If you might be aware, every Linux distribution comes with the MD5 checksum utility - md5sum. Similarly UNIX flavors like Mac OS X, come pre-bundled with utility called md5

The OS X md5 utility can be found at /sbin/md5

I'll quickly show some steps on how to use it :

  • Open Terminal
  • Create a blank empty file anywhere on the system (I mean just anywhere) using touch


Eg : touch test.xml
  • Now type the following to generate the md5 checksum of this file 
Eg : md5 test.xml > test.xml.md5

That's it. Now open the test.xml.md5 using TextEdit or Text Wrangler and you'll see the file's equivalent md5 checksum. 

If you want to change the format of the output similar to Linux's md5sum use the -r switch 

Eg : md5 -r test.xml > test.xml.md5

Note that MD5 checksums are used to verify the integrity of a file. Even a single new character added to the file will change the equivalent md5 output. Check for yourself. Modify the test.xml by adding some content to it. Now run the md5 utility again as shown above. The md5 equivalent of the file will now be different. The checksums are important to verify that the files / resources are intact throughout and ensure that they are not corrupt or tampered.

You can use this method to verify the integrity of zip or tar files that you download from the internet.

A free utility Checksum Validator is also available. At the heart of it's gui, it uses this md5 command.

This information can come particularly handy while using Maven as well. Suppose for some odd reason, you have to manually create md5 checksums of the dependency jar's - then this method tells you how to do it.

Tuesday 18 February 2014

Repeat last run command in Linux, Unix, OSX

In order to repeat the last run command on Linux, Unix, Apple OS X machines, type the following in terminal :

!!

Yes. That's precisely 2 exclamation marks.

Tuesday 28 January 2014

Vi editor - Keyboard shortcuts

If you're new to working on a Unix shell, you must know that you can edit files through the command line itself, using the vi mode. Let me quickly show you some basic most frequently used commands (keyboard shortcuts) about vi editing.

1. To create a new file or open an existing file to edit, type vi filename

2. To close an opened file :

type :wq (to save your changes and quit) and press Enter

type :wq! (to quit without saving, use !) and press Enter

type just :w (to save a file without quitting)

3. To add some text to an opened file press i (This will allow you to insert text before the cursor)

Press a to append text after the cursor.

4. To Replace some text exactly under the cursor, type r

5. To delete a character under the cursor, type

6. To delete the entire current line type dd

7. To copy the current line, type yy

8. To paste the copied line, type p

9. To search for a string, word or a pattern in a file, type :/string-to-be-searched (Note the forward slash /) This will search the string in a forward fashion (from top-to-bottom). However, if you want to start searching backwards (from bottom-to-top) type :?string-to-be-searched

10. When you search for a string using forward slash / or question mark ? you could move to the string's next occurrence by typing n

11. To display the total number of lines in the file, type :=

12. To display the current line number, type :.= (Note the dot .)

Tuesday 21 January 2014

Check if a user has root access in Mac OSX

On Mac OSX or any other unix flavor, you can find if a user has root access i.e superuser privileges or not. Open terminal and type the following

Eg:

sudo -l -U ironcladWriter

where the -U switch stands for user name.

If you have root access, you will see the following in the output :

User ironcladWriter may run the following commands on this host:
    (ALL) ALL

If by any chance, you don't know your username, type the following in Terminal :

whoami

Tuesday 7 January 2014

Count lines, words & characters of a file in Unix

On Unix and Linux machines, if you want to find out the word count, number of lines and the total number of characters used in a certain file, use the wc command in the following way.

Eg:

cat build.xml | wc

The output will be like 

9      26     297

Here, 9 is the number of lines, 26 is the number of words and 297 is the number of characters.

Monday 30 December 2013

Kill a Process in Unix

In UNIX and OSX, you can kill a process from the command line using the "kill" command.

kill -9 PID

The PID (Process ID) can be found by using ps -ef command.

Eg:

ps -ef | grep jrockit

The output will show the Process ID. Let's say , for instance it's 4519.

Then you can kill the process by using :

kill -9 4519

-9 is basically a signal (SIGKILL) to be used with the kill command. The default signal is -15 i.e SIGTERM which is milder signal to terminate a process.

More detailed information about kill signals can be found here.

Technorati cliam token USM62NJ7A438

Saturday 28 December 2013

Check if a directory exists in Unix

In Unix, you can check if a certain directory exists or not using -d

Eg:

if [-d $directory_name]
then
       execute_some_commands
fi

In the variable $directory_name you specify the name of the directory. If the directory exists, then the if loop executes and the commands would be executed. 

Sunday 22 December 2013

Bourne shell (bash) vs Korn shell (ksh)

There are a few notable differences between Bash shell and Korn shell as follows:





DifferenceBashKorn
Redo previous commandArrow Up the letter 'r'
Source fileWe can use the keyword 'source' as well as dot to source a file Eg: source file or .fileWe can just use the dot to source it Eg: .file

Friday 20 December 2013

Create file without editing in Unix

The best way to create a new file without editing or opening it is using touch command.

Eg: 

touch test1.txt  => This will create a new file test1.txt

Touch can also be used to update an existing file's timestamp to current time. The Date modified timestamp will indicate current time

Note that if you want to touch a file only and only if it exists, use the -c switch

Eg:

touch -c log.txt

(If the log.txt exists previously, only then the access and modification timestamps would update. If log.txt does not exists, the above command will do nothing i.e a new file will not be created)
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker