标签: here-document

EOF 阻止了我的脚本?

我有这个脚本(它实际上是一个更大脚本的片段)。

#!/bin/sh

 if [ $actualsize -ge $MAXSIZE ]; then
  read dummy FILEPOINT <<EOF
  `dd if=myfile skip=8001 count=1 bs=1|od -x`
  EOF
 fi
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到

./bugged.sh: 9: Syntax error: end of file unexpected (expecting "fi")
Run Code Online (Sandbox Code Playgroud)

显然,问题是“EOF”。为什么会发生这种情况,我如何才能获得相同的结果(从 myfile 中读取)?

bash shell-script here-document

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

Here-document with cat

我是 Linux、shell 脚本的新手,我正在尝试学习基础知识。我目前在重定向章节,在这里文档正在被解释。我无法真正理解下面的代码如何与cat命令结合使用。如果有人可以告诉我吗?

cat << EOF
line1 
line2
line3
EOF
Run Code Online (Sandbox Code Playgroud)

我非常了解<< EOF工作原理。关键字表示输入的结束。我的问题是;cat将文件名作为输入流;以便它可以在输出流上打印其内容。以上如何在内部工作?我们没有在任何地方指定文件名。shell 是否在内部创建了一些临时文件并将其提供给cat?我希望我的问题很清楚。

linux cat here-document

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

grep 与 heredoc 的功能

我想制作从粘贴在终端中的文本进行解析的函数。

$ cat <<eof | grep --color sometext
> foo
> sometext
> sometext1
> a sometext
> asdf
> 
> eof
sometext
sometext1
a sometext
Run Code Online (Sandbox Code Playgroud)

虽然上述工作,我既不能做别名也不能做它的功能。

alias gsi='cat <<eof | grep --color "$1"'
gsi { cat <<eof | grep --color "$1" ; }
Run Code Online (Sandbox Code Playgroud)

我认为在函数定义期间没有执行重定向。

bash here-document bash-functions

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

将变量传递给猫

我在 bash 脚本中只有一个变量,${PHP_V}并试图传入 nginx 配置文件,例如:

cat <<'EOF' > /etc/nginx/sites-available/default
server {
    listen 80 default_server; 
    listen [::]:80 default_server; 

    listen 443 ssl default_server; 
    listen [::]:443 ssl default_server; 

    root /vagrant/webroot; 

    index index.php; 

    server_name _; 

    ssl_certificate /etc/nginx/certs/vagrantbox.crt; 
    ssl_certificate_key /etc/nginx/certs/vagrantbox.key;  

    location / { 
        try_files $uri $uri/ /index.php?$args; 
    } 

    location ~ \.php$ { 
        try_files $uri =404; 
        include fastcgi_params; 
        fastcgi_pass unix:/run/php/php${PHP_V}-fpm.sock; 
        fastcgi_index index.php; 
        fastcgi_intercept_errors on; 
        fastcgi_param SCRIPT_FILENAME 
        $document_root$fastcgi_script_name; 
    }
}
EOF
Run Code Online (Sandbox Code Playgroud)

但没有成功。怎么做?

cat here-document variable

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

警告:此处文档第 2 行由文件结尾分隔(需要“EOF”)

我在这里读了很多,但仍然找不到如何解决这个问题:

steps.wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
    steps.withCredentials([steps.usernamePassword(credentialsId: "test", usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
        steps.sh """                                                      
        curl  --silent -u \${GIT_USERNAME}:\${GIT_PASSWORD} -H "Content-Type: application/json" -X POST https://some.url --data-binary @- <<-EOF
        {
        "state": "$STATE",
        "key": "\$JOB_NAME",
        "name": "\$BUILD_TAG",
        "url": "\$BUILD_URL",
        "description": "$DESCRIPTION"
        }
        EOF"""
Run Code Online (Sandbox Code Playgroud)

它位于詹金斯共享库中。我读过我必须避免空格,但我没有看到我在这里创建了哪些空格。我不断收到此错误:

line 11: warning: here-document at line 2 delimited by end-of-file (wanted `EOF')
Run Code Online (Sandbox Code Playgroud)

bash curl here-document jenkins

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

为什么 bash 将这个 HEREDOC 限制字符串混淆为命令?

我正在尝试在 bash 脚本中使用嵌套的 HEREDOC 块。
笔记

  • 外部 HEREDOC 是用户通过以下方式以不同用户身份运行命令列表 sudo
  • 内部 HEREDOC 正在将常规文本捕获cat到文件中。
# run some commands as regular user
sudo -s -u $reg_user << EOF
echo "installing Pathogen plugin manager"
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

# start the nested HEREDOC
#-----------------------------
#
echo "configuring \.vimrc"
cat <<-VRC >> ~/.vimrc
    "
    "======================================================
    " vim core settings
    "======================================================
    "
    " show numbers - to turn off :set nonumber
    set number

    " always show status …
Run Code Online (Sandbox Code Playgroud)

bash here-document

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

有什么方法可以创建 Here Documents 来替换 bash 中的某些变量而不是其他变量?

我想知道是否有任何方法可以在 bash 中创建一个 here 文档以输出到脚本中的文件,问题是在这个配置文件中我试图输出一些以 $ 开头的变量我想替换,而其他的我做不是。

让我举例说明:

cat >> /opt/apache2/sites-available/$WEBSITENAME <<END-OF-FILE
 ServerName $WEBSITENAME #This I want to replace


<VirtualHost *:80>

 ServerName $WEBSITENAME # And this
 ServerAlias *.xxx.com

RedirectMatch /(.*) https://$WEBSITENAME/$1 # But not the $1 here

 CustomLog /var/log/apache2/access.log combined

</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

这是众多示例之一,我有许多其他配置文件要替换其中的变量,但它们本身有很多自己的变量,因此我不想替换它们。

bash variable-substitution here-document

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

Bash here-documents 和 shebang 行

是否可以得出这样的结论:当使用 bash here-doc like 时bash << HEREDOC,那么总是无一例外,shebang 行 like#!/bin/bash -x是多余的?

如果我必须打赌,我敢打赌是的,它们将是多余的,只能使用我们来组织信息,就像一个标志说新用户“以下命令集最初是从传统脚本执行的,不要不要用双符号或其他类似方式将它们排成一行”。

我想知道专家怎么说 - 使用 here-doc 时,bash shebang 行真的是完全多余的吗?

shell-script here-document shebang

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

here-documents(即&lt;&lt;)用于什么?

来自高级 Bash 脚本指南

特殊符号 << ... 类似于 Interactive-program < command-file

事实上,我能想到的与<<here-document 符号有关的所有事情,我已经可以简单地使用< command-file或 来完成<<< 'list of commands'。也就是说,我可以做到

cat < <(echo fdsa
echo asdf)
Run Code Online (Sandbox Code Playgroud)

或者

cat <<< 'fdsa
asdf'
Run Code Online (Sandbox Code Playgroud)

代替

cat << a
fdsa
asdf
a
Run Code Online (Sandbox Code Playgroud)

一个(明显的)例外是,如果您希望命令列表以不同的方式终止,这取决于您传入的终止参数。但是,我想不出任何可能有用的情况。

另一个例外是,使用 here-document 允许您将特殊字符(例如 ')放入输出中,而无需担心如何转义它们。然而,这也似乎更像是一种便利而不是基本功能。

这里还有什么地方文档如此重要,以至于 Unix 为它们保留了一个特殊的操作符?

bash here-document

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

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

我有这个脚本:

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
查看次数