标签: herestring

在这里 - 字符串经历分词吗?

引用Bash参考手册man bash(版本4.3):

[n]<<< word

word经历括号扩展,波浪线扩展,参数和变量扩展,命令替换算术扩展和引用的去除.不执行路径名扩展和单词拆分.

word不应该发生的字分裂.但是,使用这个简单的代码:

var="a    b"

cat <<< $var
#output:
#a b

cat <<< "$var"
#output:
#a    b
Run Code Online (Sandbox Code Playgroud)

我错过了什么?这取决于手册的版本bash或是否存在错误?我在用GNU bash, version 4.3.48.

linux bash herestring

15
推荐指数
1
解决办法
259
查看次数

这里字符串添加换行符

似乎here string是在添加换行符.有没有方便的删除方法?

$ string='test'
$ echo -n $string | md5sum
098f6bcd4621d373cade4e832627b4f6  -
$ echo $string | md5sum
d8e8fca2dc0f896fd7cb4cb0031ba249  -
$ md5sum <<<"$string"
d8e8fca2dc0f896fd7cb4cb0031ba249  -
Run Code Online (Sandbox Code Playgroud)

bash herestring

10
推荐指数
2
解决办法
1175
查看次数

PowerShell 嵌套此处字符串

将以下内容放入脚本中将会失败:

$MyString = @'
hello
@'
'@
bye
'@

write-host $MyString
Run Code Online (Sandbox Code Playgroud)

返回的错误如下:

At C:\scripts\test.ps1:6 char:1
+ '@
+ ~~
The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Run Code Online (Sandbox Code Playgroud)

在嵌套的@'and'@前面加上反引号(重音符号)确实可以用作转义字符,但它也被视为文字,因此出现在 的输出中$MyString

是否有正确的方法可以在不干扰输出的单引号此处字符串中转义单引号此处字符串?

注意:

我应该提到,外部此处字符串的内容是动态填充的,该内容由另一个程序作为命令行参数提供给 powershell 脚本。这没有反映在示例代码中,因为我不想用我非常具体且有些利基的实现来模糊问题。

外部此处字符串不太可能包含内部此处字符串(但有可能发生),因此我认为需要进行一些防御性编程来适应这种情况。

powershell escaping herestring

6
推荐指数
1
解决办法
2207
查看次数

while 循环 bash 中变量的行

假设一个文件file有多行。

$ cat file
foo
bar
baz
Run Code Online (Sandbox Code Playgroud)

进一步假设我希望用 while 循环遍历每一行。

$ while IFS= read -r line; do
$   echo $line
$   # do stuff
$ done < file
foo
bar
baz
Run Code Online (Sandbox Code Playgroud)

最后,请假设我希望传递存储在变量中的行而不是存储在文件中的行。如何循环保存为变量的行而不收到以下错误?

$ MY_VAR=$(cat file)
$ while IFS= read -r line; do
$   echo $line
$   # do stuff
$ done < $(echo "$MY_VAR")
bash: $(echo "$MY_VAR"): ambiguous redirect
Run Code Online (Sandbox Code Playgroud)

bash heredoc while-loop herestring

6
推荐指数
2
解决办法
5669
查看次数

在 bash 中将 **heredoc** 添加到文件中

我正在使用:

cat <<<"${MSG}" > outfile
Run Code Online (Sandbox Code Playgroud)

首先向 写入消息outfile,然后继续进行进一步处理,这将附加到outfile我的 awk 脚本中。

但现在已逻辑在我的计划改变了,所以我得先填入 outfile从我的附加线awk计划(从我的bash脚本外部调用),然后在最后一步前插是$ {}味精heredoc我的头outfile。 .

我怎么能从我的 bash 脚本而不是 awk 脚本中做到这一点?

编辑

这是味精heredoc

read -r -d '' MSG << EOF
-----------------------------------------------
--   results of processing - $CLIST
--   used THRESHOLD ($THRESHOLD)
-----------------------------------------------
l
EOF
# trick to pertain newline at the end of a message
# see here: http://unix.stackexchange.com/a/20042
MSG=${MSG%l}
Run Code Online (Sandbox Code Playgroud)

bash prepend herestring

5
推荐指数
3
解决办法
1094
查看次数

如何在powershell中为herestring/heredoc设置编码?

我正在尝试更新 Windows 服务器上的主机文件,并尝试使用 powershell 中的 heredoc 来完成。

我不明白为什么我的结果在每个主机条目中的每个字符之间都有额外的空格。

我正在从 Linux 移植一些脚本。

PS C:\Users\Administrator> cat C:\Users\Administrator\AppData\Local\Temp\etchosts.ps1
@"
127.0.0.1 src.example.com
127.0.0.2 builds.example.com
127.0.0.3 ti.example.com
127.0.0.4 jira.example.com
"@ >>C:\Windows\System32\drivers\etc\hosts



PS C:\Users\Administrator> powershell C:\Users\Administrator\AppData\Local\Temp\etchosts.ps1
PS C:\Users\Administrator> cat C:\Windows\System32\drivers\etc\hosts
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address …
Run Code Online (Sandbox Code Playgroud)

powershell encoding heredoc herestring

3
推荐指数
1
解决办法
1317
查看次数

sh - 通过分隔符拆分字符串

s='id;some text here with possible ; inside'
IFS=';' read -r id string <<< "$s"
echo "$id"
Run Code Online (Sandbox Code Playgroud)

错误

restore.sh: 2: restore.sh: Syntax error: redirection unexpected
Run Code Online (Sandbox Code Playgroud)

bash版本 GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)

linux bash sh herestring

2
推荐指数
2
解决办法
8743
查看次数

如何使用$ _.在一个herestring变量?

我似乎无法弄清楚如何在herestring中使用变量,以及稍后在管道命令中扩展变量.我尝试过单引号'和双"引号,以及转义`字符.

我试图将herestring用于Exchange组的列表(例如数组),以及应用于这些组的相应条件列表.这是一个简化的示例,它无法$Conditions正确使用变量(它不会扩展$_.customattribute2变量):

# List of groups and conditions (tab delimitered)
$records = @"
Group1  {$_.customattribute2 -Like '*Sales*'}
Group2  {$_.customattribute2 -Like '*Marketing*' -OR $_.customattribute2 -Eq 'CEO'}
"@

# Loop through each line in $records and find mailboxes that match $conditions
foreach ($record in $records -split "`n") {
    ($DGroup,$Conditions) = $record -split "`t"

    $MailboxList = Get-Mailbox -ResultSize Unlimited
    $MailboxList | where $Conditions
}
Run Code Online (Sandbox Code Playgroud)

variables powershell herestring

2
推荐指数
1
解决办法
284
查看次数

bash 3 和 bash 5 以不同的方式评估此处字符串空白

我有一个脚本:

#!/bin/bash

{ read a
read b
} <<< $(echo a; echo b)

declare -p a b
Run Code Online (Sandbox Code Playgroud)

我把它写到f,做了chmod +x ./f,并期望它bash ./f./f将是相同的。

他们不是:

~/dev/test[1]$ ./f
declare -- a="a b"
declare -- b=""
~/dev/test[2]$ bash ./f
declare -- a="a"
declare -- b="b"
Run Code Online (Sandbox Code Playgroud)

我想通了,bash ./f使用/usr/local/bin/bash它是5.0.16版本,并且./f使用/bin/bash的3.2.57版本。

这些版本之间发生了什么变化以使其评估不同?这是一个已经解决的错误吗?

bash herestring

0
推荐指数
1
解决办法
48
查看次数

使用bash -c比使用here字符串有什么好处

使用bash -c 'some command'过度使用是否有任何实际好处bash <<< 'some command'

他们似乎达到了同样的效果.

linux bash herestring

-2
推荐指数
1
解决办法
147
查看次数