0

I need to extract from a list of users (2 columns on a csv file) -- user & id -- and parse this using a bash script -- then in same script -- use the extracted config to create users using an AWS CLI command on AWS Transfer Family server.

I have tried to tailor scripts what I have found online to what I need to do but have very little success when searching for a similar senario to apply. Just struggling to get the script to insert the data from the csv into the right part of the command.

The contents of the csv are:

[root@ip-10-10-6-11 users]# cat us_users.csv
user,id,,,,,
TEST_USER,1234,,,,,

I have just put one user in presently for testing purposes - but have approx 400+ to add.

Script is:

#!/bin/bash

IFS="user,id"
while read -r -a fields; do
echo ${fields[0]}${fields[1]},${fields[2]}
/usr/local/bin/aws transfer create-user --role arn:aws:iam::1234567890:role/SFTP-EFS-Role --server-id s-************** --user-name ${fields[1]} --home-directory-type LOGICAL --home-directory-mappings '[{"Entry":"/","Target":"/fs-**************/${fields[1]}"}]' --posix-profile Uid=${fields[2]},Gid=${fields[2]} --region us-east-1
done < /root/users/us_users.csv

Output after running script:

[root@ip-10-10-6-11 scripts]# ./add_sftp_users_from_csv.sh /root/users/us_users.csv
,
/usr/local/bin/aws transfer create-user --role arn:aws:iam::1234567890:role/SFTP-EFS-Role --server-id s-************** --user-name  --home-directory-type LOGICAL --home-directory-mappings '[{Entry:/,Target:/fs-**************/}]' --posix-profile Uid=,Gid= --region us-east-1
TEST_USER1234
/usr/local/bin/aws transfer create-user --role arn:aws:iam::1234567890:role/SFTP-EFS-Role --server-id s-************** --user-name 1234 --home-directory-type LOGICAL --home-directory-mappings '[{Entry:/,Target:/fs-**************/TEST_ID}]' --posix-profile Uid=,Gid= --region us-east-1

Despite the bottom half of the putput suggesting it had created the user (albeit using the id '1234' instead of what should be there 'TEST_USER' -- the user isnt added to AWS Transfer server)

Manually running the AWS CLI command works fine -- but with the large number of users, I wanted to avoid running the user specific commands 400+ times.

Just looking for some guidance on this if possible -- or whether there is a preferred method to perform this task?

Craig
  • 11
  • 3
  • 1
    `IFS="user,id"` is not working the way you think it is. It will treat any character in the IFS value as a field separator. – Aserre Jun 29 '22 at 15:51
  • Also, you are just doing `echo "/usr/local/bin/aws ...`. Why would you expect the user to be created ? You are just printing the command on the screen, not executing it – Aserre Jun 29 '22 at 15:53
  • 1
    There are several other candidates, but here is a probable duplicate : https://stackoverflow.com/questions/37173774/how-to-read-columns-from-csv-file-into-array-in-bash – Aserre Jun 29 '22 at 15:57

0 Answers0