I'm making a shell script to limit the amount of time someone spends on a program but it gives me these errors:
./time_limit: line 26: $Log: ambiguous redirect
./time_limit: line 16: [: root: unary operator expected
Here is the code:
#!/bin/bash
config (){
ImageViewer=/ 2> /dev/null
AllowedTime=30 #in minutes
AllowedPlays=1
cmd=sol #for demonstration, I use sol installed by default in Ubuntu. Set to your liking
AllowedUser=root #Set to your liking.
ImageViewer=eog #I set this to the Eye of GNOME image viewer. If no GUI or if you don't want an explosion (really?) comment out.
#If you have another desktop change to yor image viewer.
Log=/dev/null #if you want to log this, set to file of your liking
#but make sure you have write permission.
config
date=$(date)
}
if [ $USER = $AllowedUser ]
then
echo "ACSESS ALLOWED for $AllowedTime minutes."
at now + $AllowedTime minutes <<< "killall $cmd; $ImageViewer ../files/Explode.gif"
echo "Session 1: $date" >> $Log
$cmd
exit
fi
echo "ACSESS DENIED!"
echo "This UNAUTHORIZED USE has ben recorded."
echo "Violation by $USER $date" >> $Log
I ran it though ShellCheck and it seems fine. Can anyone see the problem?
$USER
isn't quoted, but not the ones about$Log
. I think what happens is that it sees the constant assignmentLog=/dev/null
, and figures that's safe even if unquoted. But it doesn't realize the function with that assignment never runs... Comment out the assignment, and it complains about using an unset variable, and reminds about quoting it. For once, not a straight missing quotes issue :)config
but it does that from within the function itself. If the function was ever called in that form, it'd nest the function call endlessly, and e.g. Bash just crashes if you do that... (zsh and ksh seem smart enough to just detect and complain about recursing too deep without crashing)