1.打印
echo用于打印信息,将输入的信息保存到name变量,加入$符号来显示变量内容
使用sh hello.sh执行
#!/bin/bash
echo "请输入您的姓名: "
read name
echo "您好,$name"
2.赋参数
使用$1,$2来在执行指令时赋给参数
#!/bin/bash
echo "请输入您的姓名: "
name=$1
channel=$2
echo "您好,$name,欢迎来到$channel!"
使用指令 sh hello.sh tom 111来执行
3.猜数字
生成随机数
#!/bin/bash
echo "请输入您的姓名: "
name=$1
channel=$2
echo "您好,$name,欢迎来到$channel!"
number=$(shuf -i 1-10 -n 1)
echo $number
$(),其中()中间的内容为指令
4.if判断猜数字结果
#!/bin/bash
echo "请输入您的姓名: "
name=$1
channel=$2
echo "您好,$name,欢迎来到$channel!"
number=$(shuf -i 1-10 -n 1)
echo $number
echo "请输入一个1-10之间的数字"
read guess
if [ $number -eq $guess ]; then
echo "猜对了"
fi
5.y/n
#!/bin/bash
echo "请输入您的姓名: "
name=$1
channel=$2
echo "您好,$name,欢迎来到$channel!"
number=$(shuf -i 1-10 -n 1)
echo $number
while true
do
echo "请输入一个1-10之间的数字"
read guess
if [ $number -eq $guess ]; then
echo "猜对了,是否继续? (y/n) : "
read choice
if [ $choice = "y" ]; then
continue
else
break
fi
else
echo "猜错了"
fi
done