? [-f file] Determine whether it is a normal file and exist
? [-d file] Determine whether it is a directory and exist
? [-e file] Determine whether a file or directory exists
>? [-r file] Determine whether the file is readable
? [-w file] Determine whether the file is writable
? [-x file] Determine whether the file is executable
[[email protected] shell]# cat if4.sh
#!/bin/bash
f=”/tmp/chenxu”
if [-f $f ]
then
echo $f “exist”
else
touch /tmp/chenxu
fi
or
#!/bin/bash
[ -f /tmp /chenxu ]&&echo “file exist”
! -f means not exist
[[emailprotected] shell]# cat if6.sh
#!/bin/bash
f= “/tmp/chenxu”
if [! -f $f ]
then
echo $f “not exist”
else
echo haha
fi
? if [-z “$a”] This means what happens when the value of variable a is empty
? if [-n “$a”] means when the value of variable a is not empty
? if grep -q ‘123’ 1.txt; then means what will happen if there is a line of ‘123’ in 1.txt
? if [! -e file ]; then means what will happen if the file does not exist< br>? if (($a<1)); then… is equivalent to if [$a -lt 1 ]; then…
? [] cannot be used in <,>,==,!=,>=, <=Symbols like this
[[email protected] shell]# cat if7.sh
#!/bin/bash
f=
if [-z $f ]
then
echo “$f is null”
else
echo haha
fi
[[emailprotected] shell] # cat if7.sh
#!/bin/bash
n=’wc -l /tmp/lalala’
if [$n -gt 100 ]
then
echo “>100 “
else
echo haha
fi
#/bin/bash
if [! -f /tmp/lalal ]
then
echo “/tmp /lalal not exist.”
exit
fi
n=wc -l /tmp/lalal
if [-z “$n” ]
then< br>echo error
exit
elif [$n -gt 100 ]
then
echo haha
fi
If the variable is equal to the result of the command, use the one next to 1 Key to extend the command.
-n can judge files and variables
[[email protected] shell]# if [-n if9.sh ];then echo ok;fi
ok
[[email Protected] shell]# b=a;if [-n “$b” ]; then echo ok;fi
ok
[[emailprotected] shell]#
Interpret Does user1 exist?
grep -w’user1′ /etc/passwd
[[emailprotected] shell]# if grep -w’user1′ /etc/passwd;then echo “user1 exist”; else useradd user1 -d /home/user1;fi
[[emailprotected] shell]# if grep -w’user1′ /etc/passwd;then echo “user1 exist”;else useradd user1 -d /home/user1 ;fi
user1:x:1000:1001::/home/user1:/bin/bash
user1 exist
Note that the content of grep is returned here, why not return it? Add the q parameter
[[emailprotected] shell]# if grep -qw’user1′ /etc/passwd;then echo “user1 exist”;else useradd user1 -d /home/user1;fi< br>user1 exist
[[email protected] shell]#
? format case variable name in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
? In the case program, you can use | in the condition, which means or, for example,
2 |3)
command
;;
[[email protected] shell]# read -p “Please input a number: “n
Please input a number: 12345
[[email protected] shell]# echo $n
12345
#!/bin/bash
read -p “Please input a number: “n
if [-z” $n” ]
then
echo “Please input a number.”
exit 1
fi
n1=echo $n|sed's/[0-9]/ /g'
———–
if [-n “$n1” ]
then
echo “Please input a number.”
exit 1
fi
if [$n -lt 60] && [$n -ge 0 ]
then
tag=1
elif [$n -ge 60] && [$n -lt 80 ]
then
tag=2
elif [$n -ge 80] && [$n -lt 90 ]
then
tag=3
elif [$n -ge 90] && [$n -le 100 ]
then
tag=4
else
tag= 0
fi
case $tag in
1)
echo “not ok”
;;
2)
echo “ok”
; ;
3)
echo “ook”
;;
4)
echo “oook”
;;
*)
echo “The number range is 0 -100.”
;;
esac
echo $?
The data word followed by exit is the value of echo$?
[[ email protected] shell]# cat test.sh
#!/bin/bash
read -p “Please input a number: “n
if [-z “$n” ]
then< br>echo “Please input a number.”
exit 1
fi
[[email protected] shell]# sh test.sh
Please input a number:
Please input a number.
[[emailprotected] shell]# echo $?
1
[[emailprotected] shell]# sh test.sh
Please input a number: 10
[[emailprotected] shell]# echo $?
0
for loop
? Syntax: for variable name in condition; do …; done
? Case 1
#! /bin/bash
sum=0
for i in seq 1 100
do
sum=$[$sum+$i]
echo $i
done
echo $sum
[[email protected] shell]# cat for.sh sums up the shell, note that the result of the command after i, so use the key next to 1 to box the command
#!/bin/bash
sum=0
for i in seq 1 100
do
sum=$[$sum+$i ]
done
echo $sum
#!/bin/bash
cd /etc/
for a in ls /etc/
do
if [-d $a ]
then
ls -d $a
fi
done
[[email Protected] shell]# touch 1 2
[[emailprotected] shell]# touch 3\ 4.txt
[[emailprotected] shell]# ls
[[emailprotected] shell]# ls
1 1.sh 2 3 4.tx
for i in ls ./
;do echo $i;done
The carriage return and space are used as separators in for
p>
[[email protected] shell]# for i in ls ./
;do echo $i;done11.sh234.txt
p>