How to use the test command in Linux

  
                

Linux system sometimes needs to detect whether a certain condition is established, then you need to use the test command. There are many usages of test under Linux. Let's learn how to use the test command in Linux system with Xiaobian.

1. Determine the result of a command using test, which returns 0, or an integer. Return 0 for true, return an integer for error code

2. Get the result of the previous command using $?

3.

presence /home /www such a folder, so ls /home /www This command returns a result of 0. For example

My server above;

ie echo $? The return value is 0

My server above does not exist /home /kkk such a folder, so the return ls /home /kkk result of this command after command execution error code; < Br>

ie echo $? The result returned is 2. This 2 means that the file or folder does not exist.

4. Common file tester

-e indicates whether this file name exists

-f indicates whether this file name is a file

-d Indicates whether this file name is a folder

-r indicates whether this file name is readable

-w indicates whether this file is writable

-x indicates whether this file is readable execution

-S Indicates whether this file ask socket

4.1 on my server /home /www as a folder

so test -e /home The result of /www is 0, which is true. This file name exists

so the result of test -f /home/www is not 0, ie false, this file name is not a file

test -d /home /www result is 0, that is true, this file is named folder

4.2 on my server /home /kkk

the file name does not exist < Therefore, the result of test -e /home/kkk is not 0, that is, false, the file name does not exist

so the result of test -d /home/kkk is not 0, that is, false, this file name Do not ask for a folder.

The above is the introduction of the use of the Linux logic command test command. If you want to determine whether a condition is true, you will use this command. Have you learned it?

Copyright © Windows knowledge All Rights Reserved