Shell实例:字符串操作 逻辑判断
#!/bin/bash
echo "enter a number"
#read input value
read ans
#case
case $ans in
1)
echo "your number is $ans"
;;
2)
echo "your number is 2"
;;
[3-9])
echo "your number is $ans"
esac
echo "Parameter numbers:$#"
echo "Shell name:$0"
echo "Shell all parameters:$*"
echo "Shell PID:$$"
echo "Shell PPID:$!"
echo "Before command return value:$?"
#echo "get command return:$(ls /home/provision)"
#if : -eq(==) -ne(!=) -lt(<) -gt(>) -ge(>=) -le(<=)
if [ "22" -lt "33" ]
then
echo "22 less than 33"
else
echo "22 not less than 33"
fi
#while
#no space left or right =
num=1
echo "init num = $num"
#need space after [ ,before ]
while [ $num -le 10 ]
do
echo "num=$num"
let num=num+1
done
#file exist
fileName="case_test.sh"
if [ -e $fileName ]
then
echo "$fileName is exist:"
else
echo "$fileName is not exist:"
fi
#is directory
if [ -d $fileName ]
then
echo "$fileName is directory"
else
echo "$fileName is not directory"
fi
if [ -z $fileName ]
then
echo "fileName length is 0"
else
echo "fileName length is not 0"
fi
if [ -n $fileName ]
then
echo "fileName length is not 0"
else
echo "fileName length is 0"
fi
#
for filename in `ls`
do
cat $filename
done
for((i=0; i<10; i++))
do
echo "--$i"
done
echo "Name:${name:-huangxiaobing}"
#function return only number cannot return string
getName(){
return 100
}
getName
echo "getName:$?"
#connect two string
firstname="huang"
lastname="xiaobing"
allname=`printf "%s%s" "$firstname" "$lastname"`
echo "All Name:$allname"
#get the char count of string
countChar=`echo "$firstname"|wc -m`
echo "$firstname count:$countChar"
#return nor number char: return null
echo "12345"|sed 's/[0-9]//g'
#return A
echo "123A45"|sed 's/[0-9]//g'
#return huang
echo ${allname%%xiaobing}
echo ${allname:0:5}
echo ${allname:2:7}
百度阅读电子书地址:http://yuedu.baidu.com/ebook/f6dbb2a2f01dc281e53af0f3
讨论QQ群:536192476
个人公众号: