【shell脚本学习-3】
part-1
#!/bin/bash
:<<FTP
#test [ 1 -eq 2] #条件测试
x="abc" #不允许有空格
y="abc"
[ "$x" != "$y" ] #字符相等判断
echo $?
#test
test "$x" ="$y"
echo $?
test -n $x #判断字符是为空
echo $?
x=1.5 #数字条件测试
y=8
#数值判断符
[ "$x" -eq "$y" ] #<=
echo $? #执行判断的必须为相同的数据类型
test "$x" -gt "$y" #>=
echo $?
test 1 -eq 1 #=
echo $?
#文件测试
FTP
echo "对/home下的document文件测试"
test -a "/home/document" #判断文件是否存在
echo $? #返回状态布尔值
mkdir -p /home/document
test -a "/home/document" #再次判断文件是否存在
echo $?
cd /home/
touch 1
test -x 1 #测试文件是否可执行
echo $?
echo "sorry you document can't execute"
chmod 777 1
test -x 1 #再次测试文件是否可执行
[ -x 1 ]
echo $?
cd /home
chmod 333 1 #去除1的x "r",保留x 的 "w" 权限
[ -r 1 -o -w 1 ] #-a -w 1 ] #逻辑运算符-a,-o,!
echo $? #判断1是否可读并且可写
#条件判断
if [ -a /etc/sysconfig/selinux ];then #if...then单分支
echo "你的linux系统加固了selinux安全子层"
fi
#echo "shell脚本" > ./test
if:;then echo "active";fi
#&&代替if测试
test "$(w)"="root" && (you login user not is superadmin;exit 1)
echo "please input your number:" #if..then..else双分支
read number #接受用户输入
if [ "$number" -gt 10 ];then
echo "your numbe gater than ten"
else
echo "your number letter than ten"
fi
part-2
#!/bin/bash #嵌套双分支
echo "input your socre:"
read socre
if [ -z "$score" ]; then
echo "your score is null,please input aegin"
else
if [ "$score" -lt 100 -a "$score" -gt 0];then
echo "your score is right,you can continue excute"
else
if [ "$score" = 90 ];then
echo "Your score level is A"
else
if [ "$score"=70];then
echo "Your score level is B"
if [ "$score"=60];
echo "Your score level is C"
else
echo "Your score level is D"
fi
fi
fi
#statements
fi
echo "please input your score" #多分支
read score
if [ -z "$score" ];then #判断分数是否为空值
echo "your score is null,please input aegin"
else
if [ "$z" -ge 100 -a "$z" -le 0 ];then #判断分数是否在[0,100]之间
echo "your score not between 0 and 100,please agin"
read score #重新读取用户输入
else
if[ "$z" -le 60 ]; then
echo "your score is D"
elif[ "$z" -le 70 ];then
echo "your score is C"
elif[ "$z" -le 80 ];then
echo "your score is B"
else
echo "your score is A"
fi
fi
fi
part-3
#!/bin/bash
if [ -e "$1" ];then #判断文件是否存在
echo "$1 file have"
exit 1 #文件不存在状态码
else
touch "$1"
echo "$1 file created" #不存在创建
exit 0 #文件存在状态码
fi
#case..in循环分支语句
echo "please input your char"
read char
case $char in #接受变量
[[:upper:]]) #判断条件1
echo "your char is letter";; #结束
[[:lower:]])
echo "your char is lower";;
[0-9])
echo "your char is number";;
*) #默认
echo "your char is other";;
esac #终止循环$
wile getopt ":pq:"y: #while循环
do
case $y in
"p")
echo "$y";;
"q")
echo "$x $y";;
"::")
echo "$x";;
"?")
echo "$x
"*")
echo "z";;";;
esac
echo "w"
done
#算数运算符
result=`expr 3 / 2`
echo "$result" #expr:算数计算符
#result=`expr \(1 * 2\)\ + 1`
#echo "$result"
result=$(( 10 + 90 / 2 )) #$(()):算数运算符
echo "$result"
#$[]算数运算符
result=$[ 5*2 ]
echo "$result"
n=1 #let算数运算符
let n=n+1
echo "$n"
result=`expr 4 += 6`
echo "$result"
part-4
#!/bin/bash
result=$((4 += 6)) #复合算数运算符
echo "$result"
:<<FTP #位运算符
x=let "2 >> 1 " #2的二进制向右移一位
echo "$x"
y=$[ "$x" ~= 1 ] #变量x与1异或
echo "y"
z=`expr 1 ^ 0` #1和0取反
echo $z
FTP
result=$[ 2 << 1 ]
echo "$result"
x=1
result=$[ x |= 2 ] #位复合运算符
echo "$result"
y=5 #自增自减运算符
result=$[ y+(++y) ]
echo "$result"
result=$((result++))
echo "$result"
result=$[ --result ]
echo "$result"
#法一:
((x=201)) #二,八,十六进制
echo "$x"
((y=021))
echo "$y"
((z=0x21))
echo "$z"
#法二
((x=2#201))
echo "$x"
((y=8#201))
echo "$y"
((z=16#201))
echo "$z"
part-5
#!/bin/bash
#for i in `ls .` #for..in循环
#do
# if echo "$i" | grep "a"
# then
# echo "$i"
# fi
#done
for i in [1..10] #1 2 3 5 8
do
echo "The Cureent number is $i"
done
total=0 #for循环
for i in {1..100..2} #1 2 3 5 8 #{start,end,步长}
do
let "total=total+i"
#echo "The Cureent number is $i"
done
echo "total is $total"
for days in {M T S T F S S} #列表值
do
echo "Today is $days"
done
for x in $(ls) #* #"ls" #循环显示命令
do
echo "$x"
done
echo "$*" #接受输入所有的参数
for arg in "$*"
do
echo "${arg}"
done
for arg in #不带列表的for
do
echo "${arg}"
done
x="a"
for ((;;)) #类c的死循环 #((i=0;i<=5;i++)) #类c的for
do
echo "$x"
done
array=(M,T,T,F,S,S) #处理数组
for i in ${array[*]}
do
echo "$i"
done
i=1
until [[ "$i" -gt 10 ]]; do #开始until循环,当i>10时退出until循环
let "square=i*i" #条件
echo "$i*$i=$square"
let "i++" #自增计数器
#statements
done
part-6
#!/bin/bash
i=1
until [[ "$i" -gt 10 ]]; do
#开始until循环,条件测试i>10时退出until循环
let "square=i*i" #let条件计算
echo "$i*$i=$square"
let "i++" #自增计数器
#statements
done #中断
i=1
while [[ "$i" -lt 10 ]]; do #while循环
let "square=i*i" #let条件计算
echo "$i*$i=$square"
let "i++" #自增计数器
#statements
done
echo "please input your number,must between 1 and 10,and 0 exits"
read number
while [[ "$number" != 0 ]]; do #while判断计数
#echo "you can input"
#read number #再次读取用户输入
if [ "$number" -lt 5 ];then #多分支判断准确输入
echo "sorry,your number lt 5"
read number
elif [ "$number" -gt 5 ];then
echo "sorry,your number gt 5"
read number
else
echo "Configuration,You are right!"
exit 0 #退出
fi
#statements
#statements
done
i=1
until [[ "$i" -ge 21 ]];do
userdel user$i #代入计数器i,执行创建用户
#echo "123" | passwd --stdin user$i >> /dev/null #设置密码
let "i++" #自增
#statements
done
while [[ i=0;i<9;i++ ]]; do
while [[ j=0;j<i;i++ ]]; do
if [[ j -gt 9 ]]; then
let "product=i*j"
printf "$i*$j=$product"
#statements
else
echo " "
echo " "
fi
#statements
done
echo
#statements
done
part-7
#!/bin/bash
for((i=0;i<10;i++));do #嵌套循环
for (( j=0;j<i;j++ )); do
let "product=i*j"
printf "$i*$j=$product"
if [[ $product -gt 10 ]]; then
echo " "
else
echo " "
#statements
fi
#statements
done
echo
done
posted on 2018-10-25 10:41 activecode 阅读(256) 评论(0) 编辑 收藏 举报