≡ Menu

shell script

FAQ Updates: April/03/2012

Our FAQ section has been updated. Here are latest howtos:

Make the most of nixCraft FAQ Section

Get intimated about our new howtos / faqs as soon as it is released via RSS feed OR follow us on twitter OR join our 20k+ facebook fans for more tips and news about Linux.

FAQ Updates – Feb/07/2011

Our FAQ section is updated in last few days with new howtos:

  1. HowTo: Upgrade Debian 5.0 (Lenny) To Debian 6.0 (Squeeze)
  2. regionset: Linux Set / Change DVD Region Code
  3. HowTo: Check Ram Size From Redhat Linux Desktop System
  4. [click to continue…]

How To Use vi as Default Editor and Viewer in Midnight Commander

This is an user contributed article.

Midnight Commander (mc) is an user-friendly text-based file manager UI for Unix. Using mc, you can browse the filesystem easily and manipulate the files and directories quickly. You will not miss the standard command line prompt, which is also available within the mc itself. If you are new to mc, Midnight Commander (mc) Guide: Powerful Text based File Manager for Unix article will give you a quick jumpstart. In this article, let us review how to solve couple of common annoyance about viewing a file in mc.

Use vi as default editor and viewer in mc

Mc uses mcedit for file editor and mcview for file viewer. Like most of you, I'm very comfortable with vi and would like to use vi for both viewing and editing than the mc's internal editor and viewer.

Launch mc in color mode by typing “mc -c” from the command line. Press F9 (or Esc followed by 9) to activate the top menu → select Options menu → select Configurations menu-item, which will display “Configure Options” dialog → de-select the check-box next to “Use Internal Edit” and “Use Internal View”, as shown below to disable, internal editor and viewer.

After this change, when you select a file and press Esc 3 to view or Esc 4 to edit, mc will use vi.

Fig.01 Mc configure options to disable internal editor and viewer

Fig.01 Mc configure options to disable internal editor and viewer

Change the Enter key behavior to view file using vi instead of executing it.

When you select a shell script and press enter, mc will execute it by default. Also, by default when you press enter on text files, nothing happens. I prefer to view the shell script when I press enter key. Also, I would like to view the text file using vi when I press enter key. You can achieve this by modifying the mc extension file as shown below.

Press F9 (or Esc followed by 9) to active the top menu. From command menu → select “Edit extension file” men-item → This will display the extension file. Go to the bottom of the extension file and change the value of open and view parameter values as shown below.

# Default target for anything not described above default/*

        Open=%var{EDITOR:vi} %f
        View=%var{EDITOR:vi} %f 

After the above change, when you press enter key on a shell script, it will open it in vi instead of executing it. This will also open text files in vi when you press enter after selecting it.

Interesting stuff – Aug 8, 2008

=> Cisco 7200 Simulator for Linux. If you decided to study for the Cisco certification, this tool may come handy. Howtoforge has detailed tutorial on setting up a Cisco lab on Linux system. Dynagen is a front-end for use with the Dynamips Cisco router emulator. It uses an INI-like configuration file to provision Dynamips emulator networks. It takes care of specifying the right port adapters, generating and matching up those pesky NIO descriptors, specifying bridges, frame-relay, ATM switches, etc. It also provides a management CLI for listing devices, suspending and reloading instances, determining and managing idle-pc values, performing packet captures, etc.

=> You can capture video of all of the amazing things happening on your desktop with one of Linux's many screencasting applications. These programs are perfect for creating demonstrations for blogs and tutorials, and for illustrating projects with more than just still images.

=> Postfix Daily Quota reportA shell script hack to create daily quota report for a Posfix mail server including file system usage of each e-mail account.

=> The developers of Firefox have unveiled an experimental project, Snowl, designed to gather all your inbound communications, whether they're in the form of email, RSS, Twitter, or social network updates.

=> IBM has marked its 10 years of participation in Linux and open source with an open source code contribution focused on supercomputing. The software is available immediately from a software repository run by the University of Illinois's National Center for Supercomputing Applications (NCSA).

=> Regular nixcraft contributed Ramesh has published a simple 6 steps to secure home wireless router / network

=> LinuxLeak is a new daily destination for all your Linux and Open Source news headlines, updated every 15 minutes.

Here is a quick way to fix half installed packages under Debian / Ubuntu Linux.

If you see an error while removing or installing debian / ubuntu package that read as follows:

(Reading database ... 147148 files and directories currently installed.)
Preparing to replace octopussy (using octopussy_0.9.6.2_all.deb) ...
Unpacking replacement octopussy ...
dpkg: warning - old post-removal script returned error exit status 10
dpkg - trying script from the new package instead ...
dpkg: error processing octopussy_0.9.6.2_all.deb (--install):
subprocess new post-removal script returned error exit status 10
dpkg: error while cleaning up:
subprocess post-removal script returned error exit status 10
Errors were encountered while processing:

In above octopussy package is not getting removed because of script error. Mostly these are shell script written using /bin/sh. These scripts called with -e option which means if not interactive, exit immediately if any untested command fails. The exit status of a command is considered to be explicitly tested if the command is used to control an if, elif, while, or until; or if the command is the left hand operand of an && or || operator. In short shell scripts will abort with an error if something fails. To fix this problem visit /var/lib/dpkg/info directory
# cd /var/lib/dpkg/info
Now find out files in following format:
$ ls octopussy.*
Now open each file and find out line that read as follows:
#/bin/sh -e
Replace with
Save and close all the files. Now run apt-get / dpkg command again to remove package:
# apt-get --purge remove octopussy
# dpkg -r octopussy

Rotate FTP Backup Using a Shell Script

I've already written about rotating sftp / ssh backup shell script to remove directories (old backup files). However, a few of our readers would like to know more about removing old backup directories using ftp. As usual, you need accurate date and time on local system and remote backup directory must be in dd-mm-yyyy or mm-dd-yyyy format. For example daily mysql backup should be stored in /mysql/dd-mm-yyyy format.

Sample Shell Script

Here is a simple and dirty shell script to remove old backups ( download link ):

# call ./script.sh 03-2007 - to remove all March-2007 directories in 01-03-2007, 02-03-2007, 31-03-2007 format
# you must have ncftp ftp client installed on BSD / Linux box
BASE="/mysql" # base dir below that dd-mm-yyyy
[ $# -eq 0 ] && exit 1 || :
echo "Getting old directories..."
ncftpls -u 'ftp-user-name' -p 'ftp-password' -x "-t" ftp://ftp.your-server.com${BASE} > /tmp/ftp.out
LIST="$(grep ${DELETE} /tmp/ftp.out)"
echo -n "Starting removal for ${DELETE}..."
for dir in $LIST
# echo "Processing ${dir}..."
 ncftp -L -u 'ftp-user-name' -p 'ftp-password' ftp.your-server.com <<EOF
 cd $rdir
 rm *
 rmdir $rdir

Run the script as follows to remove all backup for Dec-2007, enter:
$ ./script.sh 12-2007

Related: Generate backup ftp script using php based wizard

How to: Check the bash shell script is being run by root or not

Sometime it is necessary to find out if a shell script is being run as root user or not.

When user account created a user ID is assigned to each user. BASH shell stores the user ID in $UID variable. Your effective user ID is stored in $EUID variable. You can

Old way...

You can easily add a simple check at the start of a script:

Check the script is being run by root user

# Init
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
# ...

New way: Using EUID

# Init
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
# ...

Mount /dev/sdb1 only if you are a root

if [[ $EUID -ne 0 ]]; then
  echo "You must be a root user" 2>&1
  exit 1
  mount /dev/sdb1 /mnt/disk2

Updated for accuracy and more examples.