Shell scripting Tutorials

Preserve permissions and ownership when copying files using cp

In this tutorial, we will look at copying files in Linux using the cp command and preserving the existing permissions and ownership on the files which are being copied. By default, the cp command will change the owner of the destination files to the person who did the copy. It will also change the permissions on these files to default permissions; usually 644.

Which shell am I using?

In this tutorial we will look at how to find out which shell is currently being used. Let's say that you just got shell access on an Ubuntu Linux box or some other box with a Linux distribution. The first thing you should want to know is what shell you are using. This is because different shells offer different features. You may well come to realise that if you are not using a shell that you are familiar with, you may be less productive.

To find out which shell you are currently using, type the following command:

echo $SHELL

Count search engine referrals using web server logs and shell commands

Suppose you want to know how much of your website traffic comes from Google or another search engine. It's very easy if you use Awstats or Google analytics, but what if you haven't configured these tools? Using only the web server log files and some shell commands will enable us to quickly parse log files and give a count of how many referrals we had from a search engine.

For the purposes of this tutorial, we will be assuming an Apache web server, and that the search engine that we are counting the referrals from is Google.

Disable user login to SSH

In this tutorial, you will learn how to disable user logins via SSH. By default on newer Ubuntu systems and some other Linuxes with SSH installed, all users who have an account on the system are permitted to login via SSH. This is undesirable in certain situations.

Backup Website and Email Encrypted Backup using a shell script on Ubuntu/Linux

In this tutorial, we will show you how to backup a website and then encrypt the backup before emailing it using only a shell script. This guide is for those who are running their web server on Ubuntu or another version of Linux. This is very useful as a website can be backed up automatically using cron, encrypted for protection, and emailed to a safe place.

Bash For Loop

The Bash for loop is useful when we want to repeat some instructions a specified amount of times. Loops are considered iterative commands as they perform iterations, also known as repetitions.

For example, if we want to run a particular command 5 times, we would specify the command inside of a for loop and then tell the loop to run 5 times.

The following tutorial explains.

Get Bash Version

If you are wondering what version of Bash am I using, then you have come to the right place. There are a number of reasons why one would want to know their Bash version. The most likely reason is compatibility. Even though the commands remain the same for the most part, different versions of Bash may add different features and functionality that may not be available in older versions.

Substring in Bash

To find a substring in Bash, we use the bash substring operation, one of the many string manipulation operations that the Bash shell offers.

This substring operation is similar in syntax to the PHP Substring operation for those of us who do PHP programming.

Retrieving a substring in Bash is fairly easy as we will see in the tutorial below.

The syntax for the Bash substring function the way we will use it is as follows:

STRING:start:length

Concatenate strings in Bash

To concatenate strings in Bash, we use special characters to delineate the variable from the literal string.

String concatenation is useful for joining the values of variables to a string before outputting it or using it to perform some other function.

The Bash concatenate tutorial below shows how this not so well known technique works.

If we wanted to concatenate a variable to a string in a Bash script, we would do it as shown below:

#!/bin/bash
 
LOVE="Tutorial Arena"
OUTPUT="I love ${LOVE} so much!"

Run shell script from PHP

Running a shell script or command from PHP offers a number of conveniences to an administrator. When properly set up, shell scripts and commands can be run by simply requesting a web page over the Internet. The web server would receive the request and then pass it on to the PHP interpreter. By using special PHP functions, we are then able to make PHP run the shell script or command for us.