我一直试图让这个东西工作几个小时,但我无法让它工作:
if [ "$P" = "SFTP" -a "$PORT" != "22" ] || [ "$P" = "FTPS" && [ "$PORT" != "990" -a "$PORT" != "21" ] ] ; then
有人能帮我吗 ?我知道可以这样写多个条件:
if [ "$P" = "SFTP" ] && [ "$PORT" != "22" ]; then
但是我怎样才能像我的第一个例子一样覆盖这些条件呢?
我一直试图让这个东西工作几个小时,但我无法让它工作:
if [ "$P" = "SFTP" -a "$PORT" != "22" ] || [ "$P" = "FTPS" && [ "$PORT" != "990" -a "$PORT" != "21" ] ] ; then
有人能帮我吗 ?我知道可以这样写多个条件:
if [ "$P" = "SFTP" ] && [ "$PORT" != "22" ]; then
但是我怎样才能像我的第一个例子一样覆盖这些条件呢?
您不能将表达式嵌套在单括号中。它应该这样写:
if [ "$P" = "SFTP" -a "$PORT" != "22" ] || [ "$P" = "FTPS" -a "$PORT" != "990" -a "$PORT" != "21" ] ; then
这可以写成单个表达式:
if [ \( "$P" = "SFTP" -a "$PORT" != "22" \) -o \( "$P" = "FTPS" -a "$PORT" != "990" -a "$PORT" != "21" \) ] ; then
尽管 is 并不与所有 shell 完全兼容。
由于您使用的是 bash,因此可以使用双括号使命令更具可读性:
if [[ ( $P = "SFTP" && $PORT != "22" ) || ( $P = "FTPS" && $PORT != "990" && $PORT != "21" ) ]] ; then