mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
171 lines
6.4 KiB
Markdown
171 lines
6.4 KiB
Markdown
[#]: subject: "Tips for using the Linux test command"
|
|
[#]: via: "https://opensource.com/article/22/10/test-command-linux"
|
|
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
|
|
[#]: collector: "lkxed"
|
|
[#]: translator: " "
|
|
[#]: reviewer: " "
|
|
[#]: publisher: " "
|
|
[#]: url: " "
|
|
|
|
Tips for using the Linux test command
|
|
======
|
|
|
|
The [ and test commands are vital conditional statements when scripting.
|
|
|
|
The `[` command, often called a "test," is a command from the GNU Core Utils package, and initiates a conditional statement in Bash. Its function is exactly the same as the `test` command. When you want to execute a command only when something is either true or false, use the `[` or the `test` command. However, there's a significant difference between `[` or `test` and `[[`, and there's a technical difference between those commands and your shell's versions of them.
|
|
|
|
### [ vs test commands in Linux
|
|
|
|
The `[` and the `test` commands, installed by the GNU Core Utils package, perform the same function using a slightly different syntax. (You might find it difficult to search for documentation using the single left-square bracket character, however, so many users find `test` easier to reference.) Bash and similar shells happen to also have the `[` and the `test` commands built-in, and the built-in versions supersede the ones installed in `/usr/bin`. In other words, when you use `[` or `test`, you're probably not executing `/usr/bin/[` or `/usr/bin/test`. Instead, you're invoking what's essentially a function of your Bash shell.
|
|
|
|
You might wonder why `[` or `test` exist in `/usr/bin` at all. Some shells, such as [tcsh][1], don't have `[` and `test` built-in, so if you want to use those commands in that shell, you must have them installed as separate binaries.
|
|
|
|
The bottom line is that as long as you don't get an error when you type a command starting with `[` or `test`, then you've got everything you need. It almost never matters whether your shell or your `bin` directory is providing the commands.
|
|
|
|
### Testing for a file
|
|
|
|
It's common to want to know whether a file exists, often so you can confidently proceed with some action, or so you can avoid "clobbering" it with a file of the same name. In an interactive shell session, you can just look to see whether the file exists but in a shell script, you need the computer to determine that for itself. The `-e` option tests whether a file exists, but its apparent response is the same either way.
|
|
|
|
```
|
|
$ touch example
|
|
$ test-e example
|
|
$ test-e notafile
|
|
$
|
|
```
|
|
|
|
The `[` and `test` commands are essentially switches. They emit a `true` or `false` response, but considers both of them as success. You can put this to use by pairing the commands with logical operators, such as `&&` and `||`. The `&&` operator is executed when a response is `true`:
|
|
|
|
```
|
|
$ touch example
|
|
$ test-e example &&echo"foo"
|
|
foo
|
|
$ test-e notafile &&echo"foo"
|
|
$
|
|
```
|
|
|
|
The `||` operator executes when a response is `false`:
|
|
|
|
```
|
|
$ touch example
|
|
$ test-e example ||echo"foo"
|
|
$ test-e notafile ||echo"foo"
|
|
foo
|
|
$
|
|
```
|
|
|
|
If you prefer, you can use square brackets instead of `test`. In all cases, the results are the same:
|
|
|
|
```
|
|
$ touch example
|
|
$ [-e example ] && echo "foo"
|
|
foo
|
|
$ [-e notafile ] && echo "foo"
|
|
$
|
|
```
|
|
|
|
### Testing for file types
|
|
|
|
Everything in Linux is a file, so when you can test for the existence of a directory with the `-e` option, the same way you test for a file. However, there are different kinds of files, and sometimes that matters. You can use `[` or `test` to detect a variety of different file types:
|
|
|
|
- `-f`: regular file (returns `false` for a directory)
|
|
- `-d`: directory
|
|
- `-b`: block (such as `/dev/sda1`)
|
|
- `-L` or `-h`: symlink
|
|
- `-S`: socket
|
|
|
|
There are more, but those tend to be the most common.
|
|
|
|
### Testing for file attributes
|
|
|
|
You can also look at metadata of a file:
|
|
|
|
- `-s`: a file with the size greater than zero
|
|
- `-N`: a file that's been modified since it was last read
|
|
|
|
You can test by ownership:
|
|
|
|
- `-O`: a file owned by the current primary user
|
|
- `-G`: a file owned by the current primary group
|
|
|
|
Or you can test by permissions (or file mode):
|
|
|
|
- `-r`: a file with read permission granted
|
|
- `-w`: a file with write permission granted
|
|
- `-x`: a file with execute permission granted
|
|
- `-k`: a file with the sticky bit set
|
|
|
|
### Combining tests
|
|
|
|
You don't always just have to test for a single attribute. The `-a` option ("and") allows you to string several tests together, with the requirement that all tests return as `true`:
|
|
|
|
```
|
|
$ touch zombie apocalypse now
|
|
$ test-e zombie -a-e apocalypse -a-e now &&echo"no thanks"
|
|
no thanks
|
|
```
|
|
|
|
If any expression fails, then the test returns `false`:
|
|
|
|
```
|
|
$ touch zombie apocalypse now
|
|
$ test-e plant -a-e apocalypse -a-e now &&echo"no thanks"
|
|
$
|
|
```
|
|
|
|
The `-o` option ("or") requires that one expression is true:
|
|
|
|
```
|
|
$ touch zombie apocalypse now
|
|
$ test-e zombie -o-e plant -o-e apocalypse &&echo"no thanks"
|
|
no thanks
|
|
```
|
|
|
|
### Integer tests
|
|
|
|
You can also test integers. That's not necessarily directly useful (you probably inherently know that 0 is less than 1, for instance) but it's invaluable when you're using variables in a script.
|
|
|
|
The operators are fairly intuitive once you understand the schema:
|
|
|
|
- `-eq`: equal to
|
|
- `-ne`: not equal
|
|
- `-ge`: greater than or equal to
|
|
- `-gt`: greater than
|
|
- `-le`: less than or equal to
|
|
- `-lt`: less than
|
|
|
|
Here's a simple example:
|
|
|
|
```
|
|
$ nil=0
|
|
$ foo=1
|
|
$ test$foo-eq$nil||echo"Those are not equal."
|
|
Those are not equal.
|
|
$ test$foo-eq1&&echo"Those are equal."
|
|
```
|
|
|
|
Of course, you can combine tests.
|
|
|
|
```
|
|
$ touch example
|
|
$ test$foo-ne$nil-a-e example -o-e notafile &&echo"yes"yes
|
|
```
|
|
|
|
### Testing testing
|
|
|
|
The `[` and `test` commands are vital conditional statements when scripting. These are easy and common ways to control the flow of your code. There are yet more tests available than what I've covered in this article, so whether you used Bash, tcsh, ksh, or some other shell entirely, take a look at the man page to get the full spectrum of what these commands offer.
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
via: https://opensource.com/article/22/10/test-command-linux
|
|
|
|
作者:[Seth Kenlon][a]
|
|
选题:[lkxed][b]
|
|
译者:[译者ID](https://github.com/译者ID)
|
|
校对:[校对者ID](https://github.com/校对者ID)
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
[a]: https://opensource.com/users/seth
|
|
[b]: https://github.com/lkxed
|
|
[1]: https://opensource.com/article/20/8/tcsh
|