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

Bash Examples - Mistakes to Avoid

Bash Examples
A bash script is a text file containing a list of commands to be executed by the bash shell. A long time back, I had written a guide on Bash scripting which teaches you the basic syntax of bash scripts in a mere 10 seconds.

However, while writing bash scripts, you should keep a sharp lookout for any mistakes that may arise in your bash syntax. This is where Greg's wiki comes into the picture. Greg's wiki has a page which lists around 29 bash examples that highlight all the common mistakes that people usually make while writing bash scripts.

Two repositories of Linux and Unix commands and example dot files

Does your job involve writing complex bash commands ? Or perhaps you are interested in learning tips and tricks on writing versatile dotfiles. Well, here are two sites which are dedicated to collecting useful tips so as to help Linux users better.

Shell-fu - Is a great site with a growing collection of command line goodies. What makes it interesting is that there are multiple combinations of commands which achieve different tasks. And users can submit and vote for their favorite commands.

Dot files - If you precede a file with a dot, it is considered as a hidden file in Linux. There are numerous hidden files in Linux. The standard ones being .bashrc, .profile, .Xdefaults, .xinitrc and so on. But it doesn't limit to these files. Many applications create hidden files too.

Enter the following command in your terminal to see the hidden files in your home directory.
$ ls -a
This site is a repository of collections of dot files. The files are contributed by its visitors and are divided into different sections namely .vimrc, .screenrc, .bashrc, .zshrc, .Xdefaults, .conkyrc, .irbrc, .bash_profile, .xinitrc and .emacs.

Python scripts vs Bash scripts in Linux

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

Ever since I started using Linux, BASH has been the shell of my choice. Though I have used numerous other shells like Korn Shell, Bourne shell (which is the default shell in Unix), C shell and so on. Bash is actually an acronym for Bourne Again SHell.

Richard Bradshaw who is a teacher at a school in United Kingdom has put together a page detailing some of the common annoyances he has encountered while using BASH and ways of overcoming them.

As a recap, do take a look at some of the previous articles related to BASH shell on this blog such as ...

BASH shell shortcuts,
10 seconds guide to BASH scripting,
BASH completion,
Making your BASH scripts user friendly,
Quick editing of a command and
BASH FAQ ... just to name a few.

Benchmark : Which is the best Linux terminal ?

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.

10 Linux shell tricks you don't already know

Venture cake - a new blog on the horizon has written an insightful piece on the different ways in which one can use common commands to achieve complicated tasks in Linux.

The blog author provides examples in achieving the following :
  • A simple way to send output and errors to the same file. It is known as input-output redirection which I had covered a long time back.
  • Parallelize your loops by backgrounding them.
  • Catch memory leaks by using top command via cron.
  • Redirect output from other programs as standard input to a command directly from the command line.
  • Set a random initial password and force the user to change it.
  • Add Your Public Key to Remote Machines the Easy Way
  • Extract an RPM without any additional software
  • See How a File Has Changed from Factory Defaults
  • Undo Your Network Screwups After You’ve Lost the Connection
  • Check which Ports are Open on a remote machine using 'nc' command.
  • The Easy Way to Extract Tar Archives
  • Use Bash Shell variables - not the same as Bash shell shortcuts - I may add.
  • Never reboot a system for NFS failures
Apart from listing examples of each of them, he also provides in a couple of sentences the benefits and drawbacks of each trick. Check out this very interesting article to find out about the examples and explanation provided.

Bash FAQ - Frequently Asked Questions

I was once motivated enough to write a guide on Bash scripting which I chose to call "The 10 Seconds Guide to Bash Scripting" which, if missed, you can read here. Even though many of you may challenge me on the time I claim is enough to read through the guide, I am sure you will find it useful.

Today I came across a collection of tips that are compiled into an FAQ which explain many questions related to Bash scripting. Considering that Bash shell is the default shell for all GNU/Linux distributions and it being bundled with other Unix OSes, it has in my opinion gained more credibility than the erstwhile Bourne shell (which btw is still the default shell for Solaris). The FAQ is maintained by a person named Greg and can be accessed here. There are altogether 75 questions followed by their answers. Aside from being informative, many of them are interesting as well.

While we are on the topic of Bash shell, you may also be interested in reading a list of the common Bash shell keyboard shortcuts.

Useful site to learn shell scripting

In the past, I have written an article on this blog titled - 10 seconds guide to Bash Scripting - which explained the finer nuances of learning to write scripts for the bash shell. But there is much more to it than scripting for one particular shell as there are numerous shells available for the Linux/Unix environment.
The site Shelldorado positions itselves as a site which explains all the things related to shell scripting. This site contain among other things, articles explaining good coding practices while writing scripts, tips and tricks related to scripting, a good collection of scripts and much more. I found this site really informative in its depth of coverage of the art of shell scripting. In fact the tips and tricks section is categorized into beginner, intermediate, script programmer and advanced sections and is worth a look over.

 
 
 
 
Copyright © Sun solaris admin