shell脚本-02

shell字符串:

str="sasa"
echo "hello,i am \"${str}\""
country="china"
city="qingdao"

echo "${city},${country}"

echo "获取字符串长度"
echo "${#country}"

echo "截取字符串,从0开始"
echo "${country:1:3}"

echo "查找子字符串"
greet="hello heihei haha"
echo `expr index "${greet}" he`
root@VM-0-17-ubuntu:/home/bash# ./bash13.sh
hello,i am "sasa"
qingdao,china
获取字符串长度
5
截取字符串,从0开始
hin
查找子字符串
1

数组:

#数组的定义
echo "数组的定义"
arr=(zhangsan lisi wangwu)
echo ${arr[*]}

arr=(
zhangsan
lisi
wangwu
zhaoliu
)
echo ${arr[*]}

arr[0]=zhangsan
arr[1]=sasa
arr[2]=lisi
echo ${arr[@]}

#获取数组长度

echo "获取数组长度"
echo ${#arr[*]}
root@VM-0-17-ubuntu:/home/bash# ./bash14.sh
数组的定义
zhangsan lisi wangwu
zhangsan lisi wangwu zhaoliu
zhangsan sasa lisi zhaoliu
获取数组长度
4

echo命令:

str=qqq
echo 输出时双引号可以忽略
echo ${str}
echo "${str}"

echo 显示转义字符
echo \"${str}\"

echo 显示换行
echo -e "hello \n sasa"
echo "zhangsan"

echo 显示结果重定向到文件,此时file2文件中该字符串
echo "line one" >file2.txt

echo 显示命令执行结果
echo `date`
root@VM-0-17-ubuntu:/home/bash# ./bash15.sh
输出时双引号可以忽略
qqq
qqq
显示转义字符
"qqq"
显示换行
hello
 sasa
zhangsan
显示结果重定向到文件,此时file2文件中该字符串
显示命令执行结果
Sat May 26 11:52:40 CST 2018

if else 语句

cho if [] then ... fi
echo if [] then ... else ... fi
echo if [] then ... elif []  then ... elif [] then ... else ... fi

echo [] 条件判断

a=10
b=20

if [ ${a} == ${b} ]
then
        echo ==
else
        echo !=
fi

echo test 条件判断

if test ${a} == ${b}
then
        echo ==
else
        echo !=
fi

if [ ${a} == ${b} ]
then
        echo ==
elif [ ${a} -gt ${b} ]
then
        echo "a>b"
else
        echo "a<b"
fi
root@VM-0-17-ubuntu:/home/bash# ./bash16.sh
if [] then ... fi
if [] then ... else ... fi
if [] then ... elif [] then ... elif [] then ... else ... fi
[] 条件判断
!=
test 条件判断
!=
a<b

case in语句

echo please enter a num between 1 and 4
read num
case ${num} in
        1)echo 1111111
        ;;
        2)echo 222222
        ;;
        3)echo 33333
        ;;
        4)echo 4444444
        ;;
        *)echo "not in 1-4,is ${num}"
        ;;
esac
root@VM-0-17-ubuntu:/home/bash# ./bash17.sh
please enter a num between 1 and 4
7
not in 1-4,is 7
root@VM-0-17-ubuntu:/home/bash# ./bash17.sh
please enter a num between 1 and 4
3
argu1=${1}
case ${argu1} in
        [a-z]|[A-Z])
                echo "a-Z"
        ;;
        [0-9])
                echo "0-9"
        ;;
        *)
                echo "what?"
        ;;
esac
root@VM-0-17-ubuntu:/home/bash# ./bash18.sh  1
0-9
root@VM-0-17-ubuntu:/home/bash# ./bash18.sh  y
a-Z
root@VM-0-17-ubuntu:/home/bash# ./bash18.sh  *
what?
for循环
echo 每次输出的内容根据空格作为分割
for loop in 1 2 3 4 5
do echo "this is ${loop}"
done

for loop in 1234
do echo "${loop}"
done

for loop in "this is shell,enjoy it"
do echo "${loop}"
done

for loop in "this is shell,enjoy it" "java is no.1"
do echo "${loop}"
done

for file in ./*

do
if test -x ${file}
then
        echo "${file} can execute"
else
        echo "${file} can not execute"
fi

done
root@VM-0-17-ubuntu:/home/bash# ./bash19.sh
每次输出的内容根据空格作为分割
this is 1
this is 2
this is 3
this is 4
this is 5
1234
this is shell,enjoy it
this is shell,enjoy it
java is no.1
./bash01.sh can execute
./bash02.sh can execute
./bash03.sh can execute
./bash04.sh can execute
./bash05.sh can execute
./bash06.sh can execute
./bash07.sh can execute
./bash08.sh can execute
./bash09.sh can execute
./bash10.sh can execute
./bash11.sh can execute
./bash12.sh can execute
./bash13.sh can execute
./bash14.sh can execute
./bash15.sh can execute
./bash16.sh can execute
./bash17.sh can execute
./bash18.sh can execute
./bash19.sh can execute
./file1.txt can not execute
./file2.txt can not execute

while循环

echo "type ctrl+d to exit"
echo "please enter ur favourite game"
while read gameName
do
        echo "wow,it is ${gameName}"
done
root@VM-0-17-ubuntu:/home/bash# ./bash21.sh
type ctrl+d to exit
please enter ur favourite game
lol
wow,it is lol
count=0
while [[ ${count} -lt  5 ]]
do
        echo ${count}
        count=$(expr ${count} + 1)
done
root@VM-0-17-ubuntu:/home/bash# ./bash20.sh
0
1
2
3
4

until循环

a=0
until [[ ${a} -gt 10 ]]
do
        echo ${a}
        a=$(expr ${a} + 1)
done
root@VM-0-17-ubuntu:/home/bash# ./bash22.sh
0
1
2
3
4
5
6
7
8
9
10













猜你喜欢

转载自blog.csdn.net/qq_20867981/article/details/80459623