eme
eme

Reputation: 45

File creation and Editing Script returns Bad Substitution Error

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

Upvotes: 0

Views: 871

Answers (1)

murugesan openssl
murugesan openssl

Reputation: 207

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.

Upvotes: -3

Related Questions