Showing posts with label Mac. Show all posts
Showing posts with label Mac. Show all posts

Friday 6 May 2016

Identify exact port number used by a process or app on Mac OSX

Guys, if you ever wanted to find out the exact specific port number used by a process/application on Unix/Mac OSX/Linux, type the following in Terminal :

top | awk '{print $2, $7}'

The output would be something like :

COMMAND #PORTS
awk 12
top 22+

top 19

This will show only the specific Command and Port columns from the top command and will skip other details like Process ID, % CPU utilisation , Memory usage etc.

Alternatively if you wanted to just find the port number used by a specific application, say for e.g. TextEdit, you could use the following command :

top | awk '{print $2, $7}' | grep TextEdit
TextEdit 140+
TextEdit 140

TextEdit 140

Cheers guys!

Saturday 30 April 2016

Start Stop Jenkins from Terminal on Mac OS X

Ola guys. I hope you came across my previous posts on setting up Ant and Maven with Jenkins. In today's post I'll show you how to start and stop Jenkins from Terminal on Mac OS X machine.

I assume you know how to shutdown Jenkins from console OR restart it from the URL field.

Alright so here we go.

For staring Jenkins from command line, type this in Terminal :

sudo launchctl load /Library/LaunchDaemons/org.jenkins-ci.plist

Give it a few seconds for Jenkins to startup and you can then see the console in the browser. If you still stay executing the startup command again you'll get the following message :

/Library/LaunchDaemons/org.jenkins-ci.plist: service already loaded

In the meantime you can double check if Jenkins has started by checking the process status in Terminal :

ps -ef | grep jenkins

For stopping Jenkins from the command line, type this in Terminal :

sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist

A rougher way to stop Jenkins is by killing the process ID of Jenkins that you found above with the ps -ef command.


Wednesday 27 April 2016

Character Combinations using Shell Script

In today's post we'll look into a very simple entry level bash script in which we form all possible alphanumeric combinations using just 2 characters and save it in a file. You could tweak the program to suit your needs - e.g. all combinations of 6 or 7 or 8 characters or maybe include special characters as well. The file in which all these combinations are stored would be more like a dictionary.

#!/bin/bash
# Program to print all possible 2-character combinations

echo {a..z}{0..9} > /Users/ironcladzone/Documents/xyz.txt


echo {0..9}{a..z} >> /Users/ironcladzone/Documents/xyz.txt

Here's the output of xyz.txt :

a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 b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 3o 3p 3q 3r 3s 3t 3u 3v 3w 3x 3y 3z 4a 4b 4c 4d 4e 4f 4g 4h 4i 4j 4k 4l 4m 4n 4o 4p 4q 4r 4s 4t 4u 4v 4w 4x 4y 4z 5a 5b 5c 5d 5e 5f 5g 5h 5i 5j 5k 5l 5m 5n 5o 5p 5q 5r 5s 5t 5u 5v 5w 5x 5y 5z 6a 6b 6c 6d 6e 6f 6g 6h 6i 6j 6k 6l 6m 6n 6o 6p 6q 6r 6s 6t 6u 6v 6w 6x 6y 6z 7a 7b 7c 7d 7e 7f 7g 7h 7i 7j 7k 7l 7m 7n 7o 7p 7q 7r 7s 7t 7u 7v 7w 7x 7y 7z 8a 8b 8c 8d 8e 8f 8g 8h 8i 8j 8k 8l 8m 8n 8o 8p 8q 8r 8s 8t 8u 8v 8w 8x 8y 8z 9a 9b 9c 9d 9e 9f 9g 9h 9i 9j 9k 9l 9m 9n 9o 9p 9q 9r 9s 9t 9u 9v 9w 9x 9y 9z

Wednesday 20 April 2016

Area & Circumference of a circle - Shell script for Mac OS X

Guys in todays post let us look into a simple basic Mathematics problem. Let us automate the calculation of area and circumference of a circle, using a simple bash script. It's an easy script useful for learning some basic shell scripting.

Note : Value of pi used is a constant value rounded off to 3.142

#!/bin/bash
#program to calculate area and circumference of a circle

echo "=========================================="
echo "Area & Circumference of Circle Calculator"
echo "=========================================="

pi=3.142

echo "Please enter the radius of the circle : "
read radius

circumference=`expr "2*$pi*$radius"|bc`
echo "Circumference of circle : $circumference"
area=`expr "$pi*$radius*$radius"|bc`

echo "Area of circle : $area"

Output :

./AreaOfCircle.sh
==========================================
Area & Circumference of Circle Calculator
==========================================
Please enter the radius of the circle : 
5
Circumference of circle : 31.420

Area of circle : 78.550

Also do check out my previous post on Simple Interest Tool for additional scripting reference.

Script to check if a server / machine is up and running

Guys you might often come across some situations where you want to check if a server or machine is physically up and running. Quite obviously you would use the ping command to do so.

But in today's post let us write a simple bash script to automate this.

#!/bin/bash
#program to check if a server/machine is up and running

ipaddr=192.168.1.34

ping -c 4 $ipaddr >> /dev/null

if [[ $? -ne 0 ]]
then
echo "FAIL: Server seems to be down and not responding" 

else
echo "SUCCESS : Server is up and running"


fi

Note : $? is a special Unix variable to check if the previous command was executed successfully or not. It returns a numerical value - either 0 or 1. If the command was executed successfully, it returns 0, else if not, it returns 1.

In above script, simply replace the variable 'ipaddr' value to your server's ip address and check.

Also note that by redirecting the output of the ping command to /dev/null, we are suppressing and silencing the output i.e the ping o/p will not be shown on terminal. If you want to see the exact output of ping, remove the ">> /dev/null" part.

Output :

./PingServer.sh
SUCCESS : Server is up and running

Cheers!

Simple Interest Calculation : Shell Script for Mac OS X

Guys, today we'll look into a very basic simple shell script to calculate the Simple Interest. For this we need 3 essential details :

Principal Amount
Rate of Interest
Period i.e No. of years

Here's the bash script to calculate the Simple Interest.

#!/bin/bash
#program to calculate simple interest

echo "================================"
echo "Simple Interest Calculation Tool"
echo "================================"
echo "Enter principal amount : "
read principal
echo "Enter Rate of Interest : "
read roi;
echo "Enter No. of years : "
read years;

simple_interest=`expr "($principal*$years*$roi)/100"|bc`
echo "Simple Interest = $simple_interest"

Output :

./SimpleInterest.sh
================================
Simple Interest Calculation Tool
================================
Enter principal amount : 
5000
Enter Rate of Interest : 
9.5
Enter No. of years : 
5

Simple Interest = 2375

If you note, we have used the bc command to evaluate our expression i.e the Simple Interest formula. More on bc can be read here.

How to identify available shells on Mac OS X

Guys you may be aware that you can find your current shell in Mac OSX or any Unix machine by typing the following in Terminal.

echo $SHELL
/bin/bash

But to identify the available shells on Mac OS X type the following :

cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

Also, if you ant to change your default shell, type this :

chsh
Changing shell for ironcladzone.

Password for ironcladzone: 

Hope it helps. Cheers!

Saturday 2 April 2016

Mac OS X - Hidden Gems : Part 3

Guys in today's new gem, we'll see how to play the hidden game Tetris on Mac OS X which comes bundled by default for FREE. Didn't you even know that? I bet you didn't!

Allright! So to play Tetris on Terminal, do this :

  • Open Terminal
  • Type emacs
  • Now hit the Esc button and immediately press x

  • When you see the M-x prompt, just type tetris. Now simply relax and play a cool game of Tetris. How cool is that?



Friday 18 March 2016

Mac OS X - Hidden Gems : Part 2

Oh boy! yet another Mac OS X hidden gem that you probably weren't aware of.

Type this is Terminal to read Mrs Fields secret Cookies recipe.

open /usr/share/emacs/22.1/etc/COOKIES

Mac OS X - Hidden Gems : Part 1

Guys, presenting a mind blowing Mac OS X hidden gem that I just stumbled onto.

Type the following in Terminal to read some Apple supplied Jokes :)

open /usr/share/emacs/22.1/etc/JOKES

Sunday 6 March 2016

Get Mac OS X System Information from Terminal

Hii! Guys lets come back today on another topic related to Apple Mac OS X. Correct me if am wrong, the first thing you would after purchasing your new Macbook or iMac is double-check the system information right?

Typically you would click the Apple logo on top left -> About this Mac -> System Report. Well could you get the same information from command line? Yes you can!

Open terminal and just type the following :

system_profiler

It will list the system information in a matter of few seconds. You could also filter the command to get only specific information by piping to a grep command.

For eg :

system_profiler | grep Memory

system_profiler | grep Processor

system_profiler | grep CPU

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

Saturday 20 February 2016

Ant - Build.xml example on Mac OS X

In today's post guys, we'll cover the topic of building a very simple Java based project using Apache Ant. We'll learn how to write a basic build.xml file for building it.  Let's first look into the code for build.xml and then we'll breakdown the code with the explanation.

build.xml :

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

<project name="AntBuildTest" default="create-jar" basedir=".">

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

<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" />
</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" />
</target>

<path id="classpath">
<pathelement location="${build.home}/bin"></pathelement>
</path>

<target name="copydir" depends="createdir">
<copydir src="/Users/ironcladzone/Documents/workspace/AntBuildTest/src"
dest="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src"> </copydir>
</target>

<target name="compile" depends="copydir">
<javac classpathref="classpath" includeantruntime="false"
srcdir="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src"
destdir="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/bin"
includes="**/*.java"></javac>
</target>

<target name="create-jar" depends="compile">
<jar basedir="${build.home}/bin" destfile="${build.home}/dist/TestJar.jar"
includes="**/*.class"></jar>
</target>


</project>

Now let's look into the folder structure in the Eclipse Package Explorer :


Now, let's get down to the explanation guys :

1. First of all, AntBuildTest is my project workspace which has the source code in it.

2. Next I created a build directory under which I created the above build.xml file.

3. Now in the builds.xml I have defined various steps/tasks also known as targets viz : clean, createdir, copydir, compile, create-jar. Note that a build.xml will still successfully build even if it doesn't have a single target defined.

4. For every build.xml file, we first have to define the <project></project> tag, in which we mention the project name, basedir and default target to be executed.

5. Basedir is the location of the current root directory in which build.xml exists i.e the absolute path

6. The default target that we define, will be the task/target which will be executed. Now the target can have dependencies on other targets.

7. Note that target can have one or many dependencies on multiple targets.

8. The 'clean' target is the target to delete all sub-directories that we create in the 'createdir' target. So every time we perform a clean build, the directories created in the last build will be deleted.

9. In the 'createdir' target, we make directories namely : src, bin, lib, test, dist.

10. We now want to copy the source code from our local workspace to the 'src 'directory that we create in the 'createdir' target,. So in the 'copydir' target we define the step to copy the source code.

11. Now once we have the source code, we need to compile it right. So we define the 'compile' target wherein we define 'srcdir' i.e the location where we have our java files. Also we mention the 'destdir' as the bin folder i.e this is where we want our .class files to be saved.

12. During compilation, we need to specifically tell the xml where the .class files would be. So we need to define the classpath. 

13. We define the classpath i.e the bin directory which will have the .class files.

<pathelement location="${build.home}/bin"></pathelement>

14. After compilation, we define the 'create-jar' target, in which we create the jar package - TestJar.jar. Make sure not to miss the .jar file extension. Note that we have mentioned the include parameter for including the **/*.class files that need to be included in it.

15. We have also defined the 'destfile' parameter i.e the location where we want the jar file to be saved i.e the 'dist' directory for distribution.

16. Oh and I missed the 'lib' directory, which will basically hold the external jar files required for our project.

17. Well, I hope I covered the simple basic fundamental points surrounding an Ant build.  Let me know if I missed any point guys. If I did, I'll try to cover them in detail in an another upcoming post covering an advanced scenario. Till then, ciao!

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!

Tuesday 9 February 2016

How to setup Apache Ant on Mac OS X

Folks, presenting a quick tutorial on how to setup the build tool Apache Ant on a Mac OS X machine.

Download the relevant ant archive version from Apache Ant homepage. For this example we download the latest v1.9.6.
Store it locally at any location you like; maybe like /Users/Username/apache-ant-1.9.6

Now lets edit the .bash_profile to define the ANT_HOME environment variable.

sudo vim ~/.bash_profile

Add the following lines to it (make sure to replace with your own username in the path):

export ANT_HOME=/Users/IroncladZone/apache-ant-1.9.6/
export PATH=$PATH:$ANT_HOME/bin

Now source ~/.bash_profile and restart terminal.

Check the above settings by looking up the ant version in terminal :

ant -v
Apache Ant(TM) version 1.9.6 compiled on June 29 2015
Trying the default build file: build.xml
Buildfile: build.xml does not exist!
Build failed

This means your ant is ready to use for builds. Going forward all you have to do is pass the build.xml to trigger the build.

Folks, watch out for this space for more basic to intermediate to advanced tutorials coming up soon.

Monday 8 February 2016

How to Set JAVA_HOME environment variable in Mac OS X

Check out a quick reference guide to set the JAVA_HOME environment variable in Mac OS X.

Open terminal and type the following :

  • sudo vim ~/.bash_profile
  • Enter your password
  • Now in the vim editor, insert the following text :
export JAVA_HOME=(/usr/libexec/java_home)
  • Save the .bash_profile from the vim editor using :wq
  • Now type the following in terminal :
source ~/.bash_profile

Confirm your changes by typing the following in terminal :

echo $JAVA_HOME

You should see the value/path of JAVA_HOME environment variable you just set as above.



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.

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker