≡ Menu

bash

Poll: Your Favorite Scripting Language?

Like most sys admin, I'm lazy. I try to automate almost all things in order to save time. Inexperienced sys admin and help desk staff working under me finds all these tools useful. It saves their time and avoids security issues. Automation allows help desk staff to do things that they don't have enough direct system knowledge to do themselves. However, selecting correct tool and applying correct methodology is very important.
[click to continue…]

Why should shell languages be the only ones left out of the ${YOUR_LANGUAGE_HERE} Server Pages fad? This document chronicles foray into the not-so-fascinating world of ${YOUR_LANGUAGE_HERE} Server Pages technology. All of the code developed is available for free download, so you can use Bourne Shell Server Pages to build your very own killer Web application.

=> Bourne Shell Server Pages [hyperrealm.com]

This is a classic problem. One of our FAQ is about cron job. I received lots of email with a question:

How do I run my script on 3rd Monday or 4th Friday only?

Cron does not offer this kind of facility i.e. you cannot run a script on the Nth weekday of the month.

However with one shell liner you can force to run a script on a given day:

Consider following date command, it will print day:
$ date +%a
Output:

Thu

You can compare output with weekday name using bash test [exrp ] syntax and the control operators && (AND list), you can write:
$ [ $(date '+%a') == 'Thu' ] && echo 'Today is Thu, run a command' || echo 'Noop'

First echo command get exectued only on Thursday. Now all you have to do is write a cron job to execute on first Monday:
# crontab -e
Now append code as follows:
# Run a script called myscript.sh on First Monday at 11:30:
30 11 1-7 * Mon [ "$(date '+%a')" == "Mon" ] && /path/to/myscript.sh

Hope this small tip will save your day. Please do share some of your favorite bash / shell scripting hacks in the comments. I will highlight some of the best in next shell scripting post.

See also:

How do I find out what shell I’m using?

Asked by Chetan Joshi

Q. What is the best way to find out what shell I'm using. echo $SHELL is not so reliable. Please let me know any tiny command or trick.

A. Chetan, echo $SHELL should work. But here is old good UNIX trick. Use the command ps with -p {pid} option, which selects the processes whose process ID numbers appear in pid. Use following command to find out what shell you are in:

ps -p $$

So what is $ argument passed to -p option? Remember $ returns the PID (process identification number) of the current process, and the current process is your shell. So running a ps on that number displays a process status listing of your shell. In that listing you will find the name of your shell (look for CMD column) .

$ ps -p $$
Output:

  PID TTY          TIME CMD
6453 pts/0    00:00:00 csh

From my Linux box:
$ ps -p $$
Output:

  PID TTY          TIME CMD
5866 pts/0    00:00:00 bash

You can store your shell name in a variable as follows :
MYSHELL=`ps -hp $$|awk '{echo $5}'`

Please note those are backquotes, not apostrophes

Or better try out following if you have a bash shell:

MYSHELL=$(ps -hp $$|awk '{echo $5}')

Working at the shell prompt is an essential task for any Linux system administration. However many newcomers find it difficult to work at the bash prompt. Here are some tricks to speed up your work.
[click to continue…]