I am totally new to bash scripting. I am wondering if there’s a way that I can create a reference for the following code block:
read -t5 -n1 -r -p 'Press any key or wait five seconds...' key if [ "$?" -eq "0" ]; then echo 'A key was pressed.' else echo 'Five seconds passed. Continuing...' fi
Is there a way I can give this block of code a reference point and execute it using the reference several times throughout the rest of the script?
Would it be easier/better to create a separate script using this particular code and then reference it in the rest of the script I’m writing using include? e.g. Bash: How _best_ to include other scripts?
Thanks!
Advertisement
Answer
Yes, there is a way to do that: Make it a function.
ReadKey() { read -t5 -n1 -r -p 'Press any key or wait five seconds...' key if [ "$?" -eq "0" ]; then echo 'A key was pressed.' else echo 'Five seconds passed. Continuing...' fi }
And then call it:
ReadKey