linux Shell的使用
下面是我对shell脚本使用的学习,写成日志,做个笔记,可以当做自己以后的复习。
1.输出hello world!
vi test1.sh, 如果不会vi工具的使用,请参考 linux vi 工具的使用
#!/bin/bash
echo
"hello world"
|
注:新创建的文件默认是没有执行权限的,所以必须给该文件授予执行权限后才能执行。授权命令:chmod u+x test1.sh或者chmod 755 test1.sh
最后执行
./test1.sh
2.使用变量
vi test2.sh
#!/bin/bash
read -p
"please input your name:"
name
echo
"hello,$name!"
|
3.条件判断的使用
vi condition.sh
#!/bin/bash
if
[
"$1"
=
"normal"
]
then
echo
"this is normal case"
elif [ -z
"$1"
]
then
echo
"no input, ignal..."
fi
|
./condition.sh normal
输出:this is normal case
注意: [ 符号后面必须有一个空格,否则执行出错。
4. whle[]...do...done 语句
类似于c语言的do..while 语句
#!/bin/bash
echo
"please use add or delete or exit"
ACTION=
"default"
while
[ -n $ACTION ]
do
read ACTION
case
$ACTION in
add)
echo
"add somebody"
;;
delete)
echo
"delete somebody"
;;
exit)
echo
"complete"
break
;;
*)
echo
"invalide action,please re-enter"
;;
esac
done
|
5 for循环
#!/bin/bash
for
C in
123
2
3
4
5
1
hello
do
echo $C
done
|
输出每一个元素
6 函数的使用
#!/bin/bash
strcat()
{
OUT=
"$1"
" "
"$2"
return
0
}
stract2()
{
echo
"$1"
" "
"$2"
return
3
}
A=
"bird"
B=
"mouse"
OUT=
""
strcat $A $B
echo $OUT
OUT2=
'strcat2 $A $B'
echo $?
echo $OUT2
|
输出结果:
bird mouse
0
strcat2 bird mouse
|
参考资料
Android内核剖析
好吧,对shell的学习先到这里,以后继续深入学习Shell。可以使用这本书 《Linux于unix shell编程指南》。
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/3312096.html,如需转载请自行联系原作者