如何检查参数是否传递给bash脚本

问题描述:

我对脚本编程不熟悉。如何检查参数是否传递给bash脚本

我的代码是

#!/bin/bash 
path=$1 
if [ condition ]; then 
....... 
fi 

我如何检查如果在调用脚本的参数传递(我应该怎么写的条件,如果内部语句)

+2

帮助答案'()'不是记录的bash if语法的一部分。你有没有试过看任何bash脚本教程?这应该很容易找到几乎所有的人。 – 2015-02-06 15:37:41

+1

对不起,这是一个错误,C编程宿醉 – 2015-02-06 15:39:01

+0

看看http://*.com/questions/4423306/how-do-i-find-the-number-of-arguments-passed-to-a-bash-script – user7 2015-02-06 15:39:06

,终于找到了与所有

if(($#==1));#check if number of arguments is 1 and return a boolean value 
then 
<code> 
fi 

The syntax of a bash if说法是:

if statement; ... then statement; ... else statement; ... fi 

then之后的语句被执行,如果最后的状态员t then成功;否则后面的语句else(如果存在的话; else是可选的。)

number of arguments passed函数是$#

您可以使用conditional statement进行算术比较,其语法为((  arithmetic expression  ))。 (bash的手册并没有一个准确的索引标记为(()),它只是提供的链接,这对于select语句下面。)