Linux script command Status condition test

Source: Internet
Author: User
Tags linux script command

Linux Scripts:

Scripts are typically text files, and running a script actually runs a bash process, which is responsible for reading an execution logic from the script file, and then the bash process is responsible for parsing and running the logic;

Startup script:

(1) # Bash/path/to/script_file

(2) A permission to execute,

#./path/to/script_file

[Email protected] ~]# VI date.sh [[email protected] ~]# sh date.sh #第一种执行方法Thu, Sep 12:43:37 +0800[[email Protec Ted] ~]# lltotal 4-rw-r--r--1 root root (SEP) 12:43 date.sh[[email protected] ~]# chmod +x date.sh #第二种执行方法 [[Email P Rotected] ~]# lltotal 4-rwxr-xr-x 1 root root (Sep) 12:43 date.sh[[email protected] ~]#./date.sh Thu, Sep 2015 12: 44:05 +0800[[email protected] ~]#

Common options for Bash scripts:

    • -N: Check for syntax errors in the script;

    • -X: Debug Execution script;

Command status results:

The bash process is used to track the status of the successful execution of a command:

0: Success

1-255: Failure

[[Email protected] ~]# vim date.sh [[email protected] ~]# sh -n  date.sh [[email protected] ~]# sh -x date.sh + date -sdate:  option requires an argument --  ' s ' try  ' date --help '  for  More information. [[Email protected] ~]# vi date.sh [[email protected] ~]# sh -n  date.sh [[email protected] ~]# sh -x date.sh + date -rthu,  17 sep 2015 14:36:06 +0800[[email protected] ~]# echo $?0[[email  protected] ~]# vi date.sh [[email protected] ~]# echo $?0[[email  protected] ~]# sh  date.sh date: option requires an  argument --  ' s ' try  ' Date --help '  for more information. [[EMAIL PROTECTED]&Nbsp;~]# echo $?1[[email protected] ~]# 


Custom Script Status results:

Exit n

[Email protected] bashtest]# vim t1.sh [[email protected] bashtest]# sh-x t1.sh + useradd test001useradd:user ' test001 ' Already exists+ Echo test001+ passwd--stdin test001changing password for user Test001.passwd:all authentication tokens Updated successfully.+ exit 9[[email protected] bashtest]# echo $?9[[email protected] bashtest]#

Note: Executing the exit command at any location in the script terminates the current shell process;

Condition Tests: Test

Defining the process implementation environment;

(1) According to the status result of the running command;

(2) test expression

Test command

[Space Command Space]

[[Space Command space]]

[Email protected] ~]# test-e/test[[email protected] ~]# echo $?1[[email protected] ~]# [-e/test][[email protected] ~ ]# echo $?1[[email protected] ~]#[[email protected] ~]# [[-e/var]][[email protected] ~]# echo $?0[[email protected] ~]#


Integer test: Implicit in the comparison of numerical size, so do not give reference to the variable;

$A-gt $B: Whether it is greater than, or "true", otherwise "false";

$A-ge $B: is greater than or equal to;

$A-lt $B: is less than;

$A-le $B: is less than or equal to;

$A-eq $B: is equal to;

$A-ne $B: is not equal;

[[email protected] ~]# a=9[[email protected] ~]# b=12[[email protected] ~]# [[$a-gt $b]][[email protected] ~]# echo $?1 [[Email protected] ~]# [[$a-ge $b]][[email protected] ~]# echo $?1[[email protected] ~]# [[$a-lt $b]][[email Protect ED] ~]# echo $?0[[email protected] ~]# [[$a-le $b]][[email protected] ~]# echo $?0[[email protected] ~]# [[$a-eq $b] ][[email protected] ~]# echo $?1[[email protected] ~]# [[$a-ne $b]][[email protected] ~]# echo $?0[[email protected] ~] #


String test:

"$A" > "$B": is greater than;

"$A" < "$B": is less than;

"$A" = = "$B": is equal to;

"$A"! = "$B": Not equal;

-Z "$A": Empty, Empty is true, otherwise false

-N "$A": not empty, not empty "true", Empty is "false"

[[email protected] ~]# a=glancesli   #103  108 97 110 99 101  115 108 105[[email protected] ~]# b=nancy    #110  97  110 99 121[[email protected] ~]# test  "$a"  =  "$b"  &&  echo ture| | echo falsefalse[[email protected] ~]# [  "$a"  ==  "$b"  ] &&  echo ture | |  echo falsefalse[[email protected] ~]# [  "$a"  !=  "$b"  ]  && echo ture | |  echo falseTure[[email protected] ~]# [ -z  "$a"  ] &&  echo ture | |  echo falsefalse[[email protected] ~]# [ -n  "$a"  ] &&  echo ture | |  echo falseTure[[email protected] ~]# test  "$a"  \>  "$b"  & & echo ture| | echo falsefalse[[email protected] ~]# test  "$a"  \<  "$b"  &&  echo ture| | echo falseture[[email protected] ~]# [[  "$a"  <  "$b"  ]] & & echo ture | |  echo falseTure[[email protected] ~]# [[  "$a"  >  "$b"  ]]  && echo ture | |  echo falsefalse[[email protected] ~]#

Note: string comparisons greater than less must be escaped, that is, with backslashes. the order of the string comparisons is in the order of the ASCII table, and the uppercase letters are smaller than the lowercase letters.

File testing: The existence of test files and properties;

-e $file: Exists or is true, otherwise "false";

-a $file: ibid;

-F $file: Whether the file exists and is a normal file;

-D $file: Whether the file exists and is a directory;

-H $file: exists and is a symbolic link file;

-L $file: Ibid.

-B $file: exists and is a block device file;

-C $file: Whether it exists and is a character device file;

-S $file: exists and is a socket file;

-P $file: exists and is a pipe file;


-R $file: Whether the current user has read access to the file;

-W $file: Whether the current user has write access to the file;

-X $file: Whether the current user has execute permission on the file;


-U $file: Whether the file has suid permissions;

-G $file: Whether the file has Sgid permissions;

-K $file: Whether the file has sticky permissions;


-O $file: Whether the current user is the owner of the specified file;

-G $file: Whether the current user is a group of specified files;

[[email protected] home]# mkdir abc[[email protected] home]# lltotal  12drwxr-xr-x 2 root    root    4096 sep 17  17:13 abcdrwxr-xr-x 2 root    root    4096 sep &NBSP;17&NBSP;17:01&NBSP;BASHTESTDRWX------ 2 user001 user001 4096 Sep 17  15:13 user001[[email protected] home]# test -e abc[[email protected]  home]# test -e abc && echo true | |  echo falsetrue[[email protected] home]# test -e bcd &&  echo true | |  echo falsefalse[[email protected] home]# test -a bcd &&  echo true | |  echo falsefalse[[email protected] home]# test -a abc &&  echo  true | |  echo falsetrue[[email protected] home]# test -f abc &&  echo true | |  echo falsefalse[[email protected] home]# test -d abc &&  echo true | |  echo falsetrue[[email protected] home]# ln -s abc/ cde[[email  protected] home]# lltotal 12drwxr-xr-x 2 root    root     4096 sep 17 17:13 abcdrwxr-xr-x 2 root    root     4096 Sep 17 17:01 bashtestlrwxrwxrwx 1 root     root       4 Sep 17 17:15 cde -> &NBSP;ABC/DRWX------ 2 user001 user001 4096 sep 17 15:13 user001[[ email protected] home]# test -h abc && echo true | |  echo falsefalse[[email protected] etc]# test -L system-release & & echo true | |  echo falsetrue[[email protected] etc]# test -h system-release & & echo true | |  echo falsetrue[[email protected] home]# test -b abc &&  echo true | |  echo falsefalse[[email protected] home]# test -c abc &&  echo true | |  echo falsefalse[[email protected] home]# test -r abc &&  echo true | |  echo falsetrue[[email protected] home]# vim te1.sh[[email protected]  home]# test -rxw abc && echo true | |  echo false-bash: test: -rxw: unary operator expectedfalse[[email p rotected] home]# test -w abc && echo true | |  echo falsetrue[[email protected] home]# test -x abc &&  echo true | |  echo falsetrue[[email protected] home]# test -u abc &&  echo true | |  echo falsefalse[[email protected] home]# test -O abc &&  echo true | |  echo falsetrue[[email protected] home]# test -G abc &&  echo true | |  echo falsetrue

Binocular operator:

$file 1-nt $file Whether 2:file1 is new to File2, File1 's last modification timestamp is later than file2;

$file 1-ot $file 2:file1 is older than File2, File1 's last modification timestamp is earlier than file2;

$file 1-ef $file whether the 2:file1 and File2 point to the same inode; test whether they are hard links to the same file;

[[email protected] bashtest]# lltotal 8-rwxr-xr-x 1 root root 140  sep 17 15:42 1.sh-rw-r--r-- 1 root root  74 sep 17  15:43 2.sh-rw-r--r-- 1 root root   0 sep 17 17:01  nancy[[email protected] bashtest]# test 2.sh \-ot 1.sh &&  echo true | |  echo falsefalse[[email protected] bashtest]# test 2.sh \-nt 1.sh  && echo true | |  echo falsetrue[[email protected] bashtest]# test 2.sh \-ef 1.sh  && echo true | |  echo falsefalse[[email protected] bashtest]# [[email protected] bashtest]#  ln -s nancy n1.sh[[email protected] bashtest]# ln -s nancy  n2.sh[[email protected]  bashtest]# test n2.sh \-ef n1.sh && echo true | |  echo falsetrue[[email protected] bashtest]#


Linux script command Status condition test

Related Article

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.