Home >Operation and Maintenance >Linux Operation and Maintenance >Introduction and use of basic shell syntax
1. Variables
1. Naming rules for variables: start with a letter or underscore, followed by numbers, letters or underscores. It is best not to name the variable casually, but to be able to guess its meaning when you see the variable name.
2. Variable assignment: x=100
echo $x
Delete variable: unset x
3. Use braces ## to define the boundaries of variable names
#[root@bogon ~]# egon_salary=20000[root@bogon ~]# echo ${egon_salary}yuan
20000yuan
1
[root@bogon ~]# ((x+=1))
[root@bogon ~]# echo $x
11
[root@bogon ~]# x=10
[root@bogon ~]# ((x>=8))
[root@bogon ~]# echo $?
0
4. Calculator in the shell
[root@bogon ~]# echo ${res}%
33%
[root@bogon ~]# [[ 2 > 1 ]][root@bogon ~]# echo $?0 [root@bogon ~]# ((20>10))[root@bogon ~]# echo $?0 [root@bogon ~]# ((20<10))[root@bogon ~]# echo $?1String test
[root@bogon ~]# [ "abc" = "abc" ][root@bogon ~]# echo $?0 [root@bogon ~]# [[ "abc" = "abc" ]][root@bogon ~]# echo $?0 [root@bogon ~]# (("abc" = "abc"))[root@bogon ~]# echo $?1
[root@bogon ~]# [[ a = a && 1 < 2 ]][root@bogon ~]# echo $?0 [root@bogon ~]# [[ a = a && 1 < 2 ]][root@bogon ~]# echo $?0
[root@bogon ~]# (( a = a || 1 > 2 ))[root@bogon ~]# echo $?1[root@bogon ~]# [[ a = a || 1 > 2 ]][root@bogon ~]# echo $?0To compare pure numbers, use (( ))For comparisons other than pure numbers, use [[ ]]
3. Process control
1.if branch
1) Verify user account password:
input your name : zhangcan input password : 123login successful [root@bogon ~]# ./usertest.sh input your name : hha input password : hag user or password error
#! /bin/bashuser='zhangcan'password='123'read -p 'input your name : ' name read -p 'input password : ' codeif [ $name = $user -a $code = $password ];then echo 'login successful'elseecho 'user or password error'fi~2) Determine the grade
#!/bin/bash #根据用户输入的成绩,判断所属档次,并输出给用户read -p 'input your score : ' scoreif [ $score -ge 90 ];then echo '优秀'elif [ $score -ge 70 -a $score -lt 90 ];then echo '良好'elif [ $score -ge 60 -a $score -lt 70 ];then echo '及格'elif [ $score -lt 60 ];then echo '较差'fi2.while loopwhile (Condition) do Command doneExample: Determine what type of file the user inputs
#!/bin/bashwhile : do read -p 'input your file : ' fileif [ -z $file ];thencontinueelsebreakfi doneif [ -f $file ];then echo "$file is regular file"elif [ -b $file ];then echo "$file is block file"elif [ -d $file ];then echo "$file is directory file"elseecho "$file type unkonw"fi3.for loop for i in {1..10} #in does not have to be followed by a number, as long as it is a command that returns a resultdo
echo $i doneExample 1: Write a script to test the IPs that can be used in the subnet
#!/bin/bashfor i in {1..50} do ping -c1 192.168.16.$i &> /dev/null # -c1表示ping一次if [ $? -ne 0 ];then echo "192.168.16.$i successful"echo "192.168.16.$i" >> ~/ipavailable.txt fi done~Example 2: Count the number of each file type under /dev
#!/bin/bashdir='/dev'for i in $(ls $dir) doif [ -h $dir/$i ];then ((link+=1))elif [ -f $dir/$i ];then (( rfile+=1))elif [ -d $dir/$i ];then ((directory+=1))elif [ -b $dir/$i ];then (( block+=1 ))else(( typeunknow+=1)) fi done echo 'block' $block echo 'regular file' $rfile echo 'directory' $directory echo 'link' $link echo 'unknow' $typeunknow4. Nested loopsExample 1: Output a multiplication table
#!/bin/bashfor ((i=1;i<=9;i++)) dofor ((j=1;j<=i;j++)) do echo -n "$i*$j=$[$i*$j]"done echo doneExample 2: Verify the user's login account and password. After successful login, you can execute the command and exit when you enter quit
#!/bin/bashuser='zhangcan'password='123'tag=truewhile $tag do read -p 'input your name : ' name read -p 'input your password : ' codeif [[ $name = $user ]] && [[ $code = $password ]];then echo 'login successful'while $tag do read -p '>>: ' cmdif [[ $cmd = 'quit' ]];then tag=falseelse$cmd fi done fi done
The above is the detailed content of Introduction and use of basic shell syntax. For more information, please follow other related articles on the PHP Chinese website!