如何将一行bash脚本分成多行?

时间:2014-02-25 19:58:47

标签: bash

我想让我的一些bash脚本在眼睛上更容易一些。让我说我有这个脚本,它的工作正常。

if [ "$PASSWDCHECK" = "<title>401 Authorization Required</title>" ]
then echo "The Subsubscription password is wrong" ; exit
else mkdir /home/"$USERNAME"/src && mkdir /opt/logstash && mkdir /etc/snort && mkdir /etc/snort/rules/
fi

我想让它更像这样,但它无法执行。我错过了什么?

if [ "$PASSWDCHECK" = "<title>401 Authorization Required</title>" ]
then echo "The Subsubscription password is wrong" ; exit
else mkdir /home/"$USERNAME"/src \ &&
    mkdir /opt/logstash \ &&
    mkdir /etc/snort \ &&
    mkdir /etc/snort/rules/ \ &&
fi

3 个答案:

答案 0 :(得分:3)

您需要将\放在每行的末尾,而不是&&之前。确保\之后没有空格。

答案 1 :(得分:2)

只要&&是该行上的最后一个非空白标记,该命令将自动继续到下一行。

if [ "$PASSWDCHECK" = "<title>401 Authorization Required</title>" ]
then echo "The Subsubscription password is wrong" ; exit
else mkdir /home/"$USERNAME"/src &&
     mkdir /opt/logstash &&
     mkdir /etc/snort &&
     mkdir /etc/snort/rules/
fi

&&列表连接两个命令,因此解析器知道第二个命令之前的换行符应该被丢弃。

答案 2 :(得分:0)

if [ "$PASSWDCHECK" = "<title>401 Authorization Required</title>" ]
then echo "The Subsubscription password is wrong" ; exit
else 
  mkdir /home/"$USERNAME"/src && \
  mkdir /opt/logstash && \
  mkdir /etc/snort && \
  mkdir /etc/snort/rules/
fi