Use the test command to increase your scripting level

Source: Internet
Author: User
Tags aliases echo command readable

The basis of each conditional statement is to determine what is true and what is false. Knowing how it works will determine whether you're writing a quality-generic script or a script that you'll be proud of.

The ability of a Shell script is often underestimated, but in fact its ability to play is constrained by the ability of the scripting writer. The more you learn, the more you can compose a file like a juggler to automate tasks and simplify your management efforts.

Each operation in a shell script (except for the simplest command group) requires a check condition. All of the shell script "logic"--the "logic" in the broadest sense--can usually be grouped into the following three broad categories:

If {condition exists} then
... While {condition exists} does ...
Until {condition exists} do ...

Regardless of the subsequent operation, these logically based commands rely on determining whether a condition is real or not and deciding what to follow. The test command is a utility that makes it possible in each case to determine whether the condition to be judged exists. Therefore, a thorough understanding of this command is essential for writing successful shell scripts.

Working principle

The shortest definition of the test command might be to evaluate an expression, or return a value of 0 if the condition is true. If the expression is not true, it returns a value greater than 0-or it can be called a false value. The easiest way to check the status of the last command executed is to use $? Value. For demonstration purposes, this argument is used in all the examples in this article.

The test command expects to find a parameter on the command line that is considered empty when the shell does not assign a value to the variable. This means that when the script is being processed, test will report the error once the script is looking for a parameter that does not exist.

When you try to protect the script, you can solve the problem by including all the arguments in double quotes. The shell then expands the variable, and if the variable has no value, it passes a null value to test. Another approach is to add an extra check in the script to determine whether the command-line arguments are set. If no command-line arguments are set, the script tells the user that the parameter is missing and then exits. We'll use some examples to illustrate all of this more specifically.

Test and [command

Although the test command is included in each version of Linux and UNIX, the command has a more commonly used alias-the left parenthesis: [. Test and its aliases can usually be found in/usr/bin or/bin (depending on the operating system version and vendor).

When you use the left parenthesis instead of test, you must always follow a space, criteria to evaluate, a space, and a closing bracket. The right bracket is not an alias for anything, but rather the end of the required evaluation parameter. The spaces on either side of the condition are required, which means that you want to call test to distinguish between character/pattern matching operations that also frequently use square brackets.

The syntax for test and [is as follows:

Test expression
[expression]

In both cases, test evaluates an expression and then returns True or false. If it is used in conjunction with an if, while, or until command, you can have extensive control over the flow of the program. However, you do not need to use the test command with any other structure, and you can run it directly from the command line to check the state of almost anything.

Because they are aliases to each other, using test or [requires an expression. An expression is typically a comparison of text, numbers, or file and directory properties, and can contain variables, constants, and operators. The operator can be a string operator, an integer operator, a file operator, or a Boolean operator-we'll describe each operator in turn in the following sections.

Test file operator

With these operators, you can perform different actions in your program based on the evaluation of the file type:

-B File True if the file is a block-specific file
-C file True if the file is a character special file
-D File True if the file is a directory
-E File True if the file exists
-F File True if the file is a normal file
-G file True if the SGID bit of the file is set
-G file True if the file exists and is owned by the group
-K File True if the sticky bit of the file is set
-O File True if the file exists and is owned by the user
-P File True if the file is a named pipe
-R File True if the file is readable
-S file True if the file is not of zero length
-S file True if the file is a socket special file
-T FD True if FD is an open file descriptor connected to the terminal (FD defaults to 1)
-U file True if the SUID bit of the file is set
-W File True if the file is writable
-X File True if the file is executable

The following example shows the operation of this simple operation:

$ ls-l Total
drwxr-xr-w 2 root      1024  Dec 5  05:05  LST	    -rw-rw-rw-1 Emmett Users	  27360 Feb 6  07:30  evan
-rwsrwsrwx 1 root      152   Feb 6  07:32  Hannah
drwxr-xr-x 2 Emmett	    users	  1024  Feb 6  07:31  Karen
-rw-------1 Emmett	    users	  152   Feb 6  07:29  Kristin
-rw-r--r--1 Emmett     users     152   Feb 6  07:29  Spencer
$

test-r Evan
$ echo $?
0

$ test-r Walter
$ echo $?
1
$

Because the first estimate is true-the file exists and is readable-the return value is true, or 0. Because the second evaluated file does not exist, the value is false and the return value is Non-zero. It is important to specify a value of zero or Non-zero, because it will not always return 1 (although this is the normally returned value) and may return a value other than 0.

As mentioned at the beginning, in addition to using test, you can enclose the command in square brackets [] to give the same command to the shell-as shown below:

$ [w Evan]
$ echo $?
0
$ [x Evan]
$ echo $?
1
$

Again, the first expression is true, and the second expression is false-as indicated by the return value. You can also compare two files to each other by using the following command:

File1-ef File2 Test to determine if two files are connected to the same device and have the same inode number
File1-nt File2 Test to determine if the first file is newer than the second file (determined by the modification date)
File1-ot File2 Test to determine if the first file is older than the second file

The following example shows the results of comparing files using these operators:

$ [evan-nt Spencer]
$ echo $?
0
$ [karen-ot Spencer]
$ echo $?
1
$

The file named Evan is newer than the file named Spencer, and is therefore evaluated as true. Similarly, a file named Karen is newer than a file named Spencer, so the evaluation is false.

string comparison operators

As the title shows, this set of functions compares the values of strings. You can check if they exist, are the same, or are different.

String Test to determine if the string is not empty
-N String Test to determine if the string is not empty; string must be recognized by test
-Z String Test to determine whether the string is empty; string must be recognized by test
string1 = string2 Test to determine if string1 is the same as string2
String1!= string2 Test to determine if string1 is different from string2

One of the most useful tests for any variable is to determine whether its value is not empty, and simply put it on the test command line to perform such tests, as shown in the following example:

$ test "$variable"

It is strongly recommended that you enclose the variable in double quotation marks for this test so that the shell recognizes the variable (even if the variable is empty). The basic string evaluation and-n tests performed by default are functionally identical, as shown in the following example:

#example1
if Test-n "$"   
then
echo "$"      
fi

Executing the code in the above example will give the following results based on whether or not:

$ example1 Friday
Friday
$
example1
$

If you change the code to the following form, the results will be the same:

#example2
if test "$"   
then
echo "$"      
fi

as follows:

$ example2 Friday
Friday
$
example2
$

All of these indicate that-n is not usually required, which represents the default action.

To view the various possibilities from a different perspective, you can replace-n with another option and check to see if the value is empty (relative to non-null). This can be implemented with the-Z option, which is:

#example3
if Test-z "$"               
then
echo "No values were specified"  
fi

Run as follows:

$ example3               
No values were specified 
$ example3 Friday
$ 

If you run the program without command-line arguments, and the expression evaluates to True, the text in the program block is executed. If there is a value on the command line, the script exits without performing any action. It is useful to place the evaluation action at the beginning of the script, which allows you to check the value of the variable before further processing that may produce an error.

The remaining string operators evaluate the exact match between two variables/strings or the difference between them (which you can also call equivalence and "not equivalent"). The first example tests the match:

$ env
Logname=emmett
pager=less
shell=/bin/bash
term=linux
$
["$LOGNAME" = "Emmett"  ]
$ echo $?
0
$
$ ["$LOGNAME" = "Kristin"  ]
$ echo $?
1
$

Alternatively, the estimate can be used as a script to decide whether to run the script:

#example4
If ["$LOGNAME" = "Emmett"]
then
echo "processing beginning"
else 
	echo "incorrect User "
fi

This method can be used to find any value, such as a terminal type or shell type, that must match before the script is allowed to run. Note that the = or!= operator has precedence over most of the other options that can be specified, and the requirement must be accompanied by an expression. Therefore, in addition to the option to compare strings, = or!= cannot be used with an option to check for the existence of something, such as a readable file, executable file, or directory.

Integer comparison Operators

As the string comparison operator verifies that strings are equal or different, the integer comparison operator performs the same function on numbers. If the value of the variable matches the expression test is true, or false if it does not match. The integer comparison operator does not handle strings (just as string operators do not handle numbers):

Int1-eq Int2 True if Int1 equals Int2
Int1-ge Int2 True if Int1 is greater than or equal to Int2
INT1-GT Int2 True if the int1 is greater than int2
Int1-le Int2 True if Int1 is less than or equal to Int2
Int1-lt Int2 True if the int1 is less than int2
Int1-ne Int2 True if Int1 is not equal to Int2

The following example shows a code snippet in which the value given on the command line must be equal to 7:

#example5
If [$1-eq 7]
then
echo "You ' ve entered the magic number."
else 
	echo "You ' ve entered the wrong number."
Fi

In operation:

$ example5 6 You
' ve entered the wrong number.
$
$ example5 7 you
' ve entered the magic number.
$

As with strings, the value of a comparison can be a value assigned to a variable outside of the script, rather than being always available on the command line. The following example shows one way to achieve this:

#example6
If [$1-gt $number]
then
Echo Sorry, but is too high. "
else 
	echo "would work."
Fi

$ set number=7
$ export Number
$ example6 8
Sorry, but 8 is too high.
$ example6 7
7 would work.
$

One of the best uses of an integer comparison operator is to evaluate the number of specified command-line variables and determine whether it meets the required criteria. For example, if a particular command can only run with three or fewer variables,

#example7-display variables, up to three
if ["$#"-GT 3] then echo "For you have given too many the variables
."
	Exit $#
fi

The sample script runs normally (and returns a value of 0) as long as you specify three or fewer variables. If more than three variables are specified, an error message is displayed, and the routine exits-and returns an exit code equal to the number of variables given on the command line.

Modifying this process can be used to determine whether the day is the last few days of the month before the report is allowed to run:

#example8-to If it is near the end of the month#
set ' Date '  * Use backward quotes
if ["$"-ge]
  
   then echo "It is close enough to" "the" the "the" "The
" "Else echo" "This",
cannot be 
	run until After the 21st of the month 
	exit $
fi

  

In this example, six variables are set (separated by spaces):

$ = Fri
$ = Feb
$ = 6
$ = 08:56:30
$ = EST
$ = 2004

These values can be used in scripts just as they are entered on the command line. Note that the exit command returns a value again-in this case, the returned value is the date that is obtained from the $ value. This technique can be very useful in troubleshooting-if you think the script should run without running, check out $? The value.

A similar idea might be to write a script that runs only in the third Wednesday of each month. The third week, 31, is scheduled to be between 15th and 21st of the month. With Cron, you can invoke the script to run at a specified time of the day between 15th and 21st, and then use the first line of the script to check whether the value (after the set date) is Thu. If it is Thu, then execute the remaining script and, if not, exit.

Another idea may be that only the script is allowed to be in over 6:00 p.m. (18:00), and all users will go home and run. Just write the script so that it exits at a value below 18 o'clock and get the time by using the following command (set to $)

Set ' Date +%h '

Boolean operators

Boolean operators work the same way in almost every language-including shell scripts. In nutshell, they check that multiple conditions are true or false, or take actions against false conditions rather than real conditions. The operators used with test are

! Expr True if the expression evaluates to False
Expr1-a EXPR2 True if EXPR1 and EXPR2 are evaluated as true
Expr1-o EXPR2 True if EXPR1 or EXPR2 evaluates to True

You can use the!= operator instead of = for string evaluation. This is one of the simplest Boolean operators, and the normal result of test is not.

The first of the remaining two operators is the-a (that is, and) operator. To make the test final, two expressions must be evaluated to be true. If any of the evaluations are false, the entire test is evaluated as false. For example

$ env
home=/
Logname=emmett
mail=/usr/mail/emmett
path=:/bin:/usr/bin:/usr/lbin
term=linux
Tz=est5:0edt
$
["$LOGNAME" = "Emmett"-a "$TERM" = "Linux"]
$ echo $?
0
$

["LOGNAME" = "Karen"-A "$TERM" = "Linux"]
$ echo $?
1
$

In the first assessment, two conditions were tested to be true (Emmett is logged on to a Linux terminal), so the entire evaluation was true. In the second assessment, the terminal checks correctly but the user is incorrect, so the entire evaluation is false.

In short, the and operator ensures that the code executes only if two conditions are met. Conversely, the OR (-O) operator is true whenever any one of the expressions is tested as true. Let's revise the previous example and put it in a script to illustrate this point:

#example9
If ["$LOGNAME" = "Emmett"-o "$TERM" = "Linux"]
then
echo "Ready to begin."
else 
	echo "Incorrect user and terminal." 
Fi

$ env
home=/
Logname=emmett
Mail=/usr/mail/emmett
path=:/bin:/usr/bin:/usr/lbin
Term=linux
Tz=est5:0edt
$ example9
Ready to begin.
$
$ logname=karen
$ example9
Ready to begin.
$

When the script runs for the first time, the evaluation determines whether the user is equal to Emmett. If the user is found to be equal to Emmett, the script moves to the Echo statement and skips over the rest of the checks. It never checks whether the terminal is equal to Linux because it only needs to find a statement that is true to make the whole operation true. When the script runs for the second time, it determines that the user is not Emmett, so it checks and discovers that the terminal is really Linux. Because a condition is true, the script now moves to the echo command. In order to elicit the second message, all two conditions must be false.

In an example where the previous time was determined to be the end of the month, a similar check can be performed to prevent users from attempting to run the script on weekends:

#example10-do The script run over the weekend#
set ' Date ' # use  backward quotes
if ["$" = "Sat"-O "$" = "Sun"]
then
echo "This cannot is run over the weekend." 

Fi

A few useful examples

Example 1: The simplest form of "logic" that appears in a script file (as shown in all the examples in this article) is an "if ... then" statement. A previous code snippet checks for a certain number of variables and then echoes them back. Let's say we make some minor changes to this, such as we want to echo the variable, and each time we subtract the leftmost variable to show a inverted triangle.

While this sounds simple, it's not true; This is how you want to do it when you're doing large-scale processing: Handle the first variable, transfer, and process the next variable ...

For demonstration purposes, you can compose important rows in a script in the following ways:

#example11-display declining variables, up to three
if ["$#"-GT 3] # Click if more than three variables are given
  then
echo "You are have given more than three variables." 

Exit
fi
echo $*
if Test-n "$"
then
shift
Echo $*
fi
if Test-n "$"
Then
shift
echo $*
fi

It will be executed in the following manner:

$ example11 One
one
$

$ example11 One two one
two
two
$

$ example11 One two three one
two Three
two three
three
$

$ example11 One two three four you
have given more than three variables.
  $

The reason for limiting the number to three variables for inspection purposes is to reduce the number of rows to be checked in the example. Everything was done in a step-by-step manner, though it was incredibly confusing; the user was warned by using more variables than the program could handle, and the script exited. If the number of variables is 3 or less, the core portion of the operation begins to execute.

echo the variable and execute the test to see if another variable exists. If another variable exists, perform a transfer, echo the variable, perform another test, and so on. A total of 16 valid rows were used and the program could handle no more than three variables-very confusing. If you eliminate the limit of the number of variables, the program can handle any number of variables. After some modification, the script is shortened (beautified) and can handle any number of variables:

#example12-display declining variables, any number while
["$#"-GT 0]
do
echo $*
shift
done
  $ example12 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 3 4 5 6 7 8 9 0 3 4 5 6 7 8 9 0 4 5 6 7 8
0
5 6 7 8 9 0 6 7 8 9 0 7 8 9 0 8 9 0 9 0-
0

It is now reduced to only 5 valid rows and eliminates the limitation of the first script three variables and is more efficient at run time.

Example 2: Whenever you perform a process-related operation within a script, the next action always checks the state of the previous action to confirm that it completed successfully. Can you check the $? The state and verify that it equals 0来 to achieve this. For example, if a data directory is very important to access,

#example13
temp=lst
cd $TEMP
if [$?-ne 0]
then
	echo "Data directory could not is found." 
	Exit
fi

Handling Errors

There are only two types of errors that often occur with the test command. The first is not using the correct evaluation type, such as comparing a string variable with an integer variable or comparing a filled string with a string without a fill. Careful evaluation of the variables you use will ultimately lead you to the root of the error and allow you to resolve these issues.

The second type of error consists of mistaking the square brackets for something other than an alias. There must be a space between the brackets and their contents, otherwise they will not be able to interpret the objects in them. For example

$ ["$LOGNAME"-GT 9]
test:] Missing
$

Note that the error message indicates that the test has a problem, even if an alias is used. These issues are easy to spot because error messages display these problems exactly, and then you can add the necessary spaces.

Conclusion

To build logic in a shell script, you must add a conditional statement. At the heart of each of these statements is an assessment of the condition to determine whether it exists-by using the test command to complete the evaluation. Knowing how it and its aliases ([) Works will enable you to compose shell scripts that can accomplish some complex operations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.