shell 编程
>
>
>![image-20220331084804558](C:\Users\Windows\AppData\Roaming\Typora\typora-user-images\image-20220331084804558.png)
>
>
>
>![image-20220331085011703](C:\Users\Windows\AppData\Roaming\Typora\typora-user-images\image-20220331085011703.png)
>
> [root@pc1 ~]# a=1
> [root@pc1 ~]# [ $a -gt 2 ]&&echo 1
> [root@pc1 ~]#
> [root@pc1 ~]# [ $a -gt 2 ]&&echo 1||echo 0
> 0
> [root@pc1 ~]# [ $a -gt 2 ] && echo 1 || echo 0
> 0
> [root@pc1 ~]# b=5
> [root@pc1 ~]# [ $a -eq $b ] && echo 1 || echo 0
> 0
> [root@pc1 ~]# [ $a -lt $b ] && echo 1 || echo 0
> 1
>
>
>
> ![image-20220331090446830](C:\Users\Windows\AppData\Roaming\Typora\typora-user-images\image-20220331090446830.png)
>
>
>
> ![image-20220331092720451](C:\Users\Windows\AppData\Roaming\Typora\typora-user-images\image-20220331092720451.png)
>
>
>
>
>
>
>
>
>
>![image-20220331093110094](C:\Users\Windows\AppData\Roaming\Typora\typora-user-images\image-20220331093110094.png)
>
>
>
>
>
>
read 赋值给变量
> ![image-20220331101809247](C:\Users\Windows\AppData\Roaming\Typora\typora-user-images\image-20220331101809247.png)
12.文件判断
>
>作用:判断是否存在,是否是目录,是否可写??
>[ -f /etc/hosts ] ##存在且为普通文件
>[ -d /etc ] ##存在且为目录
>[ -e /etc ] ##存在/etc这个东西
>[ -r /etc/hosts ] ##存在且可读
>[ -w /etc/hosts ] ##存在且可写
>
>两种语法:
>test -f /etc/hosts 等价[ -f /etc/hosts ]
>
>[root@pc1 ~]# cat /etc/hosts
>127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
>::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
>[root@pc1 ~]# [ -f /etc/hosts ] && echo 1 || echo 0
>1
>[root@pc1 ~]# [ -f /etc/host ] && echo 1 || echo 0
>0
>
>[root@pc1 ~]# [ -f ~/oldboy.log ] && rm -f ~/oldboy.log
>
>[root@pc1 ~]# [ -d /etc ] && echo 1 || echo 0
>1
>[root@pc1 ~]# [ -d /etc1 ] && echo 1 || echo 0
>0
>查帮助man test
>
>6)配置shell编辑环境
>shell运行环境:
> [root@pc1 ~]# rpm -qa bash
> bash-4.2.46-34.el7.x86_64
>shell编辑环境:编辑程序环境
> 使用vim编辑
>
>vim配置:
>vimrc-老男孩Shell编程实战书籍文件20170126 传到Linux里改名为.vimrc
>存放位置:~/.vimrc
>
>
>7)接收用户输入read
>[root@pc1 ~]# mkdir /server/scripts/ -p
>[root@pc1 ~]# cd /server/scripts
>
>[root@pc1 scripts]# tail -6 read01.sh
>read -p "请输入用户名:" user
>read -p "请输入用户密码:" pass
>
>#输出用户输入的内容
>echo "你的用户名是$user,密码是$pass"
>##定义的时候变量前不加$,使用的时候才加$。
>
>[root@pc1 scripts]# sh read01.sh
>请输入用户名:oldboy
>请输入用户密码:123
>你的用户名是oldboy,密码是123
课堂作业
作业:成绩查询
cat > 1.sh <<EOF
!/bin/sh
echo "成绩查询"
read -p "请输入你要查询的成绩" a
if [ $a -ge 85 ] && [ $a -le 100 ]
then
echo "优秀"
elif [ $a -ge 60 ] && [ $a -lt 85 ]
then
echo "及格"
elif [ $a -lt 60 ]
then
echo "不及格"
else
echo "输入不合法"
fi
EOF
作业:成绩查询 (支持小数版):
cat >1.2.sh<<EOF
!/bin/sh
echo "成绩查询"
read -p "请输入你要查询的成绩:" a
if [ echo "$a >= 85 && $a <= 100" | bc
-ge 1 ]
then
echo "优秀"
elif [ echo "$a >= 60 && $a < 85" | bc
-ge 1 ]
then
echo "及格"
elif [ echo "$a < 0 " | bc
-lt 1 ]
then
echo "输入不合法"
elif [ echo "$a < 60 && $a >= 0" | bc
-ge 1 ]
then
echo "不及格"
else
echo "输入不合法"
fi
EOF
作业:打印菜单,根据菜单操作
cat >2.sh<<EOF
!/bin/sh
cat <<EOF
1.安装nginx
2.安装mariadb
3.退出菜单
EOF
read -p "选择:" a
if [ $a == 1 ] || [ $a == "安装nginx" ]
then
echo "安装nginx"
# yum install -y nginx
elif [ $a == 2 ] || [ $a == "安装mariadb" ]
then
echo "安装mariadb"
# yum install -y mariadb
elif [ $a == 3 ] || [ $a == "退出菜单" ]
then
echo "退出菜单"
exit
else
echo "请做出正确的选择"
exit
fi
EOF
作业:打印菜单,根据菜单操作(循环版)
cat >2.1.sh <<EOF
while true
do
cat <<'EOF'
1.安装nginx
2.安装mariadb
3.退出菜单
EOF
read -p "选择:" a
if [ $a == 1 ] || [ $a == "安装nginx" ]
then
echo "安装nginx"
yum install -y nginx
elif [ $a == 2 ] || [ $a == "安装mariadb" ]
then
echo "安装mariadb"
yum install -y mariadb
elif [ $a == 3 ] || [ $a == "退出菜单" ]
then
echo "正在退出菜单..."
sleep 1
exit
else
echo "请做出正确的选择"
fi
done
EOF