当前位置:网站首页 > 编程语言 > 正文

@test执行顺序(test指令执行结果)



1.任务描述

(1)在Linux操作系统中创建shell脚本实现条件控制。

(2)使用test命令和方括号进行条件控制。在shell脚本中检查文件是否存在、文件是否可写、两个整数是否相等、字符串是否为空,以及字符串是否相等。根据不同的条件结果,脚本会输出相应的消息。

2.任务实施

(1)在huawei用户家目录中,创建脚本文件test-condition01.sh。

[root@localhost ~]# vim test-condition01.sh [root@localhost ~]# cat test-condition01.sh #!/bin/bash ​ # 检查文件是否存在 mkdir ~/testfile if test -e ~/testfile; then        echo "File exists" else        echo "File does not exist" fi ​ # 检查文件是否可写 touch ~/www.opencloud.fun if test -w ~/www.opencloud.fun; then        echo "File is writable" else        echo "File is not writable" fi ​ # 检查两个整数是否相等 a=10 b=11 if test $a -eq $b; then        echo "a is equal to b" else        echo "a is not equal to b" fi ​ # 检查字符串是否为空 string=www.opencloud.fun if test -z "$string"; then        echo "String is empty" else        echo "String is not empty" fi ​ # 检查两个字符串是否相等 string1=www.opencloud.fun string2=opencloud.fun if test "$string1" = "$string2"; then        echo "Strings are equal" else        echo "Strings are not equal" fi ​ [root@localhost ~]# chmod +x test-condition01.sh [root@localhost ~]# https://blog.csdn.net/2403_/article/details/test-condition01.sh File exists File is writable a is not equal to b String is not empty Strings are not equal

(2)在huawei用户家目录中,创建脚本文件test-condition02.sh。

[root@localhost ~]# vim test-condition02.sh [root@localhost ~]# cat test-condition02.sh #!/bin/bash ​ # 检查文件是否存在 mkdir ~/testfile01 if [ -e ~/testfile01 ]; then        echo "File exists" else        echo "File does not exist" fi ​ # 检查文件是否可写 touch ~/www.opencloud.fun if [ -w ~/www.opencloud.fun ]; then        echo "File is writable" else        echo "File is not writable" fi ​ # 检查两个整数是否相等 a=10 b=10 if [ "$a" -eq "$b" ]; then        echo "a is equal to b" else        echo "a is not equal to b" fi ​ # 检查字符串是否为空 string= if [ -z "$string"]; then        echo "String is empty" else        echo "String is not empty" fi ​ # 检查两个字符串是否相等 string1=www.opencloud.fun string2=www.opencloud.fun if [ "$string1" = "$string2" ]; then        echo "String are equal" else        echo "String are not equal" fi [root@localhost ~]# chmod +x test-condition02.sh [root@localhost ~]# https://blog.csdn.net/2403_/article/details/test-condition02.sh File exists File is writable a is equal to b String is empty String are equal ​

1.任务描述

(1)在Linux操作系统中创建shell脚本if-user.sh,实现创建用户和设置用户密码功能。

(2)在Linux操作系统中创建shell脚本if-diskcheck.sh,用于检查根分区的使用率,并在使用率超过警告阈值时发送警告电子邮件至指定的电子邮箱。

(3)在Linux操作系统中创建shell脚本if-score.sh,用于查看分数,并根据分数确定成绩等级。

2.任务实施

(1)在huawei家目录中,创建脚本文件if-user.sh。

[root@localhost ~]# vim if-user.sh [root@localhost ~]# cat if-user.sh #!/bin/bash ​ echo "Please enter your username:" read username ​ # 检查用户是否存在 if ! id "$username" > /dev/null 2>&1; then        echo "User does not exist,please create the user"       read -p "Please enter a username for the new user:" username ​ # 提示用户输入新密码 if [ -z "$username" ]; then        echo "Username is empty,please enter a valid username."        exit 1 fi read -s -p "Please enter a password for the new user: " password echo ​ # 检查新密码是否为空 if [ -z "$password" ]; then        echo "Password is empty,please enter a valid password."        exit 1 fi ​ # 创建新用户并设置密码 sudo useradd "$username" echo "$username:$password" | sudo chpasswd echo "User $username created successfully" exit 0 else ​ # 提示用户输入现有用户的密码 read -s -p "Please enter your password: " password echo ​ # 检查密码是否为空 if [ -z "$password" ]; then        echo "Password is empty,please enter a valid password."        exit 1 fi ​ # 修改现有用户的密码 echo "$username:$password" | sudo chpasswd echo "Password changed successfully" fi [root@localhost ~]# chmod +x if-user.sh [root@localhost ~]# https://blog.csdn.net/2403_/article/details/if-user.sh Please enter your username: ls //此处输入已有用户验证效果 Please enter your password: //此处输入已有用户的密码(不可见,输入完回车即可) Password changed successfully [root@localhost ~]# https://blog.csdn.net/2403_/article/details/if-user.sh Please enter your username: ​ User does not exist,please create the user Please enter a username for the new user:lis //此处输入创建的新用户 Please enter a password for the new user: //此处输入创建新用户的密码 User lis created successfully [root@localhost ~]# ls /home/ lis  ls ​

(2)在huawei家目录中,创建脚本文件if-diskcheck.sh。

root@localhost ~]# vim if-diskcheck.sh [root@localhost ~]# cat if-diskcheck.sh #!/bin/bash # 获取根分区的使用率 root_usage=$(df -h / | awk 'NR==2{print $5}' | tr -d '%') # 设置警告阈值 warning=15 # 发送警告电子邮件至电子邮箱 email="" if [ "$root_usage" -ge "$warning" ]; then        echo "警告:根分区使用率已达到 $root_usage%。" | mail -s "磁盘空间警告" "$email"       echo "警告电子邮件已发送至 $email。" else echo "根分区使用率正常:$root_usage%。" fi [root@localhost ~]# chmod +x if-diskcheck.sh [root@localhost ~]# https://blog.csdn.net/2403_/article/details/if-diskcheck.sh https://blog.csdn.net/2403_/article/details/if-diskcheck.sh:行9: mail:未找到命令 警告电子邮件已发送至 。

(3)在huawei家目录中,创建脚本文件if-score.sh。

[root@localhost ~]# vim if-score.sh [root@localhost ~]# cat if-score.sh #!/bin/bash # 读取考试分数 read -p "请输入考试分数:" score # 判断成绩等级 if ((score >= 90)); then        grade="优秀" elif ((score >= 80)); then        grade="良好" elif ((score >= 60)); then        grade="及格" else        grade="不及格" fi # 输出成绩等级 echo "成绩等级:$grade" [root@localhost ~]# https://blog.csdn.net/2403_/article/details/if-score.sh 请输入考试分数:100 成绩等级:优秀

1.任务描述

(1)在Linux操作系统中创建shell脚本case-user.sh,用于检查磁盘、内存、CPU等的使用情况。

(2)在Linux操作系统中创建shell脚本case-service.sh,用于启动服务、停止服务及退出程序。

2.任务实施

(1)在huawei用户家目录中,创建脚本文件case-user.sh。

[root@localhost ~]# vim case-user.sh [root@localhost ~]# cat case-user.sh #!/bin/bash echo "请选择一个操作:" echo "1.查看磁盘使用情况" echo "2.查看内存使用情况" echo "3.查看CPU使用情况" echo "4.退出" read -p "请输入数字(1-4):" choice case "$choice" in        1)               df -h               ;;        2)               free -h               ;;        3)                top -bn1 | head -n 10               ;;        4)                echo "退出程序"                exit 0               ;;       *)                echo "无效输入"               ;; esac [root@localhost ~]# chmod +x case-user.sh [root@localhost ~]# https://blog.csdn.net/2403_/article/details/case-user.sh 请选择一个操作: 1.查看磁盘使用情况 2.查看内存使用情况 3.查看CPU使用情况 4.退出 请输入数字(1-4):1 文件系统               容量 已用 可用 已用% 挂载点 devtmpfs               4.0M     0  4.0M    0% /dev tmpfs                 962M     0 962M    0% /dev/shm tmpfs                 385M  7.8M 377M    3% /run /dev/mapper/rhel-root   17G  4.2G   13G   26% / /dev/nvme0n1p2         960M 260M 701M   27% /boot /dev/nvme0n1p1         599M  7.0M 592M    2% /boot/efi tmpfs                 193M   92K 193M    1% /run/user/0 /dev/sr0               9.9G  9.9G     0  100% /run/media/root/RHEL-9-3-0-BaseOS-x86_64

(2)在huawei用户家目录中,创建脚本文件case-service.sh。

[root@localhost ~]# vim case-service.sh [root@localhost ~]# cat case-service.sh #!/bin/bash function start_service() {       read -p "请输入服务名称:" service_name        sudo systemctl start "$service_name"        echo "服务 $service_name 已启动。" } function stop_service() {       read -p "请输入服务名称:" service_name        sudo systemctl stop "$service_name"        echo "服务 $service_name 已停止。" } echo "请选择一个操作:" echo "1.启动服务" echo "2.停止服务" echo "3.退出" read -p "请输入数字(1-3):" choice case "$choice" in        1)               start_service               ;;        2)               stop_service               ;;        3)                echo "退出程序"                exit 0               ;;       *)                echo "无效输入"               ;; esac ​ [root@localhost ~]# chmod +x case-service.sh [root@localhost ~]# https://blog.csdn.net/2403_/article/details/case-service.sh 请选择一个操作: 1.启动服务 2.停止服务 3.退出 请输入数字(1-3):1 请输入服务名称:docker Failed to start docker.service: Unit docker.service not found. 服务 docker 已启动。
到此这篇@test执行顺序(test指令执行结果)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • 断开了连接(断开了连接屏幕的排线,逻辑板没电压)2025-08-03 16:00:09
  • sigmoid的输出(sigmoid的输出也可以是两个吧)2025-08-03 16:00:09
  • Autokey密码(autokeys)2025-08-03 16:00:09
  • 安装虚拟机的系统可以安装给电脑用吗(虚拟机可以安装到c盘吗)2025-08-03 16:00:09
  • lda主题模型分析文本(lda主题模型基本原理)2025-08-03 16:00:09
  • samba共享(samba共享设备密码)2025-08-03 16:00:09
  • 合并数组并排序(合并数组算法)2025-08-03 16:00:09
  • mhm是什么缩写(mhm mhm什么意思)2025-08-03 16:00:09
  • 电池mha是什么意思(电池上的ms是什么意思)2025-08-03 16:00:09
  • 在线检测nat类型(在线nat检测网站)2025-08-03 16:00:09
  • 全屏图片