前言:可能这些脚本看起来很简单,很多人都不屑于去写,但是一切复杂的东西都是由一个个简单的东西组合起来的。
shell的语法比较怪异,和一般的编程语言有很大的区别,真的要多动手去写才能学会的
1. 要求:
1. 设定变量FILE的值为/etc/passwd
2. 依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如: hello, root, your shell: /bin/bash
3. 统计一共有多少用户
#!/bin/bash
FILE="/etc/passwd"
context=`cat /etc/passwd`
for i in $context; do
echo "hello, `echo $i |cut -d: -f1`, your shell: `echo $i |cut -d: -f7`"
done
echo "A total of `cat /etc/passwd |wc -l` user"
2. 求100以内能被3整除的数相加的和
#!/bin/bash
sum=0
for i in {1..100}; do
mod=$[$i%3]
if [ $mod -eq 0 ]; then
sum=$[$sum+$i]
fi
done
echo "sum=$sum"
3. 随机生成10个数,并给这10个数排序
#!/bin/bash
#
declare -a a
for x in {0..9};do
a[$x]=$[$RANDOM]
done
for (( i=0; i<=9; i++));do
for (( j=i+1; j<=9; j++));do
if [[ ${a[i]} -gt ${a[j]} ]];then
t = $[${a[i]}]
a[i] = $[${a[j]}]
a[j] = $[$t]
fi
done
done
for y in {0..9};do
echo ${a[y]}
done
shell小脚本
最新推荐文章于 2022-08-02 18:07:02 发布