linux shell是强大的脚本程序,语法简单,下面是一个可执行的sh脚本,涵盖了常用的shell语法和用法,理解它,就等于入门了。
#!/bin/bash
# title :
# date : ?2012/10/18
# author: Made by hfx
echo '[test @test test]# sh [-nvx] scripts
-n :不要执行 scripts ,查询 scripts 内的语法,若有错误则予以列出!
-v :在执行 scripts 之前,先将 scripts 的内容显示在屏幕上;
-x :将有使用到的 scripts 内容显示在屏幕上,与 -v 稍微不同 '
echo '转义字符\后面带符号'
hello=Hello\ \!\ How\ are\ you\ \?
echo $hello
echo '单引号和双引号的区别:单引号里的$只表示$,双引号里的$表示变量'
name="V.Bird"
myname1="My name is $name"
myname2='My name is $name'
echo $name
echo $myname1
echo $myname2
echo '显示声明变量declare -i'
declare -i a=3
declare -i b=4
#declare -i c=$a * $b
#echo $c
echo '交互'
echo 'cin you name:'
read name
echo 'your name is:'
echo $name
echo 'shell 程序的参数'
echo '$0 表示sh程序后的第0个参数,$1……'
echo '条件语句'
echo 'continue please cin:y or Y'
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
echo "script is running..."
else
echo "STOP!"
fi
echo 'case语句'
echo "Press your select one, two, three!"
read choice
case $choice in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}"
#exit 1
esac
echo '循环语句for()'
echo '1+……+100'
declare -i s # <==变量宣告
for (( i=1; i<=100; i=i+1 ))
do
s=s+i
done
echo "1+... + 10?0 = $s"
echo 'do...while'
echo "Press Y/y to stop"
until [ "$yn" = "Y" ] || [ "$yn" = "y" ]
do
read yn
done
echo "Stop here"
echo 'test shell end!'