True if the file exists and is readable, i.e., has its read bit set True if the file exists and is a named pipe (FIFO) True if the file exists and its "sticky'" bit is set True if the file exists and is a symbolic link True if the file exists and is set-group-id, SETGID True if the file exists and is a regular file, as opposed to a directory, a device special file, or a link, among others True if the file exists this is the same as -a above True if the file exists and is a directory True if the file exists and is a character special file such as a TTY device like /dev/TTY1 True if the file exists and is a block special file such as a hard drive like /dev/sda or /dev/sda1 True if the file exists it can be empty or have some content but, so long as it exists, this will be true I use them quite frequently in my scripts. Figure 1 lists more than 20 different operators that Bash can perform on files. It looks like: if ] then list File operatorsįile operators are a powerful set of logical operators within Bash. This format is a bit less compatible with different versions of Bash and other shells, such as ksh (the Korn shell). There is also a more recent syntax that offers a few advantages and that some sysadmins prefer. The single square braces,, are the traditional Bash symbols that are equivalent to the test command: if test arg1 operator arg2 then list The spaces in the comparison are required as shown. The functional syntax of these comparison operators is one or two arguments with an operator that are placed within square braces, followed by a list of program statements that are executed if the condition is true, and an optional list of program statements if the condition is false: if then list Each operator returns true (0) if the condition is met and false (1) if the condition is not met. There are three types of operators: file, numeric, and non-numeric operators. The most basic form of the if control structure tests for a condition and then executes a list of program statements if the condition is true. Logical operatorsīash has a large set of logical operators that can be used in conditional expressions. Logical operators are the basis for making decisions in a program and executing different sets of instructions based on those decisions. The third and final article in the series will explore the for, while, and until loops that enable repetitive operations. This second article looks into the types of file, string, numeric, and miscellaneous logical operators that provide execution-flow control logic and different types of shell expansions in Bash. The first article explored some simple command-line programming with Bash, including using variables and control operators. This three-part series (which is based on my three-volume Linux self-study course) explores using Bash as a programming language on the command-line interface (CLI). Bash is a powerful programming language, one perfectly designed for use on the command line and in shell scripts.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |