0

So I am trying to run a script that will capture the output of the command:

 echo 'password' | sudo -S strace -p14750 -s9999 -e write

for about 5 seconds and then store the output into a variable. How can I do this?

Full script:

    appium_pid_output=$(echo 'password' | sudo -S strace -p$appium_device_pid -s9999 -e write)
    echo 'captured output of node '$appium_pid_output

    if [[ $appium_pid_output == *POST* ]]; then
        echo "device [ " + $device + " ] is currently in use"
        return 1
    fi
user1607549
  • 1,499
  • 3
  • 13
  • 17

1 Answers1

0

Use the timeout command.

var="$(timeout 5 whateverProducesTheOutput)"

A drawback is that timeout doesn't work well with complex commands. The easiest way around this is to write a function and execute the function with a timeout (see this answer).

foo() {
    whateverProducesTheOutput
}
export -f foo
var="$(timeout 5 bash -c foo)"
Community
  • 1
  • 1
Socowi
  • 25,550
  • 3
  • 32
  • 54