标签: here-string

寻找匹配的“)”时出现意外的 EOF

我只是想从 sql 语句中获取输出并存储在 bash 变量中。我在寻找匹配的‘)’时遇到“意外的 EOF”错误。我不明白我做错了什么。为什么我收到这个错误?

var=$($ORACLE_HOME/bin/sqlplus / as sysdba <<EOF  
select status from v\$instance;
exit;
EOF
)
Run Code Online (Sandbox Code Playgroud)

bash sql here-string

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

字符串和文件之间的差异

基本上我想检查前后相同文件的差异 sed

试图运行:

diff /opt/postTrades.sh <<< $(sed 's/1\ MIN/10\ MIN/g' /opt/postTrades.sh)
Run Code Online (Sandbox Code Playgroud)

diff <<< $(sed 's/1\ MIN/10\ MIN/g' /opt/postTrades.sh) < /opt/postTrades.sh 
Run Code Online (Sandbox Code Playgroud)

diff <<< (sed 's/1\ MIN/10\ MIN/g' /opt/postTrades.sh) < /opt/postTrades.sh
Run Code Online (Sandbox Code Playgroud)

总是得到:

diff: missing operand after '/opt/postTrades.sh'
diff: Try 'diff --help' for more information.
Run Code Online (Sandbox Code Playgroud)

正确的做法是什么?谢谢。

diff here-document here-string

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

单行 heredocument - 在 Bash 中可能吗?

我希望cat在一行中运行一个heredocument,而不是 3 行的自然语法(开场白、内容和分隔符)。我这样做的需要主要是美观,因为重定向的内容旨在成为手册文本文件的一部分,我想在该特定文件中尽可能多地保存行)。

cat <<< TEST > ~/myRep/tiesto tiesto TEST(我通常会分成 3 部分)会导致错误:

tiesto: 没有这样的文件或目录

测试:没有这样的文件或目录。

甚至可以在 Bash 中执行一行 heredocuments 吗?

here-document here-string

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

管道生产者如何告诉管道消费者它已到达“文件结尾”?”(未命名管道,而不是命名管道)

我有一个应用程序,它要求生产者将文件名发送给消费者,并让生产者在发送最后一个文件名并到达文件末尾时向消费者指示。

为简单起见,在以下示例中, 使用和演示生产者,而使用 演示消费者。我试图推断“此处文件”方法但没有成功,用于向生产者包装器(如果存在此类东西)指示要查找的内容作为文件结尾的指示。如果它工作应该从输出中过滤。echoprintfcat<<EOFcatEOF

例 1)

输入

{
echo "Hello World!" 
printf '\x04' 
echo "EOF"
} <<EOF |\
cat
Run Code Online (Sandbox Code Playgroud)

输出

bash: warning: here-document at line 146 delimited by end-of-file (wanted `EOF')
Hello World!
EOF
Run Code Online (Sandbox Code Playgroud)

例 2)

输入

{ 
echo "Hello World!" 
printf '\x04' 
echo "EOF"
} |\
cat <<EOF
Run Code Online (Sandbox Code Playgroud)

输出

bash: warning: here-document at line 153 delimited by …
Run Code Online (Sandbox Code Playgroud)

pipe here-document here-string

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

为什么带有命令替换 herestring 的 Bash 'while' 读取循环不读取整个输入?

看看这个非常基本的 Bash 脚本:

#!/bin/bash
while read l
do
  echo $l
  echo "next one"
done <<< $(ps aux)
Run Code Online (Sandbox Code Playgroud)

我的计算机上有多个进程,该ps aux命令在终端中运行良好。

我在循环中只得到一次迭代,ps命令输出在同一行给出。为什么它不能按预期工作?

bash command-substitution read here-string

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

反引号或此处字符串或读取在 RHEL 8 中未按预期工作

我正在尝试将 python 脚本的输出重定向为交互式 shell 脚本的输入。

测试文件

print('Hello')
print('world')
Run Code Online (Sandbox Code Playgroud)

说 test.py 如上所述打印“Hello world”,它使用Here 字符串重定向提供给两个变量,如下所示

交互式脚本read a b <<< `python3 test.py`

这在 Rhel 8 服务器中没有按预期工作,而在 Rhel 7 中工作正常

雷尔8:

tmp> read a  b  <<< `python3 test.py`
tmp> echo $a $b
Hello
tmp> cat /etc/redhat-release
Red Hat Enterprise Linux release 8.3 (Ootpa)
Run Code Online (Sandbox Code Playgroud)

变量b在rhel 8中为空

雷尔7:

tmp> read a  b  <<< `python3 test.py`
tmp> echo $a $b
Hello world
tmp> cat /etc/redhat-release
Red Hat Enterprise Linux …
Run Code Online (Sandbox Code Playgroud)

bash rhel io-redirection read here-string

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

如何使用地图文件

我有一些类似的代码:

while read -r col1 col2 col3 col4 col5 col6 col7 col8 TRASH; do 
        echo -e "${col1}\n${col2}\n${col3}\n${col4}\n${col5}\n${col6}\n"
done< <(ll | tail -n+2 | head -2)
Run Code Online (Sandbox Code Playgroud)

(我实际上并没有使用ls/ll但我相信这个编辑过的示例显示了我遇到的相同问题)

问题是如果ll | tail -n+2 | head -2失败,我需要一个条件语句,所以我试图创建一个映射文件,然后read在脚本中通过它。地图文件被正确创建,但我不知道如何重定向它以便正确读取。

代码

if ! mapfile -t TEST_ARR < <(ll | tail -n+2 | head -2); then
        exit 1
fi
while read -r col1 col2 col3 col4 col5 col6 col7 col8 TRASH; do 
        echo -e "${col1}\n${col2}\n${col3}\n${col4}\n${col5}\n${col6}\n"
done<<<"${TEST_ARR[@]}"
Run Code Online (Sandbox Code Playgroud)

地图文件内容

declare -a TEST_ARR=( …
Run Code Online (Sandbox Code Playgroud)

bash here-string

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

在此处字符串中使用变量

为了向交互式脚本提供输入,我一直在使用此处的字符串:

script <<< $'1 2\n3 4\n5 6\nq'
Run Code Online (Sandbox Code Playgroud)

这有效地进入

1 2
3 4
5 6
q
Run Code Online (Sandbox Code Playgroud)

进入脚本。但是如何用变量替换其中之一?

script <<< $'$var 2\n3 4\n...'
Run Code Online (Sandbox Code Playgroud)

不起作用...

shell scripting bash here-string

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

如何在此处的文档中连接结果?

我有这个脚本:

while read $item;
do
    # Some bash logic here
done <<< "$({ cat /path/to/some/file; echo; })"
Run Code Online (Sandbox Code Playgroud)

现在我还想用来find查找一些目录的名称,并将其与循环一起cat传递while

换句话说,我想:

  • cat一份文件
  • find一些目录
  • 连接结果
  • 将它们作为此处文档送入while循环

我被困在这一点上。

我想到了重复while逻辑,这不是一个好方法。我还考虑while使用函数重用循环内的逻辑。

然而,连接此处文档中的内容似乎是正确的方法。

bash find cat here-document here-string

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