I am stumped at the error I am getting from a script I'm trying to run to suspend my machine. I am trying to use a regex in an elif statement to suspend my machine after a specific time period.
#!/bin/bash
echo "When would you like to suspend the machine?"
read "sustime"
if [ "$sustime" = "now" ]
then
sudo pm-suspend
elif [[ "$sustime" =~ [0-9]*[smhd] ]]
then
time=`expr "$sustime" : '\([0-9]+)\)'`
ttype=`expr "$sustime" : '.*\([smhd]\)'`
sudo sleep $time$ttype ; sudo pm-suspend
else
echo "Please enter either [now] or [#s|m|h|d]"
fi
The code doesn't work on the elif line, for example if I input 5s, the output of the script is:
$ sh dbussuspend.sh
When would you like to suspend the machine?
5s
dbussuspend.sh: 10: dbussuspend.sh: [[: not found
Please enter either [now] or [#s|m|h|d]
However, it should read that I've entered the string 5s run the code block under elif. I've actually tried any regex in place of [0-9]*[smhd], all with the same error.
[0-9]+[smhd]otherwisehwill be an acceptable input.expr; you can use capture groups in the original regular expression, then access the captured values from the arrayBASH_REMATCH.