Score:-2

Restrict a user to running a command only two times

cn flag

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?

Jad avatar
br flag
Jad
which command? why would you want to limit them to 2 times?
hariraj avatar
cn flag
I have created a custom command through alias which needs to executed only two times by every user and its my requirement. Please help me.
in flag
Could that custom command not keep a log of who ran it and how many times, then present an error to the user on excessive operations? It's already custom, so why not customise it further?
hariraj avatar
cn flag
my custom command is "alias extend_shutdown_150='bash extend_shutdown.sh 150". I'm not getting the way to implement more on this!
user535733 avatar
cn flag
I know of no existing command with such built-in logic for you to use. You must create it.
Score:1
cn flag

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.
Lorenz Keel avatar
gr flag
I suppose that, instead of piping to `wc`, you can use the `-c` option in `grep`. Is it correct?
vanadium avatar
cn flag
That is even better, of course. Will change it, with thanks.
hariraj avatar
cn flag
How to add this "Have the script log the date and user in a log file each time the command is started."..?
vanadium avatar
cn flag
Aren't you into bash scripting already?
hariraj avatar
cn flag
i'm into bash scripting
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.