【问题标题】:How to check if an argument is passed to bash script如何检查参数是否传递给bash脚本
【发布时间】:2015-04-06 19:13:13
【问题描述】:

我是脚本新手。

我的代码是

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

如何在调用脚本时检查参数是否传递?(我应该在if语句中写什么作为条件)

【问题讨论】:

标签: bash


【解决方案1】:

The syntax of a bash if 声明是:

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

如果then 之前的最后一条语句成功,则执行then 之后的语句;否则else 后面的语句(如果存在;else 是可选的。)

函数的number of arguments passed$#

您可以使用conditional statement 进行算术比较,其语法为(( @987654324@ ))。 (bash 手册没有(( )) 的精确索引标签;它就在提供的链接下方,即select 语句。)

【讨论】:

    【解决方案2】:

    在大家的帮助下终于找到了答案

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

    【讨论】:

      猜你喜欢
      • 2013-09-05
      • 2013-03-22
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2013-06-18
      • 2021-11-18
      相关资源
      最近更新 更多