0

I have a text file that has the following format:

group A:

red team: worker 1, worker 2

green team: worker 2, worker 3

Group B:

Team_Blue:worker 4, worker 2

Team_Black: worker 5, worker 6

Team_Grey: worker 6

From this text file, I want to create users and assign them to a group/team which for permission control.

I'm not sure how to go about this within a shell script, The python code snippet below shows the behaviour I'm after in the shell script.


Current attempt:

The file, usersfile.txt, looks like:

Headers - Group : Team : User

usersfile.txt as code block:

    Group_A:Team_Red,Team_Green:worker_1,worker_2 worker_2,worker_3
    Group_B:Team_Blue,Team_Black,Team_Grey:worker_4,worker_2 worker_5 worker_4,worker_6

Code:

with open("usersfile.txt") as file:
user_dict = {}
for line in file:
    line = line.strip()
    cols = line.split(':')
    
    #get columns
    group = cols[0]
    teams = cols[1].split(',')      
    users = cols[2].split(' ')

    #mkdir $group         <--- directory created based off group name
    
    #determine which teams a user is part of
    for idx, members in enumerate(users):
        for member in members.split(','):
            #print(idx, group, teams[idx], member)
            user_dict.setdefault(member,[]).append(teams[idx])
            
#Now we have users and their team, we can create a user account and assign them to a team
print(user_dict)
for user in user_dict:
            print(f"ADDING USER {user} who is part of groups {user_dict[user]}")
            
            # In shell script I would then create users 
            #echo " "
            #echo "ADDING USER: " $user
            #useradd $user -G $teams

output:

{'worker_1': ['Team_Red'], 'worker_2': ['Team_Red', 'Team_Green', 'Team_Blue'], 'worker_3': ['Team_Green'], 'worker_4': ['Team_Blue', 'Team_Grey'], 'worker_5': ['Team_Black'], 'worker_6': ['Team_Grey']}
ADDING USER worker_1 who is part of groups ['Team_Red']
ADDING USER worker_2 who is part of groups ['Team_Red', 'Team_Green', 'Team_Blue']
ADDING USER worker_3 who is part of groups ['Team_Green']
ADDING USER worker_4 who is part of groups ['Team_Blue', 'Team_Grey']
ADDING USER worker_5 who is part of groups ['Team_Black']
ADDING USER worker_6 who is part of groups ['Team_Grey']

If anyone has any suggestions on how to get this exact behavior from a shell script, im all ears.

Zizi96
  • 459
  • 1
  • 6
  • 23
  • 1
    Please [edit] your question and add more details. What do you want to achieve? Print the values in a specific format? Show the expected output as a code block. Or process the data somehow? How exactly? You could use nested loops. Can you use other scripting languages like awk, perl, python? – Bodo Jun 28 '22 at 12:47
  • @Bodo updated with a python surrogate and desired output, does this sufficiently explain what I'm trying to achieve? – Zizi96 Jun 28 '22 at 15:30
  • 1
    Please show the input data as a code block. You can insert a line with 3 tilde characters `~~~` (or alternatively 3 backticks) before and after the text block or indent it by 4 characters. As I understand you want to create users and use the teams as supplementary groups for this user, so you need the username and the teams as arguments for a command. What's the purpose of the groups A and B? Why do you need to replace the Python code with a shell script? Can you use `awk` or `perl`? – Bodo Jun 28 '22 at 16:34
  • @Bodo Flagged 1)the input data. 2)Your understanding is correct. 3)Groups A and B are used to create directories, the permissions of these directories are dictated by a specified team. 4) I have a list of terminal commands I use to setup this shared work environment, I thought putting these commands in a shell script would be a good way to automate. 5) yes, `awk` would be preferable between the 2 . – Zizi96 Jun 28 '22 at 16:53
  • 1
    Please [edit] your question to add requested information or clarification, don't use comments for this purpose. Show the commands you would run manually. This would make it easier for us to propose a solution. You still did not explain why you can't (or don't want to) use your Python solution. You could easily run all commands from a Python script. – Bodo Jun 28 '22 at 16:56
  • the command I would run manually are `useradd $user -G $teams`, where user is `worker_n` and teams is `team_Colour`. So for this example I would run the following in terminal: `$useradd worker_1 -G Team_Red`, `$useradd worker_2 -G Team_Red Team_Green Team_Blue`... `$useradd worker_6 -G Team_Grey`. I didn't realise its possible to execute unix commands through python, I assumed it requires some sort of workaround like downloading and importing libraries which adds complexity/points of failure hence why I prefer a shell script. Do you mind demonstrating what that looks like? – Zizi96 Jun 28 '22 at 17:25
  • **Please don't use comments to provide requested information. [Edit] your question instead.** The commands in your comment show only what you already added as a comment in your Python script, but you don't clarify how you would use the groups A and B to create directories. – Bodo Jun 28 '22 at 17:32
  • Maybe you will find some hints with a search like https://stackoverflow.com/search?q=python+run+shell+commands – Bodo Jun 28 '22 at 17:34
  • @Bodo Apologies, I didn't realise I could mix python with unix commands so I omitted that, I've added the `mkdir` command which makes use of groups to create a directory for that group. Obviously this isn't right as I still need to learn how to leverage unix via python but it gets the idea across. I will be sure to edit questions in response to information requests going forward. – Zizi96 Jun 28 '22 at 17:51

0 Answers0