Shell Learning Condition test (refer to Shell scripting tricks)

Source: Internet
Author: User
Tags bit set






1 about test testing, see the man documentation



The judgment of an expression



(EXPRESSION) #EXPRESSION is True



! EXPRESSION #EXPRESSION is False



Expression1-a EXPRESSION2 #both is true,-o means or





Whether the string is empty, equal



-N string #the length of string is nonzero,-n can removed



-Z String #the length of string is zero (nonexists,or null)



#when comes to string, we use =



STRING1 = STRING2 #the Strings is equal



STRING1! = STRING2 #the strings is not equal






Comparison of Numbers



#when comes to number,we with different EQ ne and so on



Integer1-eq INTEGER2 #INTEGER1 is equal to INTEGER2



-ge Greater or equal



-GT Greater than



-le-lt-ne I



compare files as linked files   



File1-ef FILE2 #FILE1 and FILE2 have the same device and inode numbers



eg


    [[email protected] shell]# test  txt  -ef txthardlink             
                [[email protected] shell]# echo $?
                0
                [[email protected] shell]# test  txt  -ef txtlink(soft)
                [[email protected] shell]# echo $?
                0




So this is whether the test is a file link (including soft and hard), you can try it yourself






Compare the modified time of a file



File1-nt FILE2#FILE1 is newer/older (modification date) than File2,timestamp



File1-ot FILE2 file has a modified time modify time (emphasis on content changes) change times (nature is related to property changes) Contact times Access Time (accessed)






File type



-B FILE #type: Block Special



-C FILE #type: Character Special



-D FILE # a directory



-e FILE #FILE whether exists exists



-S FILE #FILE exists and has a size greater than zero is empty



-F File #a regular file



-U FILE #set user-id SUID



-G file #FILE exists and is Set-group-id temporarily owning the permissions of the group to which the file belongs Sgid



-G FILE #FILE exists and is owned by the effective group ID true Group



-H FILE #a symbolic link (same as-l)



-K FILE #FILE exists and have its sticky bit set sticky bit other users can only add cannot delete



-L FILE #FILE exists and is a symbolic link (same as-h)



-O FILE #FILE exists and is owned by the effective user ID



-P FILE #a named pipe



-S FILE #FILE exists and is a socket



-R FILE #FILE exists and Read permission is granted



You can use-w, write,-X Execute






-T FD #file descriptor FD is opened on a terminal



A lot, but there are regular, not much used






2 The above test can also be used in a similar [-e $num] Structure





3 If/then condition test structure (also case,while, etc structure may be involved later, do not understand please Baidu)



①if [Condition]



Then



COMMAND



Fi



Or



if [Condition];then



COMMAND



Fi



②if [Condition];then



COMMAND1



Else



COMMAND2



Fi



③if [Condition];then



COMMAND1



elif [Condition];then



COMMAND2



Else



COMMAND3



Fi






Big eg.


#if1.sh
#!/bin/bash
#
read -p "guss who am i?"  HELLO
test -z $HELLO&&echo "wrong argv please reinput!"&&exit 1
if [ "$HELLO" = "Jack" ];then
echo "oh,you are jack!"
elif [ "$HELLO" = "Kitty" ];then
echo "oh,you are Kitty!"
elif [ "$HELLO" = "Mike" ];then
echo "oh,you are Mike!"
else
echo "sorry i don‘t remember."
echo "ok,my name is $HELLO"
fi




Usually if then can be changed to [[Conditon]]&& Condition ok| | Condition NOT OK



3 the difference between [[]] and []



Usually manifested in the following aspects



[] Use-a,-o to connect multiple logical expressions, while [[]] using &&,| | Concatenate multiple expressions



[]: Wildcards do not work, [[]] exhibition opening wildcards


[] You cannot use comparison operators, such as >,&LT, while [[]] can


4 (())



To test a numeric expression



The result is 0 returns 1, the result is the other return 0





[Email protected] ~]# ((1-1))



[[email protected] ~]# echo $?



1



[Email protected] ~]# ((+))



[[email protected] ~]# echo $?



0



5 Procedures for judging and testing conditions


# -N can indicate whether the file has been modified
#So there is a shell script that monitors whether the file has been modified
# watchfile.sh
#! / bin / bash

TT = 5 # 5 seconds
NFILE = $ 1 #the watched file

#origin length of the file
len = `wc -l $ NFILE | awk‘ {print $ 1} ’`

echo "$ NFILE has $ len lines"

while:
do
   if [-N $ NFILE]
   then
   echo "` date`: new entry in $ NFILE "
   newlen = `wc -l $ NFILE | awk‘ {print $ 1} ’`
   newlines = `expr $ newlen-$ len`
   tail-$ newlines $ NFILE
   len = $ newlen
   fi
   sleep $ TT
done




6 test procedures for regular expressions


# RE.sh
#! / bin / bash

for deb in / root / shell / *
do
pkgname = `basename $ deb`
if [[$ pkgname = ~. * \. deb]]; then
#Reminder if followed by [with space, there is a space in front of $, and a space after deb
   echo "FILE $ pkgname is a debian package"
else
   echo "File $ pkgname is not a debian package"
fi
done


Another addition to the captured


#How to capture the string in parentheses
#Save in BASH_REMATCH [] array
# RE1.sh
#! / bin / bash
# manage to know your name
read -p "please input your fullname:" NAME
if [[$ NAME = ~ (. *) [[: space:]] (. *)]]; then
echo "Your last name $ {BASH_REMATCH [2]}"
echo "You're called $ {BASH_REMATCH [1]}"
echo "full name $ {BASH_REMATCH [0]}"
fi
Results of the
[[email protected] shell] # ./RE1.sh
please input your fullname: liancao liu
Your surname liu
Your name is liancao
Full name liancao liu


71 scripts to guess numbers


#A guess the number of sh
# numguess.sh
#! / bin / bash
#you may want to guess between 0-50
WORDS = 51
GUESS = -1
TIME = 0
BELOW = 0
TOP = `expr $ WORDS-1`
let ANSWER = ($ RANDOM% $ WORDS)
let ANSWER + =-1

while ["$ GUESS" -ne "$ ANSWER"]
do
echo "the number is between $ BELOW and $ TOP"
         read -p "your guess:" GUESS
let TIME + = 1
if ["$ GUESS" -lt "$ ANSWER"]; then
         echo "please input larger"
BELOW = `expr $ GUESS + 1`
elif ["$ GUESS" -gt "$ ANSWER"]; then
echo "please input smaller"
TOP = `expr $ GUESS-1`
else
         echo "right! you guessed $ TIME times"
         fi
done 




This article is from the "Learning Path of Kai Learning" blog, please make sure to keep this source http://qixue.blog.51cto.com/7213178/1654508



Shell Learning Condition test (refer to Shell scripting tricks)


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.