This is the 7th day of my participation in Gwen Challenge

Branching structure

The test command

The test command is used to judge conditions

  • No standard output is produced and the result must be judged by its return value

  • Integers, strings, and files can be judged

  • Expression TRUE returns 0 (TRUE)

  • FALSE 1 (FALSE)

test expression
# or
[ expression ]  There must be a space between # [] and expression
Copy the code
  • There must be a space between [] and expression

1. The integer

The operator The semantic
-lt Less than (<)
-le Less than or equal to (<=)
-gt Greater than (>)
-ge Greater than or equal to (>=)
-eq Equals (== =)
-ne Does not equal (! =)

demo

[root@localhost sh]# test 3 -lt 4
[root@localhost sh]# echo $? # return the result of the last judgment
0
[root@localhost sh]# [ 3 -gt 4 ]
[root@localhost sh]# echo $?
1
Copy the code

2. The string

The operator The semantic
= Is equal to the
! = Is not equal to
[root@localhost sh]# a=ff
[root@localhost sh]# c=ff
[root@localhost sh]# test $a=$c
[root@localhost sh]# echo $?
0
Copy the code

3. The file

The operator The semantic
-f True if the file exists and is a regular file (not a directory or device)
-s The file exists and the number of bytes is greater than 0
-r The file exists and is readable as true
-w The file exists and is writable to true
-x The file exists and is executable true
-d The directory exists and is true

4. Other

The operator The semantic
-a Logic and
-o Logic or
! Logic is not
\( \) The grouping parentheses must use \(and) and must have Spaces between them and the expression

If statement

If/THEN /fi are independent statements. If they are placed on a line, separate them with a semicolon (;)

if [ expression ];then
	Todo
elif [ expression ];then
    Todo
else
	Todo
fi  # end
Copy the code
Single branch
#! /bin/sh
read filename  # input file name
if [ -d $filename ];then
        echo "is dir"
fi
exit 0
Copy the code
Double branch
#! /bin/sh
read filename
if [ -d $filename ]
then echo $filename "is a directory"
else
        echo $filename "is't a DIRECTOR"
fi
exit 0
Copy the code
Many branches
#! /bin/sh
read filename  Enter a file name
if [ -d $filename ];then  Check whether the directory exists
        ls $filename   Output directory
elif [ -x $filename ]  Determine whether an executable file is available
then 	echo "is"
else
        echo "not is"
fi
exit 0
Copy the code

Enter a directory and output the files in that directory

The select statement

The SELECT statement can make a list of options similar to a directory, interactively selecting the data from the list and passing it into the select body for use

Select the variableinThe list ofdo... [break]
done
Copy the code
  • The select statement is essentially a loop statement. Without the break keyword, the program cannot exit the SELECT structure
  • List elements are separated by Spaces

The instance

Choose the course you want to study

#! /bin/sh
echo "what do you want tu study?"
select sub in "Java" "C++" "WEB"   # List elements separated by Spaces
do
  echo "you have selected $sub" # use the sub
  break # Out of the loop
done
exit 0
Copy the code

If you do not break, the loop will not break

A case statement

The case statement matches the contents of a variable with multiple options. If the match is successful, the statement corresponding to that condition is executed.

casevariableinOption 1)... ;;'option 2')... ;;"Option 3")... ;; ... *)...esac  # end
Copy the code

The matching conditions of the case statement can be multiple, multiple conditions of each match is connected with “|”

The instance

#! /bin/sh
echo -e "a:\c"
read a
echo -e "b\c"
read b
echo -e "select(+ - * /):\c"
read var
case $var in
        +) echo "a+b="`expr $a + $b`;;
        -) echo "a+b="`expr $a - $b`;;
        *) echo "a+b="`expr $a * $b`;;
        /) echo "a+b="`expr $a / $b`;;
        *) echo "error"
esac
exit 0
Copy the code
[root@localhost sh]# sh case.sh
a:2
b3
select(+ - * /):+
a+b=5
Copy the code

Multiple conditions – Determines whether the input value is true /false

#! /bin/sh
read var
case $var in
        yes|y|Y) echo "true";; # multiple conditions
        no|n|N) echo "false";;
        *) echo "input error";;
esac
exit 0
Copy the code
[root@localhost sh]# sh case_isYN.sh 
y
true
[root@localhost sh]# sh case_isYN.sh 
sdcsd
input error
Copy the code