我收到此脚本的错误
#!/bin/bash
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
pink="\033[35m"
cyan="\033[36m"
white="\033[37m"
echo ""
echo -e "${green} _ __ ___ ___ "
echo -e "${green} | '_ \/ __/ __|"
echo -e "${green} | | | \__ \__ \"
echo -e "${green} |_| |_|___/___/"
echo -e "${blue} Welcome My first program"
echo ""
系统提示错误
./my_first_program.sh: Line 14: unexpected token `| ' Syntax error near
./my_first_program.sh: Line 14: `echo -e "${green} |_| |_|___/___/"'
我该如何修复它?
答案1
在行中
echo -e "${green} | | | \__ \__ \"
\
之前的那个"
会删除那个的特殊含义"
,使其成为一个不会结束字符串的普通字符。因此字符串会继续到下一行,直到之前"
和${green}
之后${green} |_| |_|___/___/
都不再被引用,从而导致错误。
在 后添加一个空格\
以防止其转义"
:
echo -e "${green} | | | \__ \__ \ "
答案2
好的,我对你的脚本做了修改,代码如下:
#!/bin/bash
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
pink="\033[35m"
cyan="\033[36m"
white="\033[37m"
echo ""
echo -e "${green} _ __ ___ ___ "
echo -e "${green} | '_ \/ __/ __|"
echo -e "${green} | | | \__ \__ \\"
echo -e "${green} |_| |_|___/___/"
echo -e "${blue} Welcome My first program"
echo ""
改变的是使用\\"
而不是\"
。这将打印一个文字反斜杠,然后结束引用,而不是打印文字"
并无法结束引用。(shell 将同时处理反斜杠和双引号(特别是在某些情况下。)