Mohammedz.com

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 test2.sh

#!/bin/rm

# self destructing script

echo hello world

$

$ ls -l test2.sh

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

$ ./test2.sh

$ echo $?

0

$ ls -l test2.sh

ls: test2.sh: 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.

Reference:

http://tldp.org/LDP/abs/html/sha-bang.html

Advertisements


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.

~mohammed


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/exec.sh 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.

~mohammed


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 192.168.1.0/24
#              pingtest 192.168.1.0-255

You can download the script from here.

~mohammed