dreamsys software

UNIX & Linux Shell Scripting Tutorial

Advanced Topics


Often times a programmer needs to be able to get a substring from a variable at a given position. In unix you can use the expr command to do this with the substr parameter. Let's say that we have the text string "5283username$$2384/" and we want to get the text "username". To do this we need to read from position 5 for a length of 8. The parameters for substr are the input string, the starting position, and the length. See the following example:



USER=`expr substr $INPUT 5 8`

echo "Sub: '$USER'"

Find in a string

Sometimes you need to find text in a string. Maybe you want to list files but print only the text appearing before the ".". So if the filename is asdf.txt, you would want to print only asdf. To do this, you will use expr index, and pass it the string followed by the text for which you are searching. Let's try an example:


# Get the files:
FILES=`ls -1`

for FILE in $FILES
	IDX=`expr index $FILE .`

	if [ "$IDX" == 0 ]; then
		IDX=`expr length $FILE`
		IDX=`expr $IDX - 1`

	SUB=`expr substr $FILE 1 $IDX`
	echo "Sub File: $SUB"

If the substring doesn't exist, 0 is returned. If 0 is returned, we want to make the IDX variable the length of the name so that we just display the whole filename. If a dot is found in the file, we want to subtract 1 from our $IDX variable because we don't want to display the dot.

To lower/upper case

If you want to transform a string to upper or lower case, you can do so with the unix tr command. Here's a simple example.


STR_UPPER=`echo $STR_ORIGINAL | tr a-z A-Z`
STR_LOWER=`echo $STR_ORIGINAL | tr A-Z a-z`

echo "Original: $STR_ORIGINAL"
echo "Upper   : $STR_UPPER"
echo "Lower   : $STR_LOWER"

Editing a file with sed

If you want to edit a file from within your script, you can use the unix sed command. It will take a regular expression and a filename and put the file manipulations to standard output. For instance, let's say that we have a file with two fields "username" and "home directory". All the home directories start with "/home", but what if the admin changes the location of the "/home" directory to "/usr/local/home". We can have sed automatically update our file. Here is the file, save it as testfile2.

user1 /home/user1
root /home/root
user2 /home/user2
user3 /home/user3

We want our regular expression to search for "/home" and replace it with "/usr/local/home", a search expression is in the following format: "s/find/replace/", where "find" is the string you are searching for and "replace" is what you want to replace it with. Since the / character is a special character, we will need to escape it with a backslash in our find and replace strings. Here is the command we will use to do the file edit:

$ sed "s/\/home/\/usr\/local\/home/" testfile2 > tmp; cp tmp testfile2 
$ cat testfile2 
user1 /usr/local/home/user1
root /usr/local/home/root
user2 /usr/local/home/user2
user3 /usr/local/home/user3

Notice that we redirect the output of sed to a file named tmp, we then on the same line copy the tmp file over the testfile2 file. We cannot specify testfile2 to be the output since it is also being read from by sed during the command. On the next line we cat the output and you can see the file modifications.

Automating another application

Sometimes we may want to automate another program or script. If the other script expects user input, we may want to write a script to automatically fill in that information. First let's create a simple program that accepts a user name and password:


#Grab user name:
echo "user: "
read USER

#Grab password:
echo "pass: "
read PWD

if [ "$USER" == "dreamsys" ] && [ "$PWD" == "soft" ]; then
	echo "Login Success!"
	echo "Login Failed!"

Save this file as up.sh. Now we need to create a script to automate this script. To do this, all we need to do is output the user name followed by the password to the command line, we will pass these as two parameters:


echo $USER
echo $PWD

Now to run this automation script, we simply need to pipe the output to the up.sh script. First we will try to run it with an invalid user and password, then we will try to run it with the correct user and password:

$ ./auto.sh testuser testpass | ./up.sh
Login Failed!
$ ./auto.sh dreamsys soft | ./up.sh
Login Success!

Prev (Search & Sort) | Next (TOC)

Blog Entries
Blob Entry 1
Blob Entry 2
Blob Entry 3
Blob Entry 4
Blob Entry 5
Blob Entry 6