Linux Tips and Tricks

Linux Tips and Tricks

Shorten long urls using curl command – shell script

September21

tinyurl is providing a api call to shorten the long url. So, we can make a api call using the curl command and get the short url from the long url.

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
 
#  This script is used to shorten the long url
#  Author  : Kamaraj Subramanian
#  Website : www.thelinuxtips.com
 
echo -n "Enter the Long URL : "
read url
 
short_url=$(curl -s http://tinyurl.com/api-create.php?url=${url})
 
echo "Short URL is : ${short_url}"

Output

1
2
3
4
5
6
7
8
9
10
11
$ ./shorten.sh
Enter the Long URL : http://www.google.com/finance?cid=694653
Short URL is : http://tinyurl.com/lhu73
 
$ ./shorten.sh
Enter the Long URL : https://sites.google.com/site/gdevelopercodelabs/app-engine/python-codelab
Short URL is : http://tinyurl.com/2vb7e2e
 
$ ./shorten.sh
Enter the Long URL : http://www.flickr.com/services/apps/tags/Linux
Short URL is : http://tinyurl.com/9za9z32
posted under Uncategorized | No Comments »

Retrieve stock value using shell script

September19

we can use wget command to retrieve the stock value of given stock symbol from google finance website.

Shell script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
 
#  This script is used to retrieve the stock value from finance.google.com
#  Author  : Kamaraj Subramanian
#  Website : www.thelinuxtips.com
 
echo -n "Enter the Stock Symbol : "
read stocksymbol
 
googleURL="http://finance.google.com/finance/info?client=ig\&q=NASDAQ%3a"
 
#Retrieve the stock value
wget $googleURL$stocksymbol -O /tmp/o.txt 2>/dev/null
stockvalue=$(awk '/,\"l\"/{print $NF}' /tmp/o.txt)
 
echo "Stock value of $stocksymbol is : $stockvalue"
 
<span style="text-decoration: underline;"><strong>Output</strong></span>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#GOOGLE
 
$ ./stockvalue.sh
Enter the Stock Symbol : GOOG
Stock value of GOOG is : "724.07"
 
#YAHOO
 
$ ./stockvalue.sh
Enter the Stock Symbol : YHOO
Stock value of YHOO is : "15.89"
 
#MICROSOFT
 
$ ./stockvalue.sh
Enter the Stock Symbol : MSFT
Stock value of MSFT is : "31.14"
 
#APPLE
 
$ ./stockvalue.sh
Enter the Stock Symbol : AAPL
Stock value of AAPL is : "701.23"
 
#FACEBOOK
 
$ ./stockvalue.sh
Enter the Stock Symbol : FB
Stock value of FB is : "22.86"
posted under Uncategorized | No Comments »

Sum of all digits in a given number using unix commands

September18

Using the below commands, we can sum of all the digits in a given number.

Eg:

If a given number is 123. Then the answer should be ( 1 + 2 + 3 ) = 6

1
2
3
4
5
6
7
8
9
10
$ echo "1234567" | grep -o . | awk '{a+=$0}END{print a}'
28
$ echo "1234567" | awk '{for (i=1;i <=length($0);i++)a+=substr($0,i,1);print a}'
28
$ echo "1234567" | fold -w1 | awk '{a+=$0}END{print a}'
28
$ echo "1234567" | sed "s/./&\n/g" | awk '{a+=$0}END{print a}'
28
$ echo "1234567" | awk '{for(i=1;i <=NF;i++){a+=$i;}print a}' FS=
28

 

posted under Uncategorized | No Comments »

Print/Read the $PATH variable in readable format

September17

The $PATH variable is specified as a list of one or more directory names separated by colon (:) characters

If you want to see the variable value, then just echo it.

my PATH variable contains the below directory names.

1
2
3
4
5
6
$ echo $PATH
/opt/sybase/ASE-15_0/jobscheduler/bin:/opt/sybase/ASE-15_0/bin:/opt/sybase/ASE-15_0/
install:/opt/sybase/ASEP/bin:/opt/sybase/DBISQL/bin:/opt/sybase/UAF-2_5/bin:/opt/sybase/
OCS-15_0/bin:/opt/sybase/ASE-15_0/jobscheduler/bin:/opt/sybase/ASE-15_0/bin:/opt/sybase/
ASE-15_0/install:/opt/sybase/ASEP/bin:/opt/sybase/DBISQL/bin:/opt/sybase/UAF-2_5/bin:
/opt/sybase/OCS-15_0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

well, the above output is not much readable format. Lets make/show it as a readable one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ echo $PATH | awk -v RS=: '1'
/opt/sybase/ASE-15_0/jobscheduler/bin
/opt/sybase/ASE-15_0/bin
/opt/sybase/ASE-15_0/install
/opt/sybase/ASEP/bin
/opt/sybase/DBISQL/bin
/opt/sybase/UAF-2_5/bin
/opt/sybase/OCS-15_0/bin
/opt/sybase/ASE-15_0/jobscheduler/bin
/opt/sybase/ASE-15_0/bin
/opt/sybase/ASE-15_0/install
/opt/sybase/ASEP/bin
/opt/sybase/DBISQL/bin
/opt/sybase/UAF-2_5/bin
/opt/sybase/OCS-15_0/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games

using some more commands, we can make it as readable.

Below is the some of the commands combination.

1
2
3
4
5
6
7
8
$ echo $PATH | tr ":" "\n"
$ echo $PATH | sed "s/:/\n/g"
$ echo $PATH | perl -pe 's/:/\n/g'
$ echo $PATH | awk '{gsub(":","\n")}1'
$ IFS=:; for i in $PATH; do echo $i; done
$ echo $PATH | perl -lane 's/:/\n/g;print $_'
In KSH
print - ${PATH//:/\\n}

In unix, we always have more ways to achieve the same goal. UNIX ROCKS 🙂

posted under Uncategorized | No Comments »

Print Random word using the Dictionary file – AWK

September16

Most of the Debian system has the below dictionary file ( which is the pure ASCII words )

/usr/share/dict/american-english

we have a built-in variable called RANDOM in bash and ksh shells. Using that, we can print random words from the above given dictionary file.

example of RANDOM variable

1
2
3
4
$ echo $RANDOM
30728
$ echo $RANDOM
24228

Using the below code, we can print some random word from the dictionary file.

1
2
3
4
5
6
7
8
9
10
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
Jeannie's
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
authorization
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
Yiddish's
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
Constantine's
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
Miro

In the above awk command, lineno is a variable which holds the RANDOM number, then we are comparing with processing line number of awk. If both number matches, we are printing that word.
exit is used to avoid processing/reading the remaining lines.

posted under Uncategorized | No Comments »

Find most commonly used commands in your system

September15

The below one liner is used to show the most commonly commands and its count.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ history | awk '{a[$2]++;next}END{for (i in a){print i " --> " a[i]}}' | sort -nr -k3
clear --> 25
cal --> 23
history --> 9
./caldate.sh --> 7
date --> 6
ls --> 2
cd --> 2
pwd --> 1
ps --> 1
man --> 1
env --> 1
echo|awk --> 1
echo --> 1
chmod --> 1
cat --> 1
awk --> 1
posted under Uncategorized | No Comments »

Retrieve IP Address from URL – nslookup

September12

In unix/linux we have a utility called nslookup. Which is very useful to query internet naming servers.

Man Page for nslookup

1) Lets see how to retrieve the ip address from the URL ( eg. www.google.com )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ nslookup www.google.com
Server:        218.186.2.16
Address:    218.186.2.16#53
 
Non-authoritative answer:
Name:    www.google.com
Address: 173.194.38.147
Name:    www.google.com
Address: 173.194.38.146
Name:    www.google.com
Address: 173.194.38.145
Name:    www.google.com
Address: 173.194.38.144
Name:    www.google.com
Address: 173.194.38.148

If you want only the ip address, then you can awk the output of the nslookup output.

1
2
3
4
5
6
7
8
9
$ nslookup www.google.com | awk '/Address/&&!/#/'
Address: 173.194.38.176
Address: 173.194.38.179
Address: 173.194.38.180
Address: 173.194.38.178
Address: 173.194.38.177
 
$ nslookup www.facebook.com | awk '/Address/&&!/#/'
Address: 66.220.149.94

If you type the above ip addresses, then you will be directed to corresponding website.

posted under Uncategorized | No Comments »

Recent Comments

    Categories