0

I have a script that is to create a config. file and input some lines into it. But when I run the script, I get the error message:

line 2: filebeat.inputs:
- type: filestream
  id: my-filestream-id
  enabled: true
: bad substitution

Any idea what this means? and how i can fix it? I have shared the script below:

#!/bin/bash
cat > "config.yml" <<EOF
filebeat.inputs:
- type: filestream
  id: my-filestream-id
  enabled: true
  paths:
    - /home/ubuntu/logs/**/*.log
filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 1
setup.kibana:
output.logstash:
  hosts: ["10.09.0.2:5044"]
processors:
  - add_host_metadata:
      when.not.contains.tags: forwarded
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~
EOF
eme
  • 45
  • 5
  • 2
    `path.config` is not a valid parameter in `bash`. – M. Nejat Aydin Sep 14 '22 at 15:13
  • 2
    If you want a literal `${path.config}` in the output then you should quote the `EOF`: `cat > "config.yml" <<'EOF'`, for instance. – M. Nejat Aydin Sep 14 '22 at 15:19
  • What output are you expecting exactly? I'm not very familiar with YAML so I'm not sure what `${path.config}` is supposed to mean. – wjandrea Sep 14 '22 at 15:21
  • 2
    @wjandrea, it's not meaningful in YAML itself at all; it's just an arbitrary string as far as YAML is concerned, but presumably whatever program parses this is going to do further interpretation. – Charles Duffy Sep 14 '22 at 15:43

1 Answers1

-2

Adding a backslash before the $ avoids this problem:

#!/bin/bash
if [ ! -f config.yml ]
then
 cat > config.yml <<EOF
printf "filebeat.inputs:
- type: filestream
  id: my-filestream-id
  enabled: true
  paths:
    - /home/ubuntu/logs/**/*.log
filebeat.config.modules:
  # Glob pattern for configuration loading
  path: \${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 1
setup.kibana:
output.logstash:
  hosts: [\"10.09.0.2:5044\"]
processors:
  - add_host_metadata:
      when.not.contains.tags: forwarded
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~"
EOF
fi
ls -ltr  config.yml

Read more at: Bash - difference between <<EOF and <<'EOF' Assume that I am having following task:

  1. Write a script to create other script.
  2. Other script need to use $PATH instead of using that value. In this case, I need:
printf "\$PATH"

instead of using

printf "$PATH"

Sample command:

$ echo -e "#\041/bin/bash\necho \$PATH" > test.sh;cat test.sh
#!/bin/bash
echo $PATH
$ chmod +x ./test.sh
$ ./test.sh
/usr/bin:/bin:....output

Hence I used $ I tried using printf you can use

  1. echo
  2. cat
  3. printf
  4. awk
  5. any other command or application that redirect output to expected output file. Also complete the task that has been assigned to you. If any of your team mate asking why ask that person: do I need to complete my task and provide related output to client and our product or to find the way to answer your question. If interested to donate, donate at mukeshgct@oksbi
  • 2
    `\$` is important (if you aren't going to use `<<'EOF'`), but why `\"`? And why put a `printf` command _inside your heredoc_, when the OP wants their output to be a YAML file, not a shell script that runs `printf` to create a YAML file? – Charles Duffy Sep 14 '22 at 15:42
  • Even with the edits, I still don't see why you're putting **any** command in your output file. `cat <output` will just copy the contents of your heredoc direct to `output`; you don't need `printf` or anything else inside that heredoc. The output file created by `cat >config.yml` is expected to be _a YAML file_, not a script that can be run to create a YAML file. – Charles Duffy Sep 15 '22 at 23:32
  • Your later demonstration is `>test.sh`, where you're explicitly creating a script (so including a command like `echo` in that script makes sense); but the earlier code is `>config.yml`, not something like `>make-yml-config`. ([bash scripts shouldn't have .sh extensions](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/), but that's a separate discussion). – Charles Duffy Sep 15 '22 at 23:34
  • 1
    Also, for the future, I strongly suggest that you consider replacing `echo -e` with its more portable cousin `printf`. See the APPLICATION USAGE and RATIONALE sections of [the POSIX standard for `echo`](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html), and the [unix.se] question [Why is printf better than echo?](https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo); the excellent answer by Stephane Chazelas there goes into the history behind `echo -e` and why it's so problematic. – Charles Duffy Sep 15 '22 at 23:37
  • 1
    you don't need printf or anything else inside that heredoc. whether this is your doubt or you are asking me question ? I have written my comment to help the problem u are facing when using commands. I tried using sample commands at my directory ~/stackoverflow/73719133 directory to help you on resolving the error you are facing. – murugesan openssl Sep 15 '22 at 23:39
  • See the APPLICATION USAGE and RATIONALE sections of. Yes known. We can see a lot using google/bing/abc url. I have not posted my query here. I have posted to help you to resolve your error you are facing at your terminal. I tried the same using my Linux system here. – murugesan openssl Sep 15 '22 at 23:42
  • instead of writing these comments. Complete your task to get appreciation from your supervisor :) – murugesan openssl Sep 15 '22 at 23:43
  • I'm trying to guide you to write a better answer. The questions are for your benefit, not mine. Note that I am not the OP here, and not the person experiencing the problem to be solved; I'm a third party reviewing and voting on your answer. – Charles Duffy Sep 15 '22 at 23:55