100个shell脚本。每天一个坚持下去。

前端之家收集整理的这篇文章主要介绍了100个shell脚本。每天一个坚持下去。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

20180306
一、case
#!/bin/bash
#测试case

echo "Hit a key,then hit return."
read keypress
#开始case
case "$keypress" in
[[:lower:]])
echo "lowercase letter.";;
[[:upper:]])
echo "uppercase letter.";;
[0-9])
echo "digit.";;
*)
echo "other letter";;
esac
##收到不同参数,输出不同结果。
20180307
二、
#!/bin/bash

result=expr 2 - 100
echo "$result"

result=$((5+5))
echo $result

result=$[5+4]
echo $result

###数值运算
20180308
三、
#!/bin/bash

for var in 1 2 3 4 5 6 7 8 9
do
echo "then is number is $var"
done

for var in {1..8}
do
echo "then is number is $var"
done
~
20180309
四、
#!/bin/bash

sum=0;

for i in {0..100..2}
do
let "sum+=i"
done
echo "then sum is $sum"
~
20180310
五、
#!/bin/bash

for file in $(ls)
do

echo $file
done

20180311
六、
#!/bin/bash
for ((i=1;i<5;i++))
do
echo $i
done

20180312
七、
#!/bin/bash

array=(Monday tuesday wednesday thuesday friday saturday sunday)
for day in ${array[*]}
do
echo $day
done
20180313
八、
#!/bin/bash

i=1
until [[ "$i" -gt 9 ]]
do
let "square=ii"
echo "$i
$i=$square"
let "i=i+1"
done

#!/bin/bash

i=1
until [ "$i" -eq 21 ]
do
useradd user$i
echo "password" | passwd --stdin user$i > /dev/null
let "i++"
done

20180313
九、
#!/bin/bash

i=1
until [ "$i" -eq 21 ]
do
userdel -r user$i
let "i++"
done

十、0314

#!/bin/bash

echo '是否是非空'
a='abc'
test $a
echo $?
echo '是否非空'
test -n $a
echo $?
echo '是否是空'
test -z $a
echo $?
echo '是否字符串对比'
b='abc'
test $a = $b
echo $?
echo '是否不等'
test $a != $b
echo $?

[ $a = $b ]
echo $?
[ $a != $b ]
echo $?

echo '是否等于'

[ 2 -eq 3 ]
echo $?

echo '是否不等于'

[ 3 -ne 2 ]
echo $?

echo '是否大于'
[ 10 -gt 9 ]
echo $?
echo '是否小于'

[ 8 -lt 9 ]
echo $?

echo '是否大于等于'

[ 10 -ge 10 ]
echo $?
echo '是否小于等于'
[ 10 -le 10 ]
echo $?

echo '文件是否存在'

[ -a 13.sh ]
echo $?
echo '是否是块文件'
[ -b /etc/rc.local ]
echo $?
echo '是否是字符文件'
[ -c 1.sh ]
echo $?
echo '是否存在且是目录'
[ -d /etc/cron.d ]
echo $?
echo '是否为空文件'
[ -s /etc/rc.d/rc.local ]
echo $?
echo '是否是连接文件'
[ -L 10.sh ]
echo $?
echo '是否设置SUID位'
[ -u 12.sh ]
echo $?

echo '判断逻辑非与或'

a=30
test "$a" -gt 20 -a "$a" -lt 100
echo $?

十四、0315 (if语句)
#!/bin/bash
echo "学生考试分数"

read num

if [ -z "$num" ]; then
echo "请输入一个分数:"
read num
else
if [ "$num" -lt 0 -o "$num" -gt 100 ]; then
echo "请输入0-100:"
read num
else
if [ "$num" -ge 90 ]; then
echo "成绩A"
else
if [ "$num" -ge 80 ]; then
echo "成绩B"
else
if [ "$num" -ge 70 ]; then
echo "成绩C"
else
if [ "$num" -ge 60 ]; then
echo "成绩D"
else
if [ "$num" -lt 60 ]; then
echo "不及格"
fi
fi
fi
fi
fi
fi
fi

####>>>>>>>>if多分支语句

原文链接:https://www.f2er.com/bash/388575.html

猜你在找的Bash相关文章