0

I have this exercise: Create a "groups" BASH shell script, which takes as an argument a groups_file.txt file structured as follows:

adm:x:4:syslog,adm1
admins:x:1006:adm2,adm12,manuel
ssl-cert:x:122:postgres
alan2:x:1009:aceto,salvemini
conda:x:1011:giovannelli,galise,aceto,caputo,haymele,salvemini,scala,adm2,adm12 
adm1Group:x:1022:adm2,adm1,adm3
docker:x:998:manuel

that:

  1. prints the maximum number of fields on a line in a given file

  2. creates a subdirectory for each group in the file, giving read and write access to users of the groups "adm" and "admins"

  3. creates a file for each subdirectory containing the users belonging to that group line by line

I did the first two points but I don't know how to do the last one. I tried to get the groups equal to $1 with awk and then I redirected them to the "$group/user" file but it doesn't work

#!/bin/bash

echo "Stampo il numero massimo di campi di una linea in un dato file: "
cat groups_file.txt | awk -F "[\t,:]" 'NF>6 {print $0}' 

g=$(cat groups_file.txt  | awk -F :  '{print $1}')

for group in $g
do
        mkdir "$group"
        chmod +rwx "$group"
        
        touch "$group"/users 
        chmod +rw "$group"/users
done
tripleee
  • 175,061
  • 34
  • 275
  • 318
saretta2
  • 37
  • 5

1 Answers1

0

You want to read a line at a time and split it on the colons.

A subdirectory can't belong to more than one group, so you have to come up with a separate mechanism for providing read access to more than one group. I simply make it group adm below; a better solution would perhaps be to create a new group with all the members of adm and admins.

I imagine the "maximum number of fields" means to print the line with the highest number of members.

#!/bin/bash

awk -F "[,:]" 'NF>max { maxline = $0; max=NF }
  END { print max, maxline }' groups_file.txt

while IFS=: read -r group pass gid members; do
    mkdir "$group"
    chown "$group":adm "$group"
    chmod 775 "$group"
    echo "${members//,/$'\n'}" >"$group"/users
done <groups_file.txt

Notice also how this avoids the useless use of cat.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • thanks but from errors, if i wanted to do as i did how can i do? – saretta2 Mar 01 '23 at 12:43
  • Those are English words, but I don't think I understand what you are trying to ask. The fundamental problem with your approach is that you are throwing away all the fields after the first. You could read the file again a second time to obtain the fields you threw away, but that's just a silly and inefficient workaround for not throwing them away in the first place. – tripleee Mar 01 '23 at 12:44
  • Tangentially perhaps see also [don't read lines with `for`](http://mywiki.wooledge.org/DontReadLinesWithFor) – tripleee Mar 01 '23 at 12:48
  • yes sorry I'm Italian, I say that it gives me an error, the first part okay but the while I didn't understand – saretta2 Mar 01 '23 at 12:48
  • The actual error message would be useful. It works reasonably here; see https://ideone.com/q9Ayby – tripleee Mar 01 '23 at 12:53
  • these errors : '/cccc.sh: line 12: syntax error near unexpected token ` '/cccc.sh: line 12: `done – saretta2 Mar 01 '23 at 12:54
  • Are you sure you are running it with Bash? Like the ideone link shows, the syntax is fine – tripleee Mar 01 '23 at 12:55
  • yes, followed up I get this: 12 conda:x:1011:giovannelli,galise,aceto,caputo,haymele,salvemini,scala,adm2,adm12 *** we can't chown or chmod here, so just print the commands : No such file or directoryfile.txt – saretta2 Mar 01 '23 at 12:58
  • The only hypothesis I can come up with is that you have not copy/pasted the code correctly. – tripleee Mar 01 '23 at 13:00
  • I have pasted the same code to three different systems just to check that it's not a portability issue. This works on Mac, Debian, and Ubuntu. – tripleee Mar 01 '23 at 13:02
  • but I don't think that's the expected output – saretta2 Mar 01 '23 at 13:07
  • Maybe you can get back to your professor and ask them to clarify the task, or provide information from earlier lectures for what mechanism exactly you are expected to use to provide permissions to more than one group. – tripleee Mar 01 '23 at 13:08
  • I thought that the purpose of the exercise was to create a folder for each group containing a file with the users – saretta2 Mar 01 '23 at 13:13
  • I can't know which users or groups exist on your system so that part is speculative. If you take out the `chown` the code should do exactly that. – tripleee Mar 01 '23 at 13:17
  • okay but you don't have to work based on the file? – saretta2 Mar 01 '23 at 13:20
  • Demo transcript: https://pastebin.com/RHb1mP87 – tripleee Mar 01 '23 at 13:20
  • yes okay, i don't know why as soon as i run it the first time on my mac it doesn't work....you have been very patient thank you very much and sorry – saretta2 Mar 01 '23 at 13:24