100個shell腳本。天天一個堅持下去。

20180306
1、case
#!/bin/bash
#測試casegit

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
2、
#!/bin/bashbash

result=expr 2 - 100
echo "$result"ide

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

result=$[5+4]
echo $resultcode

###數值運算
20180308
3、
#!/bin/bash字符串

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

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

sum=0;test

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

for file in $(ls)
do

echo $file
done

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

20180312
7、
#!/bin/bash

array=(Monday tuesday wednesday thuesday friday saturday sunday)
for day in ${array[*]}
do
echo $day
done
20180313
8、
#!/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
9、
#!/bin/bash

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

10、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 $?

十4、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多分支語句

相關文章
相關標籤/搜索