The echo command
echo is a simple command that enables you to write output from a shell program. echo writes its arguments to the standard output. You can use echo directly, as a regular command, or as a component of a shell script. echo is used in shell scripts to display prompts and to output information. It is also frequently used to examine the value of a shell parameters and variables.
Example:
$echo My name is Amit
My name is Amit
Echo Escape Sequences
\b Backspace
\c Print line without newline
\f Form feed
\n Newline
\r Return
\t Tab
\v Vertical Tab
\\ Backslash
\0n The octal ASCII code for any character.
The read command
The echo command enables you to prompt for user input to your shell script. The read command lets you insert the user input into your script interactively. read reads just one line from user input and assigns the line to one or more shell variables. You can also use the read command to assign several variables at once. When you use read with several variable names, the first field typed by the user is assigned to the first variable; the second field, to the second variable; and so on. Leftover fields are assigned to the last variable. The field separator for shell input is defined by the IFS (Internal Field Separator) variable, which has the default value of a blank space. If you wish to use a different character to separate fields, you can do so. Simply redefine the IFS shell variable; IFS=: will set the field separator to the colon character (:).
Example:
$echo “Enter the no”
read no
The && and || Operators
The UNIX system shell provides two other conditional operators in addition to if . . . then, if . . . then . . . else, if . . . then . . . elif, and case. These are the && (logical AND), and || (logical OR) operators. The command use is
command1 && command2
or
command1 || command2
For && (logical AND) the first command is executed; if it returns a true (zero) exit status, then (and only then) is the second command executed. && returns the exit value of the last command sequence executed. If either the first or second command fails, && returns false (nonzero); to return a true (zero) value from &&, both commands must return false (nonzero); to return a true zero (true).
For || (logical OR) the first command is executed; if it returns a false (nonzero) exit status, then (and only then) is the second command executed. || returns false if both commands return false; it returns true only if either the first or second command (but not both) returns true.
The While and Until Commands
While Command
The if commands let you test if something is true and execute a command sequence based on the result. for lets you repeat a command several times. The while and until commands combine these two capabilities; you can repeat a command sequence based on a logical test. The while and until commands provide a simple way to implement test-iterate-test control.
Syntax:
while commands1
do
commands2
done
When while is executed, it runs the commands1 list following while. If commands1 completes successfully (the return value of that command is true), the commands2 list is executed. The done statement indicates the end of the command list, and the program returns to the while. If the return value of the while command is false, while terminates. It returns an exit value of the last do command list that was executed.
Until Command
The until command is the complement of the while command, and its form and usage are similar. As the command name suggests, a sequence of commands are executed until some test condition is met.
Syntax:
until commands1
do
Commands2
done
The single difference between the while and until commands is in the nature of the logical test made at the top of the loop. The while command repeatedly executes its do command list as long as is true. The until command repeatedly executes its do command list as long as is false. While loops while the test is true; until loops until the test is true.
The break and Continue Commands
Normally when you set up a loop using the for, while, and until commands, execution of the commands enclosed in the loop continues until the logical condition of the loop is met. The shell provides two ways to alter the operation of commands in a loop.
break exits from the immediately enclosing loop. If you give break a numerical argument, the program breaks out of that number of loops. In a set of nested loops, break2 breaks out of the immediately enclosing loop and the next enclosing loop. The commands immediately following the loop are executed.
continue is the opposite of break. Control goes back to the top of the smallest enclosing loop. If an argument is given, for example, continue 2, control goes to the top of the nth (second) enclosing loop.
Positional Parameters
You provide arguments to a simple shell program by specifying them on the command line. When you execute a shell program, shell variables are automatically set to match the command line arguments given to your program. These variables are referred as positional parameters and allow your program to access and manipulate command line information. The parameter $# is the number of arguments passed to the script. The parameters $1,$2,$3,$4,$5 refer to the first, second, third, fourth, fifth, and so forth, arguments on the command line. The parameter $0 is the name of the shell program. The parameter $* refers to all of the command line arguments.
cmd argu1 argu2 argu3 argu4 . . . . argu9
$0 $1 $2 $3 $4 . . . . $9
To see the relationship between words entered on the command line and variables available to a shell program, create the following example shell program, called show_args:
$ cat > show_args
echo $0
echo $1
echo $2
echo $3
echo $4
echo $*
echo $#
which prints the name of the script, $0, its four arguments, $1 through $4, and then all of the arguments given on the command line, represented by $*, and total number of the arguments are displayed by $#. The output of this script is seven lines that correspond to the seven echo commands:
$ sh show_args My self Amit Chaudhary doing BCA.
Show_args
My
Self
Amit
Chaudhary
My self Amit Chaudhary doing BCA.
6
The first argument, $0, is the name of the command show_args; the second line is the value of $1, “My”; the third line is the value of $2, “self”; the $* prints whole argument and last $# prints total number of arguments, 6
The test Command
test evaluates an expression; if the expression is true, test returns a 0 (zero) exit status. If the expression is not true, test returns a nonzero status. (test will also return a nonzero status if no expression is given to it.) The if command receives the value returned by test and continues processing based on it.
test enables you to evaluate strings, integers, and the status of UNIX System files. For example, the number of arguments given to a shell program is kept in the string variable #. If a user inappropriately attempts to execute a program without arguments, test can be used in providing a diagnostic message.
Numeric test
n1 –eq n2 True if integers n1 and n2 are equal
n1 –ne n2 True if integers n1 and n2 are not equal
n1 –gt n2 True if integer n1 is greater than n2
n1 –ge n2 True if integer n1 is greater than or equal to n2
n1 –lt n2 True if integer n1 is less than or equal to n2
String test
-z string True if length of string is zero
-n string True if length of string is non-zero, that is, if string exists.
String1 = string2 True if string1 and string2 are identical
String1 != string2 True if string1 is not equal to string2
String1 True if string1 is not the null string
Example:
cat > f1
if test –n “$1”
then echo Hello
fi
Expl: Notice that in the test expression, $1 is enclosed in double quotation marks. This is necessary because if no argument was given, then $1 is unset, and therefore, there is no argument to the test command. The test would return in an error message. The double quotes around $1 signify that even if it is unset there will be an argument with a null value. Here in example, if argument is not passed than it will not print anything, but if quotes are removed from it than it will print Hello because null character will be used as character.
Using test to Evaluate File and Directory Status
Of special importance in the UNIX System environment is the fact that test can evaluate the status of files and directories. The following tests can be made:
File tests
-a file True if file exists
-r file True if file exists and is readable
-w file True if file exists and is writeable
-x file True if file exists and is executable
-f file True if file exists and is regular file
-d file True if file exists and is a directory
-c file True if file exists and is a character special file
-b file True if file exists and is block special file
-p file True if file exists and is a named pipe
-s file True if file exists and has a size greater than zero
The capability to make tests of file status is important for two reasons. First, user shell scripts often manipulates files; the capability to make tests of file status within a shell scripts makes it simple to do this. For example, say you want to display a file, but only if the file exists. Normally, if you try to display a file that does not exist or cannot be read, you will get an error message:
Example:
If test –r f1
then cat f1
fi
Expl: If file f1 exists than and than only it will display f1 file.
Other Test Operators
! Negation operator
-a Binary AND operator
-o Binary OR operator
There is an alternative way to specify the test operator: The “[“ and “]” (Square brackets) surrounding a comparison mean the same as test. The following are
test $# -eq 0 [ $# -eq 0 ]
test –z $1 [ -z $1 ]
test amit = $1 [ amit = $1 ]
test –n $3 [ -n $3 ]
The correct way to use brackets in place of test is to place the brackets around the comparison being made. The brackets must be separated by spaces from the text, as in [ $# -eq 0 ]. If you forget to include the spaces, as in [$#-eq0], the test will not work.
Compiled By: Chaudhary Amit V.
Comments
Post a Comment