bash 无法让 ASCII 艺术回显到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25214084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:36:34  来源:igfitidea点击:

Can't get ASCII art to echo to console

bashscriptingsyntax-errorascii

提问by user3698316

I'm new to Bash scripting, and this here is just puzzling to me. I'm adding ASCII art to a project, and can't seem to figure out how to escape certain characters. Would someone please help me get the following code below to work?

我是 Bash 脚本的新手,这让我感到困惑。我正在将 ASCII 艺术添加到一个项目中,但似乎无法弄清楚如何转义某些字符。有人可以帮我让下面的代码工作吗?

Whenever I tried adding slashes as escape characters to fix the errors, the slashes also wound up printing to console on execution. This ruins the image. I don't understand what I'm doing wrong, so I've posted the code below in the hopes that someone will take a moment to show me the right way. Please? I've removed the quotes to prevent more clutter.

每当我尝试添加斜杠作为转义字符来修复错误时,斜杠也会在执行时打印到控制台。这破坏了形象。我不明白我做错了什么,所以我在下面发布了代码,希望有人能花点时间向我展示正确的方法。请?我删除了引号以防止更混乱。

echo -en "\E[31m"
echo
echo       _,.
echo     ,` -.)
echo    '( _/'-\-.              
echo   /,|`--._,-^|          ,    
echo   \_| |`-._/||          ,'|      
echo     |  `-, / |         /  /     
echo     |     || |        /  /      
echo      `r-._||/   __   /  / 
echo  __,-<_     )`-/  `./  /
echo '  \   `---'   \   /  /
echo     |           |./  / 
echo     /           //  /    
echo \_/' \         |/  /        
echo  |    |   _,^-'/  /             
echo  |    , ``  (\/  /_       
echo   \,.->._    \X-=/^        
echo   (  /   `-._//^` 
echo    `Y-.____(__}             
echo     |     {__)          
echo           ()`    

回答by that other guy

Quotes in bash are important syntactic elements, not clutter. However, to print ASCII art, save yourself the trouble of proper quoting and escaping and just use a here document:

bash 中的引号是重要的句法元素,而不是混乱。但是,要打印 ASCII 艺术,请避免正确引用和转义的麻烦,只需使用here document

cat << "EOF"
       _,.
     ,` -.)
    '( _/'-\-.               
   /,|`--._,-^|            ,     
   \_| |`-._/||          ,'|       
     |  `-, / |         /  /      
     |     || |        /  /       
      `r-._||/   __   /  /  
  __,-<_     )`-/  `./  /
 '  \   `---'   \   /  / 
     |           |./  /  
     /           //  /     
 \_/' \         |/  /         
  |    |   _,^-'/  /              
  |    , ``  (\/  /_        
   \,.->._    \X-=/^         
   (  /   `-._//^`  
    `Y-.____(__}              
     |     {__)           
           ()`     
EOF

Make sure not to remove the quotes here. They are not optional.

确保不要删除此处的引号。它们不是可选的。

回答by Keith Thompson

echotakes a series of arguments. If you type

echo需要一系列参数。如果你输入

echo  foo      bar

the echocommand gets two arguments, "foo"and "bar", and the spacing between the words is discarded.

echo命令有两个参数"foo""bar",并且单词之间的间距被丢弃。

For what you're trying to do, you probably want echoto receive exactly one argument for each line. In bash, the easiest way is probably to use so-called "ANSI-C Quoting". Within each string, each apostrophe 'and backslash \character has to be escaped with a backslash.

对于您要执行的操作,您可能希望echo每行只接收一个参数。在 bash 中,最简单的方法可能是使用所谓的"ANSI-C Quoting"。在每个字符串中,每个撇号'和反斜杠\字符都必须用反斜杠转义。

Here's a version of your script using this method:

这是使用此方法的脚本版本:

#!/bin/bash

echo -n $'\E[31m'
echo $''
echo $'      _,.'
echo $'    ,` -.)'
echo $'   \'( _/\'-\\-.'
echo $'  /,|`--._,-^|          ,'
echo $'  \_| |`-._/||          ,\'|'
echo $'    |  `-, / |         /  /'
echo $'    |     || |        /  /'
echo $'     `r-._||/   __   /  /'
echo $' __,-<_     )`-/  `./  /'
echo $'\'  \   `---\'   \   /  /'
echo $'    |           |./  /'
echo $'    /           //  /'
echo $'\_/\' \         |/  /'
echo $' |    |   _,^-\'/  /'
echo $' |    , ``  (\/  /_'
echo $'  \,.->._    \X-=/^'
echo $'  (  /   `-._//^`'
echo $'   `Y-.____(__}'
echo $'    |     {__)'
echo $'          ()`'

(The added backslashes do mess up the picture in the script, but it appears correctly on output.)

(添加的反斜杠确实弄乱了脚本中的图片,但它在输出中正确显示。)

For this case, that other guy's answeris a better approach, since it avoids the need to escape any of the special characters, but this technique could be useful for smaller output.

对于这种情况,其他人的答案是更好的方法,因为它避免了转义任何特殊字符的需要,但这种技术对于较小的输出可能很有用。

Or you could just put the raw picture into a file and catit to standard output.

或者您可以将原始图片放入一个文件中,cat然后将其输出到标准输出中。