shell training day7 8.21

• [-f file] determines whether the file is normal, and there
• [-d file] determines whether it is a directory, and there
• [-e file] Analyzing the file or directory exists
• [-r file] whether the file readable
• [-w file] to determine whether the file is writable
• [-x file] to determine whether an executable file

[root@docker 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 指不存在
[root@docker 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 represents what happens when the variable value is a space-time
• if [-n "$ a" ] represents the value when the variable a is not empty
• if grep -q '123' 1.txt; represents if the then 1.txt row containing '123' happens
• if [-e file!]; then represents what happens when the file does not exist
• if (($ a <1 )); then ... is equivalent to the IF [$ -LT-a. 1]; then ...
• [] can not be used <,>, ==, =,> =, <= such a symbol!

[root@docker shell]# cat if7.sh
#!/bin/bash
f=
if [ -z $f ]
then
echo "$f is null"
else
echo haha
fi

[root@docker 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
echo error
exit
elif [ $n -gt 100 ]
then
echo haha
fi

If the variable is equal to the command results, use 1 next to the button to expand live command.

-n can determine the file and variables

[root@docker shell]# if [ -n if9.sh ];then echo ok;fi
ok
[root@docker shell]# b=a;if [ -n "$b" ]; then echo ok;fi
ok
[root@docker shell]#

User1 interpretation whether there
grep -w 'user1' / etc / passwd

[root@docker shell]# if grep -w 'user1' /etc/passwd;then echo "user1 exist";else useradd user1 -d /home/user1;fi
[root@docker 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 returns the contents of grep, and how not to return it? Plus q parameter

[root@docker shell]# if grep -qw 'user1' /etc/passwd;then echo "user1 exist";else useradd user1 -d /home/user1;fi
user1 exist
[root@docker shell]#

• Format case the variable name in
value1)
the Command
;;
value2)
the Command
;;
*)
commond
;;
esac
•, can be used in case the program in the condition |, representation or meaning, such as
2 | 3)
the Command
;;

[root@docker shell]# read -p "Please input a number: " n
Please input a number: 12345
[root@docker 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 $?

exit followed by the data word is the echo $? value

[root@docker shell]# cat test.sh
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
[root@docker shell]# sh test.sh
Please input a number:
Please input a number.
[root@docker shell]# echo $?
1
[root@docker shell]# sh test.sh
Please input a number: 10
[root@docker shell]# echo $?
0

for loop

• Syntax: for variable names 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

[root @ docker shell] # cat for.sh summing shell, i note behind that command results, so use a command that key box next to the
#! / 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

[root@docker shell]# touch 1 2
[root@docker shell]# touch 3\ 4.txt
[root@docker shell]# ls
[root@docker shell]# ls
1 1.sh 2 3 4.tx

for i in ls ./;do echo $i;done

and for which the transport spaces as the delimiter

[root@docker shell]# for i in ls ./;do echo $i;done
1
1.sh
2
3
4.txt

Guess you like

Origin blog.51cto.com/testdb/2432498