P
P
Pinkman2021-05-23 17:29:19
bash
Pinkman, 2021-05-23 17:29:19

How to run a set of commands from a variable in a bash script?

Good evening! There is a small script that should check the output of two programs (the standard shell and my shell): here is the script

#!/bin/bash
a=0

function test {
  ./minishell -c "$cmd" > dest
  ($cmd) > src
  echo "Test: " "$cmd"
  diff dest src >> diff
  if [ "$?" -eq "$a" ]
  then
    echo -e "\033[37;20;42m  OK \033[0m"
  else
    echo -e "\033[37;40;41m  KO \033[0m"
  fi

}

rm -f diff
cmd='echo 12'
test
cmd='echo 12 | cat -e'
test

The output is:
Test: echo 12
OK
Test: echo 12 | cat -e
KO
My program output:
$12
bash output:
12 | cat -e

How to feed bash a variable so that it reads it as different commands, and not as arguments to echo?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pinkman, 2021-05-23
@famousman204

Found this option:
replace ($cmd) > src with echo "$cmd" | bash > src
and everything worked as it should

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question