Tag Archives: linux

10 Mar

10 useful shell script code snippets | Linux

This post is collection of 10 useful unix / linux shell script code snippets I’ve often used.

Following these code snippet you will be able to answer following questions:
1. How to calculate script run-time in shell.
2. How to parse parameters / arguments to shell script.
3. How to safely change a directory in shell script.
4. How to check if a command was successful in shell script.
5. How to add timestamps in script log output.
6. How to check if a process is running.
7. How to colour your script output.
8. How to read variables from config file.
9. How to loop over files in a folder.
10. How to perform actions based on variable value using SWITCH…CASE

And a bonus snippet
11: How to send emails from shell script.

Let’s begin.
Continue Reading…

12 Nov

How to Get Stock Quote on Linux using Google ,Curl, Grep, Awk

If you are a Stock Market Addict and a Techie; normally working on Linux(es); this post will interest you as you still can get quote of your stock on your Linux prompt.

Consider, we have Google Finance url for Tata Consultancy Services Stock: http://www.google.com/finance?q=NSE:TCS

So, how do you get above stock price while you’re on Linux / Unix server?
It’s a sinlge line command and you get your stock’s current price. 🙂

curl --silent -X Get "http://www.google.com/finance?q=NSE:TCS" > /tmp/ChangeIsInevitable && cat /tmp/ChangeIsInevitable | grep -m1 -E 'span id="ref_' | awk -F ">" '{print $2}' | awk -F "<" '{print $1}'

This work for other Stock Exchanges like NYSE as well:

curl --silent -X Get "http://www.google.com/finance?q=nyse:goog" > /tmp/bgz && cat /tmp/bgz | grep -m1 -E 'span id="ref_' | awk -F ">" '{print $2}' | awk -F "<" '{print $1}' && date
Fri Nov 12 17:07:52 IST 2010

and that’s it. Well, I’m thinking thinking more about further extensions of this.
Special thanks goes to: Jignesh Bhai, Google, Curl, Grep, Awk.

07 Jan

crontab not working with dynamic date filename – rhel linux

I have done it for so many times, setting a cronjob.
Today I was tring to set a simple cronjob which will output the log file with timestamp attached filename.

Hourly running cron entry was:

0 * * * * perl demo.pl > demo-out_`date "+%Y-%m-%d_%H-%M"`.log

Running the same command on shell prompt was working fine. Execute permissions and path were proper. But it kept on failing to create the proper log!!

I changed the crontab as follows, added \ before % to escape and voila!!

Things worked !!

0 * * * * perl test.pl > test-out_`date "+\%Y-\%m-\%d_\%H-\%M"`.log

A quick lesson.

Good to note some of the date changers in linux for finding yesterdays and tomorrows and so on:
date –date=”1 days ago”
date –date=”yesterday”
date –date=”next day”
date –date=”-1 day”
date –date=’tomorrow’
date –date=’1 day’
date –date=’10 day’
date –date=’10 week’
date –date=’10 month’
date –date=’10 year’
date –date=”next Friday”
To escape problems you should learn to escape “special characters” – Krex
P.S.:This works in real life as well 😉

18 Dec

MySQL Memory Usage Limits on 32 bit Linux

I’m having RHEL 5 (32-bit) and MySQL-5.

I was trying to check how much is the Memory Utilization limits for MySQL 5 on 32bit OS.

We can easily calculate the maximum capacity of the address space is 2^32 bytes and that is where these limits come from.
* For MYSQL 5.0: Following are memory (storage) limits:

Max DB size:  Unlimited
Max table size:  MyISAM: 256TB; Innodb: 64TB
Max row size: 64 KB
Max columns per row: 4096
Max Blob/Clob size: 4 GB (longtext, longblob)
Max CHAR size: 64 KB (text)
Max NUMBER size: 64 bits
Min DATE value: 1000
Max DATE value: 9999
Max column name size: 64

* OS Memory Limitations:

Maximum x86 per-process virtual address space for Red-Hat Linux Version
Version 4: ~ 4GB
Version 5: ~ 3GB
* Server Parameters memory allocation limits:

For following buffers:
• key_buffer_size
• join_buffer_size
• myisam_sort_buffer_size
• sort_buffer_size
– Maximum allowable setting is 4GB.
– As of MySQL 5.1.23, values larger than 4GB are allowed for 64-bit platforms.

net_buffer_length -1MB.
read_buffer_size – 2GB.
read_rnd_buffer_size – 2GB.

* About globally limiting memory usage by MySQL Server:

MySQL is not having any other parameter that directly limits the memory usage. It’s these global buffers / server variables are the only setting which extends / shrinks server memory usage limits.

Eg. max_connections, variable for specifying number of simultaneous connection, depends on OS limits to handle file handlers. No documented limit is there for the same.


-- Kedar Vaijanapurkar --