So I have an array full of commands, I want to run each each element and output the commands output to the screen and also check if there's an error in each command and echo "Command failed". I can't seen to find a way to do this programmatically I basically want to run the commands without the command's errors output flooding the screen.
example:
array=(
"cat something"
"grep something"
"rm something"
"read -r -p 'something' something"
)
length=${#array[@]}
for (( i=1; i<${length}+1; i++ ));
do
if echo ${array[$i-1]} | sh 2>/dev/null; then
echo "command succeded"
else
echo "command failed"
fi
done