For Linux and Shell scripting.

Leave a comment

Sha-Bang (#!/bin/bash) line in Scripts

The sha-bang line at the beginning of the script tells your system that this file is a set of commands to be fed to the command interpreter indicated. Immediately following the sha-bang is a path name to the program that interprets the commands in the script.

The #! line in a shell script will be the first thing the command interpreter (sh or bash) sees. Since this line begins with a #, it will be correctly interpreted as a comment when the command interpreter finally executes the script. The line has already served its purpose – calling the command interpreter. If the script includes an additional #! line, then shell will interpret it as a comment.

Of course, the sha-bang (#!) line must be the very first line in the script. Even a blank line above that could change the meaning, and it would then be considered as a comment. However space between #! and path to interpreter should work.

How it works?

Note: These are my assumptions after a series of tests. I haven’t seen this way of explanation in any other sites. I also couldn’t collect any proofs from strace or similar tools.

As soon as your shell find the magic sha-bang line, it feeds your script file as an argument to the command interpreter mentioned there. Here is a simple test script which uses /bin/rm as sha-bang. When you executes the script, it runs successfully (but the commands within the script won’t be executed). The return value of the execution will be 0 (success), but you won’t find your script after that. The /bin/rm would have already deleted it.

$ cat


# self destructing script

echo hello world


$ ls -l

-rwxr–r– 1 root root 27 May 29 20:47

$ ./

$ echo $?


$ ls -l

ls: No such file or directory


You can repeat this test with /bin/more as sha-bang instead of /bin/rm. It will show the script contents (like you do more on the command line), instead of executing the script.


Leave a comment

Linux: Monitor your mount points to see if they go to read-only mode

This bash script should be helpful to you if your partitions go to read-only mode frequently. This script will check if any of your partition is in read-only mode. If yes, it will remount into read-write mode. You will be alerted via email if the remount fails.

You can download the script from here.


Leave a comment

C Program to setuid to root user (run programs owned by other users)

Sometimes you will need to permit normal users to run programs that are owned by other users (or root). Suppose, you have some important data under root’s ownership and you don’t want the users to read/modify them. But, users require them during their daily works. In such situation, you can make use of this program.

This program will call the shell script /root/scured/scripts/ which inturn will run under root’s ownership and should be able to access any files in the system. The users can access root owned files only through this program.

Along with userid (uid of the user who runs this program), this program will pass 9 commandline arguments to the shell script. If you want to pass more commandline arguemetns, you can add it. *

gcc setuid.c -o setuid-pgm *
chmod +s setuid-pgm *
mv setuid-pgm /usr/bin/

You can download the program from here.


Leave a comment

A bash script for ping test

Here is a simple bash script to ping multiple hosts at a time. It’s a very simple and might not be useful always. I just wrote this when I wanted to test hundreds of IP addresses by ping.

# Usage: pingtest
#              pingtest

You can download the script from here.


Leave a comment

A Sample PHP Script for uplaoding Files to a Server

Here is a PHP script that can be used to upload files to a server:

<body><form action=”upload_file.php” method=”post”
<label for=”file”>Filename:</label>
<input type=”file” name=”file” id=”file” />
<br />
<input type=”submit” name=”submit” value=”Submit” />


The above HTML form calls a PHP script named upload_file.php. Here are the contents for upload_file.php.

if (($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”)
&& ($_FILES[“file”][“size”] < 20000))
if ($_FILES[“file”][“error”] > 0)
echo “Return Code: ” . $_FILES[“file”][“error”] . “<br />”;
echo “Upload: ” . $_FILES[“file”][“name”] . “<br />”;
echo “Type: ” . $_FILES[“file”][“type”] . “<br />”;
echo “Size: ” . ($_FILES[“file”][“size”] / 1024) . ” Kb<br />”;
echo “Temp file: ” . $_FILES[“file”][“tmp_name”] . “<br />”;if (file_exists(“upload/” . $_FILES[“file”][“name”]))
echo $_FILES[“file”][“name”] . ” already exists. “;
“upload/” . $_FILES[“file”][“name”]);
echo “Stored in: ” . “upload/” . $_FILES[“file”][“name”];
echo “Invalid file”;

The above script saves the uploaded file into a folder called upload and then prints a description about the uploaded file. Please remember to provide write permission for the upload folder for all users.