Skip to content

workaround for stdin

Michał edited this page Aug 26, 2018 · 2 revisions

Possibility to test simple interactive scripts

Let's say you have a simple script that uses stdin (descriptor 0). There is a possibility to create parent script (test.sh) and call your script (test2.sh) with predefined input.

test.sh - redirects "y\n":

#!/bin/bash

source ./test2.sh arg1 <<EOF
y

EOF

test2.sh - consumes redirected input, while debugging:

#!/bin/bash

read -p "Are you sure? " -n 1 -r

if [[ $REPLY =~ ^[Yy]$ ]]
then
    echo "success $1"  # prints success arg1 
fi

This is a workaround, might not work for some cases...