LINUX 102 | SCRIPTING

|^|

source:

http://www.informit.com/library/content.aspx?b=red_hat_linux7&seqNum=240

    • Creating and Executing a Shell Program

    • Variables

    • Positional Parameters

    • Built-In Variables

    • Special Characters

    • Comparison of Expressions

    • Iteration Statements

    • Conditional Statements

    • Miscellaneous Statements

    • Functions

Table: Shells with Red Hat Linux

1st Script:

#!/bin/sh #Name display program if [ $# -eq 0 ] then echo "Name not provided" else echo "Your name is "$1 fi

[rex@masterdns ScriptPractice]$ ls myenv mypgm [rex@masterdns ScriptPractice]$ sh mypgm Rex L Your name is Rex

Built-In Variables

Built-in variables are special variables that Linux provides to you that can be used to make decisions within a program. You cannot modify the values of these variables within the shell program.

Some of these variables are

To show these built-in variables in use, here is a sample program called mypgm2:

Script 2

#!/bin/bash

#my test program

echo "Number of parameters is "$#

echo "Program name is "$0

echo "Parameters as a single string is "$*

[rex@masterdns ScriptPractice]$ ls myenv mypgm mypgm2 [rex@masterdns ScriptPractice]$ ./mypgm2 Juan Dela Cruz Number of parameters is 3 Program name is ./mypgm2 Parameters as a single string is Juan Dela Cruz

Special Characters

Some characters have special meaning to Linux shells, so using them as part of variable names or strings causes your program to behave incorrectly. If a string contains such characters, you also have to use escape characters (backslashes) to indicate that the special characters should not be treated as special characters. Some of these characters are shown in Table.

Table 25.2. Special Shell Characters

A few characters deserve special note. They are the double quotes ("), the single quotes ('), the backslash (\), and the backtick (`), all discussed in the following sections. Also note that you can use input and output redirection from inside your shell scripts. Be sure to use output redirection with care when you're testing your shell programs, because you can easily overwrite files!

String Comparison

The following operators can be used to compare two string expressions:

Next are some examples comparing two strings, string1 and string2, in a shell program called compare1:

SCRIPT 3

#!/bin/sh string1="abc" string2="abd" if [ $string1 = $string2 ]; then echo "string1 equal to string2" else echo "string1 not equal to string2" fi if [ $string2 != string1 ]; then echo "string2 not equal to string1" else echo "string2 equal to string2" fi if [ $string1 ]; then echo "string1 is not empty" else echo "string1 is empty" fi if [ -n $string2 ]; then echo "string2 has a length greater than zero" else echo "string2 has length equal to zero" fi if [ -z $string1 ]; then echo "string1 has a length equal to zero" else echo "string1 has a length greater than zero" fi

[rex@masterdns ScriptPractice]$ ls myenv mypgm mypgm2 mystring [rex@masterdns ScriptPractice]$ . mystring string1 not equal to string2 string2 not equal to string1 string1 is not empty string2 has a length greater than zero string1 has a length greater than zero

Number Comparison

The following operators can be used to compare two numbers:

The following examples compare two numbers, number1 and number2, in a shell program called compare2:

eof