Restrict a user to running a command only two times [closed]
I want to restrict a user to run a command only two times in Ubuntu.
Specifically, I have created a custom command through an alias which needs to be executed only two times by every user.
My custom command is
alias extend_shutdown_150='bash extend_shutdown.sh 150'
I'm not getting the way to implement more on this!
#!/bin/bash
minutes=$1
echo $minutes >> /usr/bin/input.log
a=`grep -i 15 /usr/bin/input.log | wc -l`
b=`grep -i 30 /usr/bin/input.log | wc -l`
c=`grep -i 60 /usr/bin/input.log | wc -l`
#echo $a $b $c
if [[ "$a" -gt "2" ]] || [[ "$b" -gt "2" ]] || [[ "$c" -gt "2" ]] ; then
echo " Error: User tried to snooze the system more then 2 times"
elif [[ "$minutes" -eq "15" ]] || [[ "$minutes" -eq "30" ]] || [[ "$minutes" -eq "60" ]] ; then
echo " System shutdown is extend $minutes Minutes more"
else
echo " Error: Minutes must be one of 15,30 or 60"
fi
So based on user input the data will be stored into /usr/bin/input.log
so I'm comparing the same file to stop the user from snoozing the system.
Is there any possible way I can append the username & date in /usr/bin/input.log
e.g.: hari 9/2/2021 15
?
Solution 1:
You may implement that yourself in your bash script.
- Have the script log the date and user in a log file each time the command is started.
- Have the script read the log and check whether the date/user combination occurs less than twice (e.g. a
grep -c $PATTERN $FILE
could do: this counts the number of lines in which a certain pattern, e.g. "date + username", occurs) - Continue execution or halt with a message if the number of lines > 2.