-5

This script is used to adding new ufw rules with the container name without re-typing the container name again. It uses grep to get container name.

This is the bash script:

#!/bin/bash
echo -e "\e[92m[Docker-compose] Deleting old ufw rules...\e[0m"
sudo ufw-docker delete allow ${grep container_name: ./docker-compose.yml | awk '{print $2}'}
echo -e "\e[92m[Docker-compose] Successfully deleting old ufw rules.\e[0m"
sleep 3
echo -e "\e[92m[Docker-compose] Adding new ufw rules...\e[0m"
sudo ufw-docker allow ${grep container_name: ./docker-compose.yml | awk '{print $2}'}
echo -e "\e[92m[Docker-compose] Successfully adding new ufw rules.\e[0m"

What's the right way to execute this bash script?

4
  • 2
    Where did you get the command? What is it supposed to do? What have you done so far, that you ended up with this? Doesn't seem like a valid meaningful command...
    – d.c.
    Nov 16 at 0:16
  • 1
    extract the individual commands and pipelines, and run them on their own. Are you happy with the results? Always paste your script into https://shellcheck.net, a syntax checker, or install shellcheck locally. Make using shellcheck part of your development process.
    – waltinator
    Nov 16 at 0:22
  • Well, the command for getting container name is ${grep container_name: ./docker-compose.yml | awk '{print $2}'} Does it work by itself? e. g. echo ${grep container_name: ./docker-compose.yml | awk '{print $2}'}
    – d.c.
    Nov 16 at 0:23
  • It's difficult to understand what you mean. You say that you failed? How did you fail? Was there an error message? Or did it just not run? Edit your question and provide more details. Nov 16 at 3:24
3

Where did you get that script from? Why does it appear to be attempting command substitution with ${ ... } (which will just get a "bad substitution" error from bash) instead of $( ... ). Try it with:

$(grep container_name: ./docker-compose.yml | awk '{print $2}')

Also, you don't need grep when you're using awk (awk can do its own regex matching). That would be better written as:

$(awk '/container_name:/ { print $2 }' ./docker-compose.yml)
0

To run the script, either:

  • Set the file's execute permissions:

    chmod 755 <filename>
    

    and then run it

    ./<filename>
    
  • Call it using as an argument to bash, no need to set the permissions first:

     bash <filename>
    

    To see each line execute, use -x

     bash -x <filename>
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.