Negating multiple conditions in Bash

前端 未结 3 908
半阙折子戏
半阙折子戏 2021-01-24 17:30

I realize this is a simple question, but I am finding a hard time getting an answer due to the finnicky syntax requirements in bash. I have the following script:



        
3条回答
  •  梦毁少年i
    2021-01-24 18:03

    Remember (or learn!) that [ is not part of shell syntax, it is a command.

    If you want to group together multiple commands, then you should use { }

    if ! { [ -z "$1" ] || [ -z "$2" ]; }
    

    Note that the parser expects to see a line ending or semicolon before the closing }.

    For the record, I'd avoid the negation and use && here:

    if [ -n "$1" ] && [ -n "$2" ]
    

    ...and since you specified bash, you may as well take advantage of the enhanced test construct:

    if [[ -n $1 && -n $2 ]]
    

提交回复
热议问题