AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 407367
Accepted
Ahn
Ahn
Asked: 2012-07-13 21:21:05 +0800 CST2012-07-13 21:21:05 +0800 CST 2012-07-13 21:21:05 +0800 CST

如何避免缺少命令行参数的语法错误?

  • 772

如何避免缺少命令行参数的语法错误?

示例 shell 脚本:

 var1=$1;
 var2=$2;
 echo $var1
 echo $var2
 var3=`expr $var1 + $var2`;
 echo $var3

输出 :

shell>sh shelltest 2 3
2
3
5

输出 :

    shell>sh shelltest
expr: syntax error

由于没有传递任何参数,我怎样才能避免这种情况并传递我自己的消息而不是“expr:语法错误”?

shell-scripting
  • 2 2 个回答
  • 7574 Views

2 个回答

  • Voted
  1. Best Answer
    Abhijeet Kasurde
    2012-07-13T21:47:52+08:002012-07-13T21:47:52+08:00

    $#您可以使用变量检查 shell 脚本中缺少的参数。

    例如:

    #!/bin/bash
    #The following line will print no of argument provided to script
    #echo $#
    USAGE="$0 --arg1<arg1> --arg2<arg2>"
    
    if [ "$#" -lt "4" ] 
    then 
        echo -e $USAGE;    
    else 
        var1=$2;
        var2=$4;
        echo `expr $var1 + $var2`;
    fi
    
    • 4
  2. mgorven
    2012-07-13T21:52:03+08:002012-07-13T21:52:03+08:00

    我通常使用“Indicate Error if Null or Unset”参数扩展来确保指定参数。例如:

    #!/bin/sh
    var1="${1:?[Please specify the first number to add.]}"
    var2="${2:?[Please specify the second number to add.]}"
    

    然后这样做:

    % ./test.sh
    ./test.sh: 2: ./test.sh: 1: [Please specify the first number to add.]
    % ./test.sh 1
    ./test.sh: 3: ./test.sh: 2: [Please specify the second number to add.]
    

    从联机帮助页:

     ${parameter:?[word]}  Indicate Error if Null or Unset.  If parameter is
                           unset or null, the expansion of word (or a message
                           indicating it is unset if word is omitted) is
                           written to standard error and the shell exits with
                           a nonzero exit status.  Otherwise, the value of
                           parameter is substituted.  An interactive shell
                           need not exit.
    
    • 4

相关问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve