How to create and assign folder to a group in nextcloud in a bash file?

Here is my problem, I wrote some code to get all groups in my nextcloud and automatically create folders for these groups in the admin account. I would now like to ensure that the folders created with the name of the groups are shared with the groups. Here my code :

#! /bin/bash

#Sync nextcloud folders via Groups.
cd /WAY_TO_NEXTCLOUD/html/nextcloud/

#Get users groups list.
php occ group:list > /tmp/usersBYgroups.txt

#Clean and format for 'mkdir -p'.
sed '/    - /d' /tmp/usersBYgroups.txt > /tmp/groupsWusers.txt
sed 's/  - //' /tmp/groupsWusers.txt > /tmp/groupsWuseless.txt
sed 's/.$//' /tmp/groupsWuseless.txt > /WAY_TO_LOG/groups.txt
sed 's/-/\//' /WAY_TO_LOG/groups.txt > /WAY_TO_LOG/groupsCreator.txt

#Create groups folders in Nextcloud and NFS.
cd /WAY_TO_NEXTCLOUD/html/nextcloud/data/admin/files/
xargs mkdir -p < /WAY_TO_LOG/groupsCreator.txt
cd /WAY_TO_NFS/
xargs mkdir -p < /WAY_TO_LOG/groupsCreator.txt

#Create logs.
cd /WAY_TO_SCRIPTS/
mv ./groups/groupsCreator.txt ./diff/groups_"`date +"%H-%d-%m-%Y"`".txt
diff ./diff/groups_$(date -d '-1 hour' +'%H-%d-%m-%Y').txt ./diff/groups_"`date +"%H-%d-%m-%Y"`".txt$

#Scan new files.
cd /WAY_TO_NEXTCLOUD/html/nextcloud/
php occ files:scan admin