Skip to content

Instantly share code, notes, and snippets.

@danielfsilva88
Created May 20, 2024 02:13
Show Gist options
  • Save danielfsilva88/e92b48996f1344347cfa4c983be3061b to your computer and use it in GitHub Desktop.
Save danielfsilva88/e92b48996f1344347cfa4c983be3061b to your computer and use it in GitHub Desktop.
Bash general content

Execute Linux command from txt

  • Example:
    • Markdown file with text and code together
    • Inside Markdown you have a sequence of lines like:
      1. Use docker compose file in this folder (compose-zoo-kafka-postg.yaml) to set a zoo-keeper, a kafka, and a Postgree DB
      2. To enable this environment, use the command: $ docker compose -f compose-zoo-kafka-postg.yaml up -d
      3. To access the "kafka terminal", use the command: $ docker exec -it kafka bash
      4. To disable this environment, use the command: $ docker compose -f compose-zoo-kafka-postg.yaml down
      
    • Then you can use following commands to execute those commands straight from file:
        $(cat README.md | grep docker | grep up | cut -d "$" -f2)
        $(cat README.md | grep docker | grep down | cut -d "$" -f2)
      
  • Brief explanation:
    • Command cat "print" the file
    • | enable you to add command into the output of the previous command
    • grep enable you to search for a string inside a text and return the lines where this text exists
    • cut -d <character you're looking for> -f2 enable you to get the text after the "character you're looking for"
    • $(<cmd>) enable you to execute the "cmd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment