Last active
January 20, 2019 14:57
-
-
Save tisaconundrum2/975841363cbf3fc3f6a70ad2252703fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You can follow the steps below (not the only way): | |
#[x] 1. Using the commands who and cut, extract the list of usernames currently | |
# logged in the system | |
#[x] 2. To check after a minute, the sleep command can be used: sleep 60 | |
#[x] 3. Get the new list of users logged in after a minute | |
#[x] 4. For each user in the list; | |
#[x] 1. check if he is in the second list using if and grep. You can dump the | |
# output of grep to /dev/null when using it in the as a test for the if | |
# statement | |
#[x] 2. If the user is not in the second list, print that the user has logged out | |
#[x] 5. For each user in the second list, | |
#[x] 1. If he is in the first list, print that the user has logged in | |
#[x] 6. Rename the second list as the first list | |
#[x] 7. Repeat the process from step 2 | |
# Example: | |
# jcantre5 logged in | |
# ramdisk logged in | |
# dugger logged in | |
# ngfinch logged in | |
# yucanyu logged in | |
# ebrody logged out | |
# ybudiya logged in | |
# cdchapm2 logged in | |
mapfile -t first_list < <(who | cut -d" " -f1) | |
while true; do | |
sleep 60 | |
mapfile -t secon_list < <(who | cut -d" " -f1) | |
for user in ${first_list[*]}; do | |
valid=$(echo -e "${secon_list[*]}" | grep -Ec "${user}") | |
if [ "$valid" == 1 ]; then | |
echo "${user} logged in" | |
else | |
echo "${user} logged out" | |
fi | |
done | |
for user in ${secon_list[*]}; do | |
valid=$(echo -e "${first_list[*]}" | grep -Ec "${user}") | |
if [ "$valid" == 1 ]; then | |
echo "${user} logged in" | |
else | |
echo "${user} logged out" | |
fi | |
done | |
first_list=${secon_list[*]} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment