Shell条件练习题

目录

1.检查用户家目录中的 test.sh 文件是否存在,并且检查是否有执行权限

#!/bin/bash

[ -f ~/test.sh ]
if [ $? -eq 0 ]
then
echo "test.sh文件存在于家目录中" [ -x ~/test.sh ]
if [ $? -eq 0 ]
then
echo "test.sh文件具有执行权限"
else
echo "test.sh文件不具有执行权限"
fi else
echo "test.sh文件不存在于家目录中"
fi

2.提示用户输入100米赛跑的秒数,要求判断秒数大于0且小于等于10秒的进入选拔赛,大于10秒的都淘汰,如果输入其它字符则提示重新输入;进入选拔赛的成员再进一步判断男女性别,男生进男生组,女生进女生组,如果输入错误请提示错误

#!/bin/bash

read -p "请输入您100米赛跑的秒数:" second
if [ $second -gt 0 -a $second -le 10 ]
then
echo "恭喜进入选拔赛"
read -p "请输入您的性别(男/女):" sex if [ $sex = 男 ]
then
echo "您将进入男生组继续比赛"
elif [ $sex = 女 ]
then
echo "您将进入女生组继续比赛"
else
echo "输入错误,请重新输入:"
fi elif [ $second -gt 10 ]
then
echo "很遗憾,您被淘汰了"
else
echo "输入错误,请重新输入:"
fi

3.用case语句解压根据后缀名为 .tar.gz 或 .tar.bz2 的压缩包到 /opt 目录

#!/bin/bash

case $jieya in
*.tar.gz)
if [ -f $jieya ]
then
tar -zxvf $jieya -C /opt &> /dev/null
echo "$jieya 文件解压成功"
else
ehco "$jieya 压缩文件不存在"
fi
;; *.tar.bz2)
if [ -f $jieya ]
then
tar -jxvf $jieya -C /opt
echo “$jieya 文件解压成功”
else
echo "$jieya 压缩文件不存在"
fi
;; *)
echo "文件名输入错误"
;;
esac

4.提示用户输入内容,使用if 语句判断输入的内容是否为整数。

#!/bin/bash

read -p "请输入一个整数:" num

expr $num + 0 &> /dev/null

if [ $? -eq 0 ]
then
echo "您输入的$num 是一个整数"
else
echo "您输入的$num 不是一个整数"
fi

5.根据上一题再判断输入的内容是奇数还是偶数。

#!/bin/bash

read -p "请输入一个整数:" num

expr $num + 0 &> /dev/null

if [ $? -eq 0 ]
then
echo "您输入的$num 是一个整数"
else
echo "您输入的$num 不是一个整数"
fi expr ${num} % 2 &> /dev/null if [ $? -ne 0 ]
then
echo "您输入的$num 是一个偶数"
else
echo "您输入的$num 是一个奇数"
fi

6.用if 语句判断主机是否存活

#!/bin/bash

ping -c 3 -i 0.5 -w 2 $1 &> /dev/null

if [ $? -eq 0 ]
then
echo "$1 is online!"
else
echo "$1 is offline!"
fi

7.用case语句在/etc/init.d/目录中写一个firewalld脚本,并加入到系统服务管理中使能够使用 service firewalld start|stop|restart|status 来管理firewalld服务,要求如果命令选项不对,则提示 “用法: $0 {start|stop|status|restart}”。

#!/bin/bash

read -p "输入start|stop|status|restart:来管理防火墙:" firewalld
case $firewalld in
"start")
systemctl start firewalld
echo "防火墙已开启"
;; "stop")
systemctl stop firewalld
echo "防火墙已关闭"
;; "restart")
systemctl restart firewalld
echo "防火墙已重启"
;; "status")
systemctl status firewalld
echo "防火墙状态查看"
;; *)
echo "用法:$0{start|stop|status|restart}"
;; esac

Shell条件练习题的更多相关文章

  1. shell条件测试test

    shell条件测试可以通过以下两种方式: test   参数    测试内容 [ 参数  测试内容 ] 一.测试文件类型: test  -e   文件名          (测试文件是否存在) [ - ...

  2. shell 条件语句

    shell 条件语句 #!/bin/bash # 条件语句 NUM1=100 NUM2=200 if (($NUM1 > $NUM2));then echo "$NUM1 greate ...

  3. shell基础练习题

    shell 基础练习题 1.编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小 #!/bin ...

  4. shell条件测试语句实例-测试apache是否开启

    终于理解了shell条件测试语句"!="和"-n"的用法区别,于是有了如下的shell脚本,做为练习. 第一种方法:测试apache是否开启?字符串测试 #!/ ...

  5. Shell循环练习题

    Shell循环练习题 目录 Shell循环练习题 1.计算从1到100所有整数的和 2.提示用户输入一个小于100的整数,并计算从1到该数之间所有整数的和 3.求从1到100所有整数的偶数和.奇数和 ...

  6. [Shell]条件判断与流程控制:if, case, for, while, until

    ---------------------------------------------------------------------------------------------------- ...

  7. shell条件测试

    文件状态测试-b filename : 当filename 存在并且是块文件时返回真(返回0)-c filename : 当filename 存在并且是字符文件时返回真-d pathname : 当p ...

  8. Shell条件与测试

    分类参考 文件状态测试 -b filename 当filename 存在并且是块文件时返回真(返回0) -c filename 当filename 存在并且是字符文件时返回真 -d pathname ...

  9. shell条件判断if中的-a到-z的意思

    [ -a FILE ]  如果 FILE 存在则为真.  [ -b FILE ]  如果 FILE 存在且是一个块特殊文件则为真.  [ -c FILE ]  如果 FILE 存在且是一个字特殊文件则 ...

随机推荐

  1. getRequestDispatcher 中请求转发和请求包含的使用说明

    getRequestDispatcher() getRequestDispatcher() 包含两个方法,分别是请求转发和请求包含. RequestDispatcher rd = request.ge ...

  2. CSS基础 水平居中案例

    html结构 <body> <div class="father"> <div class="son"></div&g ...

  3. python appium自动化报“Encountered internal error running command: UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server

    运行app自动化代码时报"Encountered internal error running command: UnknownError: An unknown server-side e ...

  4. 初识python 之 爬虫:爬取中国天气网数据

    用到模块: 获取网页并解析:import requests,html5lib from bs4 import BeautifulSoup 使用pyecharts的Bar可视化工具"绘制图表& ...

  5. git 忽略本地文件的修改

    项目开发过程中,会遇到本地配置文件每个开发人员不同的情况,但如果遇到类似数据库配置这种最终需要加入 git 版本控制的配置,则会陷入两难境地.要么不跟踪,要么有人提交后其他人同步下来必须手动修改,非常 ...

  6. centos7.5上安装go1.13.4

    一.安装 1.下载go1.13.4.linux-amd64.tar.gz 先进入官网https://golang.google.cn/ 再 https://golang.google.cn/doc/i ...

  7. x86架构应用如何向Arm架构低成本迁移

    曾几何时,无论是在服务器还是个人电脑,CPU芯片领域一直是 Intel 独占鳌头,旗下的 X86_64 架构被广泛采用.然而王权没有永恒,近年来 Arm64 架构异军突起,服务器端有华为鲲鹏920高性 ...

  8. How to die?

    下次给一个 vector 搞 unique 之前,一定要记得给它排序!!1(少点自以为是) 对一个 set "同时"删除两个数时,一定要注意特判两个数是否重复/重叠. 有两个序列, ...

  9. Rancher Fleet使用教程

    官方文档: https://fleet.rancher.io/ https://github.com/rancher/fleet 博客截止日期为:20201204 当前官网版本为v0.3.0,但在实践 ...

  10. 【linux】Ubuntu20.04使用apt安装tomcat9

    Ubuntu20.04使用apt安装tomcat9 前言 系统环境:ubuntu20.04 java版本:openjdk version "11.0.11" 2021-04-20 ...