Execute complex command from bash script

Alexander Zhak

I'm working with kafka and I want to monitor topics. Basically I output several topics to stdout and to file for later review. For single topic monitoring I came up with the following command:

${KAFKA_HOME}/bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic mytopic | \
    while IFS= read -r line; do 
      printf '[%s | %20s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "mytopic" "${line}";
    done | tee -a kafka.out

However, there're many topics to consume, so I'm trying to do something like

consumer=${KAFKA_HOME}/bin/kafka-console-consumer.sh
mapfile -t topics < <(${KAFKA_HOME}/bin/kafka-topics.sh --zookeeper localhost:2181 --list)
for i in ${topics[@]}; do
  xterm -T ${i} -e "my_command" &
done
tail -F kafka.out

where my_command is the above command. It doesn't work. I suspect I'm messing up with quotes, unfortunately I can't figure out how to apply them correctly

Stéphane Chazelas

If you want to store some text verbatim, it's usually easier with this syntax:

mycommand=$(cat << 'EOF'
"${KAFKA_HOME}/bin/kafka-console-consumer.sh" --zookeeper localhost:2181 --topic "$TOPIC" |
  while IFS= read -r line; do 
    printf '[%s | %20s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$TOPIC" "${line}";
  done | tee -a kafka.out
EOF
)

In ksh93, bash or zsh, you can also make it:

mycommand=$(<<'EOF'
....
EOF

In ksh93 that optimises out the exec of cat and the fork, while with bash only the exec (in zsh, it makes no difference).

In mksh:

mycommand=<<'EOF'
...
EOF

And then:

export TOPIC KAFKA_HOME
for TOPIC in "${topics[@]}"; do
  xterm -T "$TOPIC" -e "$mycommand" &
done

Here, I'd use ts to time-stamp the input (or do it in gawk or perl if ts is not available) instead of that very inefficient while read loop.

ts '[%F %T | mytopic]'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute complex bash script within Python

From Dev

Execute sed command in a bash script

From Dev

BASH script - execute command in variable

From Dev

Bash Script execute command with argument

From Dev

Bash script doesn't execute command from PATH

From Dev

Cannot execute an AWS IOT command formed from a bash script

From Dev

Bash script doesn't execute command from PATH

From Dev

Is it possible to run/execute a bash script command from inside a python code?

From Dev

Execute a bash script from the command line which requires an argument with spaces in

From Dev

Execute command with sudo and execute Bash script with sudo

From Dev

Bash script can't execute Go command

From Dev

Bash script to monitor file change and execute command

From Dev

How to execute SQL command with parameters in bash script

From Dev

Bash Script execute more than one command

From Dev

How to execute custom command in bash script

From Dev

How to execute a command within a bash script?

From Dev

Bash script to monitor file change and execute command

From Dev

Bash script that execute command only if port is free

From Dev

Bash script for execute one command for many files

From Dev

how to execute a bash command in a python script

From Dev

Bash script with "execute" command and strange regexp

From Dev

Bash script to monitor file change and execute command

From Dev

bash script to read my files and execute the command

From Dev

Execute command with backquote in bash shell script

From Dev

Bash script builds correct $cmd but fails to execute complex stream

From Dev

Bash script builds correct $cmd but fails to execute complex stream

From Dev

Sed command does not execute correctly when called from bash script and works fine when called from prompt

From Dev

Execute Perl Script from Bash script

From Dev

How do I execute a xfconf-query command from a bash script launched with sudo?

Related Related

  1. 1

    Execute complex bash script within Python

  2. 2

    Execute sed command in a bash script

  3. 3

    BASH script - execute command in variable

  4. 4

    Bash Script execute command with argument

  5. 5

    Bash script doesn't execute command from PATH

  6. 6

    Cannot execute an AWS IOT command formed from a bash script

  7. 7

    Bash script doesn't execute command from PATH

  8. 8

    Is it possible to run/execute a bash script command from inside a python code?

  9. 9

    Execute a bash script from the command line which requires an argument with spaces in

  10. 10

    Execute command with sudo and execute Bash script with sudo

  11. 11

    Bash script can't execute Go command

  12. 12

    Bash script to monitor file change and execute command

  13. 13

    How to execute SQL command with parameters in bash script

  14. 14

    Bash Script execute more than one command

  15. 15

    How to execute custom command in bash script

  16. 16

    How to execute a command within a bash script?

  17. 17

    Bash script to monitor file change and execute command

  18. 18

    Bash script that execute command only if port is free

  19. 19

    Bash script for execute one command for many files

  20. 20

    how to execute a bash command in a python script

  21. 21

    Bash script with "execute" command and strange regexp

  22. 22

    Bash script to monitor file change and execute command

  23. 23

    bash script to read my files and execute the command

  24. 24

    Execute command with backquote in bash shell script

  25. 25

    Bash script builds correct $cmd but fails to execute complex stream

  26. 26

    Bash script builds correct $cmd but fails to execute complex stream

  27. 27

    Sed command does not execute correctly when called from bash script and works fine when called from prompt

  28. 28

    Execute Perl Script from Bash script

  29. 29

    How do I execute a xfconf-query command from a bash script launched with sudo?

HotTag

Archive