写一个有read和if判断的shell脚本
时间: 2024-05-12 21:17:39 浏览: 16
Sure, here's an example shell script that includes a read statement and an if statement:
```
#!/bin/bash
echo "Enter a number:"
read num
if (( num < 10 )); then
echo "The number is less than 10"
else
echo "The number is greater than or equal to 10"
fi
```
This script prompts the user to enter a number, reads in the input using the `read` statement, and then uses an `if` statement to check whether the number is less than 10. Depending on the result of the test, the script will output either "The number is less than 10" or "The number is greater than or equal to 10".
相关问题
shell脚本用read写一个判断题
可以通过以下脚本实现一个简单的判断题:
```
#!/bin/bash
question="1+1=2. (T/F)" # 题目
answer="T" # 正确答案
echo $question
read user_answer # 获取用户输入的答案
if [ "$user_answer" == "$answer" ]; then # 判断用户答案是否正确
echo "回答正确!"
else
echo "回答错误!"
fi
```
你可以根据需要修改问题、答案和提示信息。运行脚本后,它将提示用户回答问题并给出相应的反馈。
写一个shell脚本:判断指定网卡有无IP
好的,以下是判断指定网卡是否有IP地址的shell脚本:
```
#!/bin/bash
# 输入需要判断的网卡名称
echo "请输入需要判断的网卡名称:"
read nic_name
# 获取网卡IP地址
ip_addr=`ifconfig $nic_name | grep 'inet addr' | awk '{print $2}' | awk -F ':' '{print $2}'`
# 判断是否有IP地址
if [ -n "$ip_addr" ]; then
echo "$nic_name 网卡有IP地址:$ip_addr"
else
echo "$nic_name 网卡没有IP地址"
fi
```
使用方法:将以上脚本保存为一个文件,例如`check_ip.sh`,然后在终端中运行`bash check_ip.sh`,按照提示输入需要判断的网卡名称即可。