An introduction to scripting
The ultimate edge that Linux has to offer over Windows is the ability to do extensive scripting. Nearly all tasks on a Linux station can be done through the use of scripts. There are many scripting languages supported out of the box or with minimal installation on a Linux stations, like PHP, perl,... But a lot of people are surprised that even the (standard) Linux shell, BASH, offers extensive scripting.
These so-called shell scripts are really easy to learn because they use the commands that you actually use in a shell. So there's no need to remember functions or syntaxes specific to the language. Giving a detailed guide about Linux scripting is beyond the scope of this article - there are hundreds of sites devoted just to shell scripting. But why not show you what scripting can do with an example?
You will probably remember that you had to add a user to the samba password back-end separately. Now, if you want to add another user, you have to remember all of those steps - because if you don't, the user cannot access the samba shares. So first we have to add the user to the system, and afterwards we have to add him to the samba password back-end. If the user gets VNC access, that's a whole additional step. So let's write a small and easy script that handles this for us.
Fire up a terminal. Root or not doesn't matter that much to write the script, however, it does matter in the execution of the script. It is logic that only root can add users to the system, right? So lets build a check for this in the script too. We will call the script 'useradd.sh', so we issue “nano useradd.sh”. This will create a file named 'useradd.sh' in our parent directory (since it's a fresh terminal, that would be your home directory) and let nano (the file editor) open it.
To start our script, we have to tell the system what to use to execute the script. This is going to be a shell script, so we will use bash. We tell this to the system by the special string “#!” followed by the path to the script interpreter. Therefor, the first line of the script will be “#! /bin/bash”. From now on each line will be interpreted as 'BASH language' In BASH, comments start with a “#” and continue to the end of the line. Let's personalise our script in the OSS spirit, by claiming credits at the start of the script. so, line 2 will become something along the lines of “# Useradd script written by Ken Gypen (Glider)”. Be creative, as long as it starts with a “#”, all is good.
So, now on to the serious work. First we shall check if we execute this script as root. There are a lot of ways of doing this, but probably the easiest way is through the use of the 'whoami' command. If you type 'whoami' in a terminal, the system will return your username. Used in a script it wil return the username of the one executing the script, but in BASH system commands have to be placed between backtics “`”(not single quotes). So we want to know if the output of the 'whoami' command is “root”, if it isn't, the script has to exit with a fancy exit message. So let's break it down line by line in BASH code.
We start with the comparison, using an 'if' structure. So, “if [ `whoami` != “root” ]” will be our next line. If the output of the command is not equal to root. Easy isn't it? Now, on the following line, we just write “then”. So if it's not equal to root, then... And the things to do go on the following lines. First we shall inform the user by showing a fancy line of tekst, and then we exit the script. Showing text is done through the command “echo”. So the line under 'then' will be something along the lines of “echo “You should be root to add users!””, followed by “exit“ on the next line. Now we have to let the script know that the end of the if statement is reached. We do that in BASH by typing “fi” (if backwards) on the next line.
Several popular Linux sites, including LinuxOnline and LinuxQuestions.org offer scripts for download that you can tweak to fit your needs.
There, now we are sure that everything that is after that if statement is run as root, because otherwise the script exits. Now, we will ask the user to input the username of the new user, which we will use in the “useradd” and “smbpasswd” command. The former command to add the user to the system, the latter to add the user to the samba password backend. We ask for input through the “read” command. The input is saved in a variable, which we will call 'username'. So, next 2 lines in our script will ask for a username, and store it in the variable. Just type “echo -n “Username: “” and “read username” on the next 2 lines.
Now, on to the adding of the user. Useradd takes quite a few switches to set things like the location of the users homedir and the users default shell. For more information about useradd, just type “man useradd” in a terminal. In our case, we want to set the shell to /bin/bash, and the userdir to /home/<username> and also make sure that directory is created. This gives us following command: “useradd -d /home/$username -m -s /bin/bash $username” notice the “$” in front of the username variable. This is BASHs way of calling variables. So, if we entered “bit” as username, the command will become “useradd -d /home/bit -m -s /bin/bash bit” Now, we also want to set a password for the user, this is done through the “passwd” command. So the next line will simply be “passwd $username”.
Now, the final step is adding the user to the samba backend. The command “smbpasswd” with the correct switch (-a in our case) will do that for us. So, the final line will become "smbpasswd -a $username". There you go, save and exit and you have just written your first BASH script. It should be something along the lines of:
#! /bin/bash
# Useradd script written by Ken Gypen (Glider)
if [ `whoami` != “root” ]
then
echo “You should be root to add users!”
exit
fi
echo -n “Username: “
read username
useradd -d /home/$username -m -s /bin/bash $username
passwd $username
smbpasswd -a $username
Try running it as a your regular user by issuing
sh useradd.sh. You'll be greeted by your message and the script will just stop. Now try “sudo sh useradd.sh” (or change to user root first by issuing
sudo su and then call the script). You'll see the script does indeed work.
I hope you understand the power that scripts possess. There are plenty of (more complex) scripts floating around the net, usually being offered on the various forums dedicated to Linux and/or Open Source Software. Almost all are Open Source, meaning it's hard to not find what you're looking for or be able to tweak it to do just what you need. I've seen scripts do as little as echo the system variables, or as much as sorting entire collections of MP3s by
reading their ID3 tags, automatically
ripping DVDs, converting
MPEG movies to AVI, and pretty much anything else that can be done on the computer.
Want to comment? Please log in.