草庐IT

linux - Bash 'here document' 字/定界符未加引号

coder 2023-06-21 原文

我有以下脚本:

#!/bin/bash
cat << EOF
^A^B^C
EOF

地点:

  • '^A' = 十进制值 1
  • '^B' = 十进制值 2
  • '^C' = 十进制值 3

所以,文件看起来像这样:

xxd main.sh 
00000000: 2321 2f62 696e 2f62 6173 680a 6361 7420  #!/bin/bash.cat 
00000010: 3c3c 2045 4f46 0a01 0203 0a45 4f46 0a    << EOF.....EOF.

当我运行脚本时,十进制值 1 似乎消失了:

./main.sh | xxd
00000000: 0203 0a                                  ...

我必须引用单词/分隔符 ( cat << "EOF" ) 才能让十进制值 1 也出现,但我不明白为什么。

根据 bash 引用手册,这是当 word 时发生的情况未加引号:

If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \newline is ignored, and ‘\’ must be used to quote the characters ‘\’, ‘$’, and ‘`’.

我的问题是:

为什么没有引用单词/分隔符时十进制值 1 消失?参数扩展、命令替换和算术扩展对此有何影响?

最佳答案

正如评论中所指出的,我认为这也是一个错误。

在我的示例中,Bash 最终将 here 文档文本转换为一个文件,同时,它将 here 文档文本转换为某种内部字符串 (istring)。这是我执行的回溯:

#0  expand_word_internal (word=0x7fffffffd610, quoted=2, isexp=0, contains_dollar_at=0x0, expanded_something=0x0) at subst.c:9155
#1  0x00005555555c1bb9 in call_expand_word_internal (w=0x7fffffffd610, q=2, i=0, c=0x0, e=0x0) at subst.c:3614
#2  0x00005555555c1cb3 in expand_string_internal (string=0x5555558a6b28 "\001\002\003\n", quoted=2) at subst.c:3649
#3  0x00005555555c2088 in expand_string_leave_quoted (string=0x5555558a6b28 "\001\002\003\n", quoted=2) at subst.c:3777
#4  0x00005555555c2197 in expand_string (string=0x5555558a6b28 "\001\002\003\n", quoted=2) at subst.c:3825
#5  0x00005555555f203b in write_here_document (fd=3, redirectee=0x5555558a6b08) at redir.c:394
#6  0x00005555555f227f in here_document_to_fd (redirectee=0x5555558a6b08, ri=r_reading_until) at redir.c:476
#7  0x00005555555f2f8b in do_redirection_internal (redirect=0x5555558a6b48, flags=1) at redir.c:970
#8  0x00005555555f1cba in do_redirections (list=0x5555558a6b48, flags=1) at redir.c:234
#9  0x00005555555a347d in execute_disk_command (words=0x5555558a5b08, redirects=0x5555558a6b48, command_line=0x5555558a5408 "cat  <<    EOF\n\001\002\003\nEOF\n", pipe_in=-1, pipe_out=-1, async=0,
    fds_to_close=0x5555558a6b88, cmdflags=0) at execute_cmd.c:5212
#10 0x00005555555a1ced in execute_simple_command (simple_command=0x5555558a6ac8, pipe_in=-1, pipe_out=-1, async=0,     fds_to_close=0x5555558a6b88) at execute_cmd.c:4386
#11 0x000055555559b098 in execute_command_internal (command=0x5555558a6a88, asynchronous=0, pipe_in=-1, pipe_out=-1,     fds_to_close=0x5555558a6b88) at execute_cmd.c:802
#12 0x000055555559a6a6 in execute_command (command=0x5555558a6a88) at execute_cmd.c:405
#13 0x00005555555841ff in reader_loop () at eval.c:180
#14 0x0000555555581bc3 in main (argc=2, argv=0x7fffffffdb68, env=0x7fffffffdb80) at shell.c:792

expand_word_internal 将我的字符串从 "\001\002\003" 转换为其内部字符串表示形式 "\001\002\001\003\" 而不是 "\001\001\001\002\001\003"我认为这是理想的行为。

关于linux - Bash 'here document' 字/定界符未加引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49582441/

有关linux - Bash 'here document' 字/定界符未加引号的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  4. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  5. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  6. ruby - 匹配未转义的平衡定界符对 - 2

    如何匹配未被反斜杠转义的平衡定界符对(其本身未被反斜杠转义)(无需考虑嵌套)?例如对于反引号,我试过了,但是转义的反引号没有像转义那样工作。regex=/(?!$1:"how\\"#expected"how\\`are"上面的正则表达式不考虑由反斜杠转义并位于反引号前面的反斜杠,但我愿意考虑。StackOverflow如何做到这一点?这样做的目的并不复杂。我有文档文本,其中包括内联代码的反引号,就像StackOverflow一样,我想在HTML文件中显示它,内联代码用一些spanMaterial装饰。不会有嵌套,但转义反引号或转义反斜杠可能出现在任何地方。

  7. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  10. ruby-on-rails - 新 Rails 项目 : 'bundle install' can't install rails in gemfile - 2

    我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="

随机推荐