Shell编程
#! /bin/sh
a=5
b=3
echo a=$a
echo b=$b
if (test “$a” = “$b”) then
echo a=b
else
echo a!=b
fi
第一行前面的“#”号不是注释,“#!”而且出现在第一行代表通知系统用/bin/sh来执行下面的程序。
参数 |
说明 |
$i |
代表第i个参数 |
$# |
代表参数个数 |
$* |
代表所有参数 |
$? |
上一个命令的返回值 |
#! /bin/sh
echo $#
echo $1
echo $*
怎么办呢?
echo $# $*
shift
echo $# $*
echo “today is” `date`
PDATE=`date`
a=9
b=10
var1=$a+$b
echo $var1
输出结果是”9+10”
var1=`expr $a + $b`
#! /bin/sh
read a
read b
var1=`expr $a + $b`
echo $var1
if (test condition) then
commands
else
commands
fi
#! /bin/sh
if (test “$1” = “start”) then
echo “is start”
else
echo “is nothing”
fi
if [ -option expr ]; then
commands
else
commands
fi
option |
说明 |
-d file |
文件存在并且是个目录 |
-e file |
文件存在 |
-r file |
文件存在并且可读 |
-w file |
文件存在并且可写 |
-x file |
文件存在并且可执行 |
#! /bin/sh
if [ -f abc.c ]; then
echo “have abc.c”
else
echo “have not abc.c”
fi
while [ expr ];
do
commands
done
#! /bin/sh
times=0
while [ "$times" != "5" ];
do
echo $times
times=$[$times + 1]
done
case “string” in
pattern_1)
commands
;;
pattern_2)
commands
;;
*)
commands
;;
esac
#! /bin/sh
case "$1" in
start)
echo "is start"
;;
stop)
echo "is stop"
;;
*)
echo "is nothing"
esac
for varname in list ;
do
commands
done
#! /bin/sh
sun=0
for i in 1 2 3 4 5;
do
sum=$[$sum + i]
echo $sum
done
exit code
:
function funcname
{
commands
}
#! /bin/sh
function func
{
echo "function is begin"
}
func
#! /bin/sh
function func
{
echo "function is begin"
a=$1
b=$2
echo `expr $a + $b`
}
func 3 5
#! /bin/sh
function func
{
echo "function is begin"
a=$1
b=$2
return `expr $a + $b`
}
func 3 9
var1=$?
echo $var1