Showing posts with label bash shell. Show all posts
Showing posts with label bash shell. Show all posts

Python scripts vs Bash scripts in Linux

February 11, 2008 5 comments
Almost all Linux users who are at least familiar with bash shell commands will be at home in writing simple bash scripts to automate tasks. For those who do not know how to write a bash script, here is how you do it.

Open a new file in your favorite text editor and save it in any name of your choice but with the extension .sh. For example, let us name it myfirstscript.sh . The sh file extension is not mandatory but is a nice way for us to later remember that this file is a script.
Now in this file (myfirstscript.sh), enter the following line.

#!/usr/bin/bash

This line should always be entered the first thing in any bash script you create. It lets the shell know where to find the bash program.

In some Linux distributions the bash command is located at /bin/bash.


When in doubt, use the which command to find the location as follows :

$ which bash
/usr/bin/bash


# Continuation of the above script ...
# Now lets enter a few commands in this script ...
ls -l
sleep 2
who
sleep 2
w

Nothing spectacular here. Note that sleep is used to pause the execution process for certain time in seconds. Next fire up a console (xterm, gnome-terminal ...) and set the executable bit for the script myfirstscript.sh as follows :

$ chmod u+x myfirstscript.sh

This lets you execute the script by its name as follows :

$ ./myfirstscript.sh

All this may seem easy.

But you can get added benefits if you substitute bash with Python language to write your scripts.

Noah Gift provides compelling reasons to pick up Python programming language skills and start writing your scripts in this language.

If you do not know Python language and is looking for some direction then you should look at the two books Core Python programming and Python phrasebook which will give you a head start in mastering this powerful but easy to learn language.

BASH Tips and Tricks

December 03, 2007 0 comments
This is a collection of a few tips that will help you in using the Bash terminal efficiently.

Bash Tip 1: Make your Bash scripts user friendly.

If you are writing shell scripts in Bash, you can make your scripts user friendly using the dialog command line tool. Using this tool, you can create lists, dialog boxes, message boxes, buttons, and so on. Read this guide to know more about constructing a Dialog.

Bash Tip 2: Quickly edit a lengthy command in Bash

You entered a lengthy command in the Bash shell and pressed the Enter key to execute the command. However, there was an error in your typing and you want to go back and correct the mistake. No worries, fc command will come to your rescue.

Bash Tip 3: Bash completion

You can extend the functionality of the Bash shell and make it more user friendly if you install an add-on package called Bash completion.

Bash Tip 4: Learn Bash Scripting in a mere 10 SECONDS

Don't believe it? Then wait till you read the guide Bash shell scripting. It teaches you how to write Bash shell scripts in a mere 10 seconds. Or your money back!!

Bash Tip 5: Bash shell shortcuts

Bash shell supports a number of short cut keys that aid you in moving around in the shell. Many of these short cut keys are real time savers. To know more, read the article Bash shell shortcuts.

Bash Tip 6: Bash shell variables

Bash shell associates special meaning to a few set of characters. Using these characters in Bash scripts offer you a great deal of flexibility. Read more in the article - Bash shell variables.

Benchmark : Which is the best Linux terminal ?

September 02, 2007 2 comments
It has many times occurred to me which terminal among the plethora of terminals at our disposal are the most efficient in terms of speed, memory consumption and ease of use. And I was of the opinion that the ubiquitous xterm was a likely candidate for a terminal with less memory footprint. Well I couldn't have been further from the truth.

It seems xterm is a very poor choice when selecting a terminal. Martin Ankerl benchmarked a number of terminals available in Linux including xterm, gnome-terminal, KDE konsole, wterm, eterm and aterm. And his findings indicate gnome-terminal and konsole as the best choice of terminals for its speed. And by speed I mean the speed of execution of commands in it and not the program's startup time. But the down side is that these two terminals consume a significant amount of memory. For example, Gnome-terminal is found to consume around 45 MB.

Martin states that he found wterm as the best bet for a low memory footprint with just 6.5 MB consumption. And it is sufficiently fast in executing. xterm loses in this department too with each instance consuming around 16 MB.

Do read the full benchmark details and also a nifty graph of his results.

Bash Shell Scripting - 10 Seconds Guide

October 23, 2005
This Bash shell scripting guide is not a detailed study but a quick reference to the BASH syntax. So lets begin...

If you like this 10 seconds guide, don't forget to read -

Common environment variables


PATH - Sets the search path for any executable command. Similar to the PATH variable in MSDOS.

HOME - Home directory of the user.

MAIL - Contains the path to the location where mail addressed to the user is stored.

Make your bash scripts user friendly using - Dialog

October 22, 2005
Dialog is a command line tool that allows you to create professional looking Dialog boxes and other GUI controls using any shell scripting language.

You can easily master shell scripting in just 10 seconds!!


Dialog supports 8 types of GUI controls namely -

  1. Menu boxes
  2. Input boxes
  3. Message boxes
  4. Radio lists
  5. Text boxes
  6. Checklist boxes
  7. Info boxes, and
  8. Yes / No boxes.

Creating a dialog is very easy. The following examples will throw light on its usage.

Input boxes


Input boxes allow the user to enter a string. After the user enters the data, it is written to standard error . You may also redirect the output to a file.

$ dialog --title "Ravi's Input Box" --inputbox "Enter the parameters..." 8 40

As you can see, the options are self explanatory. The last two options 8 and 40 are the height and width of the box respectively.

And this is how the resulting input box looks on your terminal.

Input box
Fig: Inputbox

Text box


A text box takes a file as the parameter and shows the file in a scrollable box.

$ dialog --title "textbox" --textbox ./myfile.txt 22 70

... it shows the file myfile.txt in a textbox.

Text box
Fig: Textbox displaying the file.

Check list


A checklist presents the user with a list of choices. The choices can be toggled ON or OFF individually using the space bar.

$ dialog --checklist "Choose your favorite distribution:" 10 40 3 1 RedHat on 2 "Ubuntu Linux" off 3 Slackware off

In the Dialog command shown above, 10 is the height of the box, 40 - width, 3 is the number of choices, and the rest are the choices numbered 1,2 and 3.

Radio list


A radio list displays a list containing radio buttons. The user can choose only a single option from the set of options.

$ dialog --backtitle "Processor Selection" --radiolist "Select Processor type:" 10 40 4 1 Pentium off 2 Athlon on 3 Celeron off 4 Cyrix off

10 and 40 are the height and width respectively. 4 denotes the number of items in the list, followed by the items.

Info box


An info box is useful for displaying a message while an operation is going on. As an example, see the code below:

$ dialog --title "Memory Results" --infobox "`echo ;vmstat;echo ;echo ;free`" 15 85

Info box
Fig: Information box - listing the vmstat and free listing.

Message box


And the following is a message box.

% dialog --title "Message" --msgbox "You are PROHIBITED from rebooting your machine!!" 8 30

Message box
Fig: Message box

Dialog is usually used inside a script which gives the script a degree of user friendliness.

There is another package called Xdialog which gives the same features for scripts executed in X Windows. Xdialog utility also has additional functionality not found in the dialog utility.

To know more in using the Dialog command line tool in Linux, check its manpage.

Bash Completion - Makes life easier for Linux users

September 13, 2005 4 comments
One thing that really makes working in the command line in Linux a pleasure is the various in-built shortcuts and name completion features in Bash - the default shell in Linux.

But one grouse I always had was it was really difficult to remember all the options that each command had. For example, 'find' came with numerous options which I found difficult to memorize and had to resort to reading the man page each time I had to use the command. Now you can enhance the bash shell to give you the added functionality of listing the options that can be used with a command. For that you should download and install an add-on package called bash-completion. I use Fedora Core 2 but if you are using the latest Linux distribution, it might be installed by default on your machine.
In Debian based Linux distributions, you may install it using the following command :
# apt-get install bash-completion
After installing the bash-completion package, fire up a terminal and type:

$ grep --

... followed by two TABs and you get all the options that can be passed to the grep command (see figure). This works for any command in linux. Now you don't have to remember all those options that need be passed to the programs any longer.

bash completion
Also read:
Bash Shell Shortcuts
Special Shell Variables

Bash Shortcuts

August 31, 2005
Bash, which is the default shell in Linux contains a whole lot of key bindings which makes it really easy to use . The most commonly used shortcuts are listed below :

CTRL Key Bound


Ctrl + a - Jump to the start of the line

Ctrl + b - Move back a char

Ctrl + c - Terminate the command

Ctrl + d - Delete from under the cursor

Ctrl + e - Jump to the end of the line

Ctrl + f - Move forward a char

Ctrl + k - Delete to EOL

Ctrl + l - Clear the screen

Ctrl + r - Search the history backwards

Ctrl + R - Search the history backwards with multi occurrence

Ctrl + u - Delete backward from cursor

Ctrl + xx - Move between EOL and current cursor position

Ctrl + x @ - Show possible hostname completions

Ctrl + z - Suspend/ Stop the command

ALT Key Bound


Alt + < - Move to the first line in the history

Alt + > - Move to the last line in the history

Alt + ? - Show current completion list

Alt + * - Insert all possible completions

Alt + / - Attempt to complete filename

Alt + . - Yank last argument to previous command

Alt + b - Move backward

Alt + c - Capitalize the word

Alt + d - Delete word

Alt + f - Move forward

Alt + l - Make word lowercase

Alt + n - Search the history forwards non-incremental

Alt + p - Search the history backwards non-incremental

Alt + r - Recall command

Alt + t - Move words around

Alt + u - Make word uppercase

Alt + backspace - Delete backward from cursor

More Special Key bindings


Here 2T means Press TAB twice. And $ is the bash prompt.

$ 2T - Display all available commands(common)

$ string 2T - Display all available commands starting with string.

$ /2T - Show entire directory structure including hidden ones.

$ 2T - Show only sub-directories inside including hidden ones.

$ *2T - Show only sub-directories inside excluding hidden ones.

$ ~2T - Show all present users on system from "/etc/passwd"

$ $2T - Show all sys variables

$ @2T - Show all entries from "/etc/hosts"

$ =2T - List output like ls or dir

Special Shell Variables

June 06, 2005 0 comments
These are the special shell variables which can be used in shell scripts in Linux.

$# - Denotes the number of arguments you have passed to the shell script from the command line.

$@ - All arguments, as separate words.

$* - All arguments, as one word.

$$ - ID of the current process.

$? - Exit status of the last command.

$0,$1,..$9,${10},${11}…${N} - These are positional parameters. After “9″ you must use the ${k} syntax.

Here is an example of a shell script using one of the special shell variables.

#!/bin/sh
#File: test.sh

  echo $$

I have used the code echo $$. If you run the script, it will print its process ID.

To know more about a process in Linux, read the article -
What is a process in Linux ?.