2

I have 2 scripts created. The first one called "lock_logoff" and it does just that. It locks the account in the script and then logs them off. The second script unlocks the account. I got tire of going over to my daughter's computer and doing this by hand when she gets grounded from the computer.

Lock script:
sudo passwd -l amber
sudo pkill -u amber
echo "Account locked and logged off! Screaming in 3....2....1..."

Unlock script:
sudo passwd -u amber
echo "Account unlocked!"

What I am trying to do, since there are more than 1 account on this computer, I would like to add a variable to the script so I do not have to have 3 different lock and unlock scripts. Is there a way I can run "lock_logoff jon" or "unlock amber" and it runs the script for that user?

1
  • It never fails. If I search for what I am trying to do and cannot find it, I will find what I am looking for within moments of posting a question. Here is my lock script: read -p "who to lock?" name sudo passwd -l $name sudo pkill -u $name echo "$name has been locked. Prepare for screaming in 3.....2.....1..."
    – KC5SDY
    Aug 18, 2015 at 1:52

1 Answer 1

1

You absolutely can use only one script instead of 3. For that, you could just pass arguments from the command line. In every bash scripts and functions, some variables called positional parameters are automatically generated. At the beginning of your script (i.e. when no other command has been executed yet), you will have :

$0 : the name of the script
$1 : the 1st argument passed
$2 : the 2nd argument ...

Note that those arguments are affected by your IFS & your quotation, so john doe will count as 2 arguments whereas "john doe" will count as one. You can read more about it here

Once you've read the arguments, you will also have to ensure that the user really exists (i.e. you didn't call your script with a typo). To do that, you could use the following code snippet :

#!/bin/bash
user_name="$1"

#here, id returns 'true' if the user exists, else 'false' 
#>/dev/null 2>&1 hides the output of the id command
if id -u "$user_name" >/dev/null 2>&1; then
    sudo passwd -l "$user_name"
    sudo pkill -u "$user_name"
    echo "Account locked and logged off! Screaming in 3....2....1..."
else
    echo "Error: user $user_name does not exist"
fi

You can now simply call your script like this : ./myscript amber or ./myscript john

3
  • 1
    Excellent. This can of course be expanded with an unlock/lock option. "./myscript amber lock" and "./myscript amber unlock" making it 1 script in total.
    – Rinzwind
    Aug 18, 2015 at 10:05
  • Thank you!!! That is much more than I would have cobbled together myself. I just got threw taking a programming fundamentals class last semester and it is starting to come back to me.
    – KC5SDY
    Aug 19, 2015 at 1:39
  • @KC5SDY Glad it worked for you :) If it helped, don't hesitate to accept the answer by clicking the green mark next to it in order to close your question.
    – Aserre
    Aug 19, 2015 at 3:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .