I have a input file called usernames.txt:
user1 group1 group2
user2 group1
user3
What I need to do is take each line, make it an array and with the first element (user) create a file called user.json. I then need to take the remaining elements in the array and use them for the user groups.
The output file(s) should look like this:
{
"id": "user1",
"groups": "group1, group2",
"shell": "\/bin\/bash",
"password": "!"
}
What I have so far:
cat usernames.txt | tr ' \t' '\n\n' | while read user; do
if test -f "$user.json"; then
echo "Skipping \"$user\", it already exists"
else
arr=$user
cat > ${arr[0]}.json << EOF
{
"id": "${arr[0]}",
"groups": "${arr[@]:1}",
"shell": "\/b=in\/bash",
"password": "!"
}
EOF
fi
done
I am not sure how to handle the comma between groups, but I'm sure that's not too difficult.
What I'm currently getting for output:
Skipping "group1", it already exists
group1.json group2.json user1.json user2.json user3.json
It is creating files for the groups which I don't want...
user1.json:
{
"id": "user1",
"groups": "ser1",
"shell": "\/bin\/bash",
"password": "!"
}
It also seems each array element is a letter instead of the whole word? I'm sure it has something to do with how I create the array, but I'm not sure beyond that.
Any ideas how to get this working?