one,
1 Notes on using the if statement
a=1 #No spaces on both sides of the equal sign
b=1
if [ $a -eq $b ];then #if there must be a space after the brackets, it is best to leave a space on both sides of the brackets, otherwise an error may be reported
echo 11111111110 $a #String variable connection processing
be
2. In square brackets, numerical judgment
[ INT1 -eq INT2 ] INT1 and INT2 are equal and return true, = [ INT1 -ne INT2 ] INT1 and INT2 are not equal and return true, <> [ INT1 -gt INT2 ] Returns true if INT1 is greater than INT2, > [ INT1 -ge INT2 ] INT1 greater than or equal to INT2 returns true, >= [ INT1 -lt INT2 ] Returns true if INT1 is less than INT2, < [ INT1 -le INT2 ] INT1 less than or equal to INT2 returns true, <=
Three, string judgment
[ -z STRING ] Returns true if the length of STRING is zero, i.e. null is true [ -n STRING ] Returns true if the length of STRING is non-zero, i.e. not empty is true [ STRING1 ] Returns true if the string is not empty, similar to -n [ STRING1 == STRING2 ] Returns true if the two strings are the same [ STRING1 != STRING2 ] Returns true if the strings are not identical [ STRING1 < STRING2 ] Returns true if "STRING1" is lexicographically sorted before "STRING2". [ STRING1 > STRING2 ] Returns true if "STRING1" is lexicographically sorted after "STRING2".
4. Logical judgment
[ ! EXPR ] Logical NOT, true if EXPR is false. [ EXPR1 -a EXPR2 ] Logical AND, returns true if both EXPR1 and EXPR2 are true. [ EXPR1 -o EXPR2 ] Logical OR, returns true if EXPR1 or EXPR2 is true. [ ] || [ ] Use OR to combine two conditions [ ] && [ ] Use AND to combine two conditions
Five, file/directory judgment:
E.g: b='/opt/elasticsearch-jdbc-2.3.3.0/bin/statefile.json' if [ -a $b ];then echo 11111111110 be [ -a FILE ] True if FILE exists. [ -b FILE ] Returns true if FILE exists and is a block file. [ -c FILE ] Returns true if FILE exists and is a character file. [ -d FILE ] Returns true if FILE exists and is a directory. [ -e FILE ] Returns true if the specified file or directory exists. [ -f FILE ] Returns true if FILE exists and is a regular file. [ -g FILE ] Returns true if FILE exists and SGID is set. [ -h FILE ] Returns true if FILE exists and is a symbolic link file. (This option does not work on some older systems) [ -k FILE ] Returns true if FILE exists and the hazard bit has been set. [ -p FILE ] Returns true if FILE exists and is a command pipe. [ -r FILE ] Returns true if FILE exists and is readable. [ -s FILE ] Returns true if FILE exists and has a non-zero size. [ -u FILE ] Returns true if FILE exists and the SUID bit is set. [ -w FILE ] Returns true if FILE exists and is writable. (A directory must be executable in order for its contents to be accessed) [ -x FILE ] Returns true if FILE exists and is executable. [ -O FILE ] Returns true if FILE exists and is a valid user ID. [ -G FILE ] Returns true if FILE exists and the default group is the current group. (only check system default group) [ -L FILE ] Returns true if FILE exists and is a symbolic link. [ -N FILE ] Returns true if FILE exists and has been mod if ied since it was last read. [ -S FILE ] Returns true if FILE exists and is a socket. [ FILE1 -nt FILE2 ] Returns true if FILE1 is newer than FILE2, or if FILE1 exists but FILE2 does not. [ FILE1 -ot FILE2 ] Returns true if FILE1 is older than FILE2, or if FILE2 exists but FILE1 does not. [ FILE1 -ef FILE2 ] Returns true if FILE1 and FILE2 point to the same device and node number.
Check if a file exists:
#!/bin/bash _prefix="/usr/local/nginx" time=`date +%Y%m%d%H` if [ -s ${_prefix}/logs/access.log ]; then echo "${time} hive data" >> /tmp/test.log else echo "${time} no data" >> /tmp/test.log be
6. Judging abnormal exit
if [ ! -n "$doiido" ]; then echo "$doiido is empty" exit 0 be example: 1 variables are equal: if [ "$var1" = "$var2" ]; then echo '$var1 eq $var2' else echo '$var1 not eq $var2' be
2: Comparison of numerical values:
if [ "$num" -gt "150" ] echo "$num is biger than 150" be
3:a>b且a<c
(( a > b )) && (( a < c )) [[ $a > $b ]] && [[ $a < $c ]] [ $a -gt $b -a $a -lt $c ]
4 a>b或a<c
(( a > b )) || (( a < c )) [[ $a > $b ]] || [[ $a < $c ]] [ $a -gt $b -o $a -lt $c ]
5 Detect the user executing the script
if [ "$(whoami)" != 'root' ]; then echo "You have no permission to run $0 as non-root user." exit 1; be
6 Regular matches
doiido="hero" if [[ "$doiido" == h* ]];then echo "hello,hero" be
7 View the current operating system type
#!/bin/sh SYSTEM=`uname -s` if [ $SYSTEM = "Linux" ] ; then echo "Linux" elif [ $SYSTEM = "FreeBSD" ] ; then echo "FreeBSD" elif [ $SYSTEM = "Solaris" ] ; then echo "Solaris" else echo "What?" be
8 if using read to pass parameters to judge
#!/bin/bash read -p "please input a score:" score echo -e "your score [$score] is judging by sys now" if [ "$score" -ge "0" ]&&[ "$score" -lt "60" ];then echo "sorry,you are lost!" elif [ "$score" -ge "60" ]&&[ "$score" -lt "85" ];then echo "just soso!" elif [ "$score" -le "100" ]&&[ "$score" -ge "85" ];then echo "good job!" else echo "input score is wrong , the range is [0-100]!" be
9 Determine whether the file exists
#!/bin/sh today=`date -d yesterday +%y%m%d` file="apache_$today.tar.gz" cd /home/chenshuo/shell if [ -f "$file" ];then echo "OK" else echo "error $file" >error.log mail -s "fail backup from test" [email protected] <error.log be
10 This script is executed by cron every Sunday. If the week number is even, he reminds you to clear the trash can
#!/bin/bash WEEKOFFSET=$[ $(date +"%V") % 2 ] if [ $WEEKOFFSET -eq "0" ]; then echo "Sunday evening, put out the garbage cans." | mail -s "Garbage cans out" your@your_domain.org be
10 comparisons include:
http://www.cnblogs.com/ginsonwang/p/5525340.html Method 1: Use grep to find strA="long string" strB="string" result=$(echo $strA | grep "${strB}") if [[ "$result" != "" ]] then echo "contains" else echo "does not contain" be First print the long string, then grep in the long string to find the string to be searched, and use the variable result to record the result If the result is not empty, strA contains strB. If the result is empty, the description does not contain. This method takes full advantage of grep's features and is the most concise.
Method 2: Using String Operators
strA="helloworld" strB="low" if [[ $strA =~ $strB ]] then echo "contains" else echo "does not contain" be Use the string operator =~ to directly determine whether strA contains strB. (Isn't this more concise than the first method!)
Method 3: Use wildcards
A="helloworld" B="low" if [[ $A == *$B* ]] then echo "contains" else echo "does not contain" be
Method 4: Use the case in statement
thisString="1 2 3 4 5" # source string searchString="1 2" # search string case $thisString in *"$searchString"*) echo Enemy Spot ;; *) echo nope ;; this
Method 5: Use Replacement
STRING_A=$1 STRING_B=$2 if [[ ${STRING_A/${STRING_B}//} == $STRING_A ]] then ## is not substring. echo N return 0 else ## is substring. echo Y return 1 be
12 Simple usage of shell ${}
file=/dir1/dir2/dir3/my.file.txt We can replace each with ${ } to get different values: ${file#*/}: remove the first / and its left string: dir1/dir2/dir3/my.file.txt ${file##*/}: remove the last / and the string to the left: my.file.txt ${file#*.}: remove the first . and the string to the left: file.txt ${file##*.}: remove the last . and the string to the left: txt ${file%/*}: remove the last / and the string to the right: /dir1/dir2/dir3 ${file%%/*}: remove the first / and the string to the right of it: (null) ${file%.*}: remove the last . and the string to the right: /dir1/dir2/dir3/my.file ${file%%.*}: remove the first . and the string to the right: /dir1/dir2/dir3/my The way to remember is: # is to remove the left side (on the disc, # is to the left of $) % is to remove the right (% is to the right of $ on the disc) A single symbol is the smallest match; two symbols is the largest match. ${file:0:5}: Extract 5 consecutive bytes starting from the 0th character: /dir1 ${file:5:5}: Extract 5 consecutive characters starting from the 5th character: /dir2 We can also replace strings in variable values: ${file/dir/path}: Extract the first dir to path: /path1/dir2/dir3/my.file.txt ${file//dir/path}: Extract all dir to path: /path1/path2/path3/my
13 Differences between various character variables
${ } is used for variable substitution. In general, $var is no different from ${var}. ${ } is clearly delimited, and there are many interception and replacement functions $( ) is basically the same as ` ` The former has a good visual effect, but some shells do not support it. The latter needs to be translated with backslashes when nesting, but the shell has good support. single and double quotes Single quotes tell the shell to ignore all special characters, while double quotes ignore most but not $, \, `. The content inside the backticks will be executed as a command