0

I have a CSV table with Students of a school which includes firstname, lastname, Day of Entry and Day of leaving.

My CSV-File looks like this: Table of the Students

I already started with the code for adding Users but this script would add a User for everyone in this CSV-File

#!/bin/bash

OLDIFS=$IFS
IFS=";"

while read firstname lastname
do
      useradd -c "${firstname} ${lastname}" -d /home/"${firstname}" -G group1 -s /bin/bash "${lastname}"
done < file.csv

Now I have to code the Bash script so that it reads the day of leaving, compares it to the current date and if the day of leaving is in the future then the script should add a User using the Names in the Tablerow.

Next part is that the script should deactivate every User whose day of Leaving already passed. And after 4 weeks the User should be deleted.

I´m pretty new to bash and I wasn´t able to find anything useable on the Internet. Sorry for my bad English. ^^"

Chloe
  • 5
  • 2
  • 1
    Google is your friend: https://stackoverflow.com/questions/1842634/parse-date-in-bash https://unix.stackexchange.com/questions/84381/how-to-compare-two-dates-in-a-shell – dosas Nov 09 '20 at 13:16

1 Answers1

0

One option is to wrap everything in an awk script:

awk '{
       "date -d "gsub("\\.","-",$4)" +%s" | getline ldat;
       dat=strftime("%s");
       if (ldat>dat) 
                     { system("useradd -c \""$1\" \""$2\"" -d /home/"$1" -G group1 -s /bin/bash "$2) 
       } 
      }' file  

One liner:

awk '{"date -d "gsub("\\.","-",$5)" +%s" | getline ldat;dat=strftime("%s");if (ldat>dat) { system("useradd -c \""$1\" \""$2\"" -d /home/"$1" -G group1 -s /bin/bash "$2) } }' file

With the required data in the file called file, we convert all the "." into "-" with awk's gsub function. We then run this through the date command to get the epoch time and read it into a variable ldat. We then use awk's strftime function to get the epoch date format for todays date. If todays date is less than the leave date, use awk's system function to add the user.

This logic can then be extended to deactivate accounts as required

NOTE - Make sure that the command to be executed with system is as expected before actually execution.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • Thanks! I think that looks good. I will check tomorrow when I´m back at work if it works and will share the results. ^^ – Chloe Nov 09 '20 at 14:51