shell接收参数

2020-07-25 11:07:47

脚本文件名后加参数

格式:

xxx.sh p1 p2 p3

接收

\(1代表p1,\)2代表p2,\(3代表p3</p><p>例子(计算器):</p><p><pre lay-lang="Shell" lay-title="Shell">#!/bin/bash echo \)1 \(2 \)3 if [ $1 = ‘a’ ] then

echo `expr $2 + $3`

elif [ $1 = ’s’ ] then

echo `expr $2 - $3`

elif [ $1 = ’m’ ] then

echo `expr $2 \* $3`

elif [ $1 = ’d’ ] then

if [ $3 == 0 ]
then
    echo 'param2 can not be 0'
else
    echo `expr $2 \/ $3`
fi

else

echo 'param error'

fi