当前位置:网站首页>作业7.15 shell脚本
作业7.15 shell脚本
2022-07-25 09:20:00 【不知名大学生M】
1. 无符号整数,不考虑翻转后超出范围的情况,你需要将这个整数中每位上的数字进行反转。
输入123 输出321
输入45678 输出87654
代码
#!/bin/bash
read -p "输入一个无符号整数:" var1
len=${
#var1}
i=$((len-1))
var2=""
while [ $i -ne -1 ]
do
var2="$var2${var1:i:1}"
((i--))
done
echo $var2
运行结果

2. 从终端输入字符串,求出小写字母的个数,大写字母的个数,数字的个数。
代码
#!/bin/bash
read -p "输入一个字符串:" var
count1=0
count2=0
count3=0
export LC_ALL=C
for (( i=0;i<${
#var};i++ ))
do
case ${
var:i:1} in
[a-z])
((count1++))
;;
[A-Z])
((count2++))
;;
[1-9])
((count3++))
;;
*)
;;
esac
done
echo "小写字母的个数:$count1"
echo "大写字母的个数:$count2"
echo "数字的个数:$count3"
unset LC_ALL
运行结果

3. 要求输入年月日,输出这是今年的第几天 ---->选做
代码
#!/bin/bash
read -p "请输入年月日:" year month date
day=(31 28 31 30 31 30 31 31 30 31 30 31)
count=0
if [[ $((year%4)) -eq 0 && $((year%100)) -ne 0 ]] || [ $((year%400)) -eq 0 ]
then
day[1]=29
fi
for ((i=0;i<$((month-1));i++))
do
((count+=${
day[i]}))
done
((count+=date))
echo "$year年$month月$date日是今年的第$count天"
运行结果

边栏推荐
猜你喜欢
随机推荐
【代码源】每日一题 非递减01序列
How can technologists start their personal brand? Exclusive teaching of top five KOLs
Unable to start debugging on the web server, the web server failed to find the requested resource
¥1-3 SWUST oj 942: 逆置顺序表
什么是贫血模型和充血模型?
Interviewer: tell me the difference between redis and mongodb? [easy to understand]
The difference between abstract classes and interfaces (the most detailed)
多态和接口
那天帮妹纸装了个数据库。。。就又帮她整理了篇快捷键
通过robocopy对文件/夹进行复制
OverTheWire-Natas
idea 热部署
uni-app如何获取位置信息(经纬度)
yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
『每日一问』LockSupport怎么实现线程等待、唤醒
sqli-labs Basic Challenges Less11-22
Redis安装(Ubuntu)
抽象类和接口的区别(最详细)
TCP网络应用程序开发流程
¥1-2 例2.2 将两个集合的并集放到线性表中









