admin 管理员组文章数量: 1087139
2024年1月18日发(作者:roundup是什么意思)
一些自己练习时所写的简单shell脚本
(centos 6.3)【复制粘贴时请注意空格、引号、分号等格式】
1、 使用for循环、while循环、until循环计算100以内所有偶数的和
for循环:
#!/bin/sh
Sum=0
for i in `seq 0 2 100`
do
done
echo “SUM=$Sum”
exit 0
while循环:
#!/bin/sh
Sum=0
i=0
while [ “$i” –le 100 ]
do
done
echo “SUM=$Sum”
exit 0
until循环:
#!/bin/sh
Sum=0
i=0
until [ “$i” –gt 100 ]
do
done
echo “SUM=$Sum”
exit 0
2、 通过循环实现从1开始叠加,直到和的结果大于2000为止(使用break循环控制符)
#!/bin/sh
Sum=0
for (( i=1;;i++)
do
let “Sum+=i”
if [ “$Sum” –gt 2000 ]
then
echo “i=$i”
echo “SUM=$Sum”
break
let “Sum+=i”
let “i+=2”
let “Sum+=i”
let “i+=2”
let “Sum+=i”
#也可用for i in $(seq 0 2 100)
#或者 for (( i=0;i<=100;i+=2 ))
done
fi
exit 0
3、 找出100以内所有能被3整除的数,每行显示8个数,然后换行显示
#!/bin/sh
times=0 #循环次数
for i in $(seq 1 100)
do
let “temp=i%3”
let “times++”
if [ “$temp” –eq 0 ]
then
printf “$i”
let “a=times%8”
if [ “$a” –eq 0 ]
then
printf “n”
fi
fi
done
printf “n”
exit 0
4、 打印九九乘法表
#!/bin/sh
for (( i=1;i<=9;i++)
do
for (( j=1;j<=i;j++ ))
do
let “temp=i*j”
echo –n “$j*$i=$temp “
done
echo “”
done
exit 0
5、 显示颜色类型,并让用户选择(使用select结构)#!/bin/sh
echo “What is your favorite color?”
select color in “red” “blue” “green” “white”do
break
done
echo “You have selected $color.”
exit 0
6、 显示当前工作目录下的文件数和目录数
#!/bin/sh
Number()
{
let “dir_number=0”
let “file_number=0”
black”
“
done
ls
echo “”
for file in `ls`
do
if [ -d “$file” ]
then
then
fi
let “file_number+=1”
let “dir_number+=1”
elif [ -f “$file” ]
echo “The number of dirs is $dir_number.”
echo “The number of files is $file_number.”
}
Number
exit 0
7、 打印下面图案:
*
**
***
****
*****
******
*******
********
*********
**********
#!/bin/sh
for (( i=1;i<=10;i++ ))
do
done
exit 0
8、 输入一个整数,判断是否为完数(完数:一个数恰好等于它的因子之和,如6=1+2+3)
#!/bin/sh
sum=0
echo “Please input a number(>1):”
read number
for (( i=1;i<$number;i++ ))
do
done
let “temp=$number%i”
if [ “$temp” –eq 0 ]
then
fi
let “sum+=i”
for (( j=1;j<=i;j++ ))
do
done
printf “n”
echo –n “*”
if [ “$number” –eq “$sum” ]
then
echo “$number is Perfect number!”
else
fi
exit 0
9、 输入一个数字(1-12),然后显示其对应的月份的英文(使用case结构)
#!/bin/sh
echo “Please input a month(1-12):”
read month
case “$month” in
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
*)
esac
exit 0
10、 输入一个年份,判断是否为闰年,判断条件:
(1)
(2)
#!/bin/sh
echo “Please input a year:”
read year
#设置取余参数
let “n1=$year%4”
let “n2=$year%100”
let “n3=$year%400”
能被4整除,但不能被100整除的年份都是闰年;
能被100整除,但又能被400整除的年份是闰年。
echo “The month is not in (1-12)!”;;
echo “The month is December!”;;
echo “The month is November!”;;
echo “The month is October!”;;
echo “The month is September!”;;
echo “The month is Augest!”;;
echo “The month is July!”;;
echo “The month is June!”;;
echo “The month is May!”;;
echo “The month is April!”;;
echo “The month is March!”;;
echo “The month is February!”;;
echo “The month is January!”;;
echo “$number is not Perfect number!”
if [ ! “$n1” –eq 0 ]
then
then
then
else
fi
if [ “$leap” –eq 1 ]
then
else
fi
exit 0
暂时就这些吧,都是挺简单的例子。
我也是初学者,有误的地方欢迎指出,欢迎交流,共同学习!
邮箱:nowhere789@
echo “$year is not a leap year!”
echo “$year is a leap year!”
leap=1
leap=0
leap=1
elif [ ! “$n3” –eq 0 ]
leap=0
elif [ ! “$n2” –eq 0 ]
版权声明:本文标题:linux下简单shell脚本例子 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/b/1705527660a488630.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论