Shell编程简单入门

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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值