How to make a loop to read the input from a file part by part?

Hi All,

We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI (installed on centos 7). To achieve this I've created below shell script to launch the workspaces from CLI with least efforts. Please note, the ws-userlist.txt has a list of 60 users, one word per line.

for i in `cat ws-userlist.txt`;do command-to-create-workspaces DirectoryId=<directoryID>,UserName=$i,BundleId=<bundleID>;done

Now, here, there is a limitation of above workspace creation command. It can only build maximum 25 workspaces at one execution. I wonder if there any way to make the loop read the ws-userlist.txt in such a way that it read the first 25 users and take it as input for the command and execute it. once the it get executed, loop should go back to the input file, and read next set of 25 users and execute the command. Once it get executed, loop again go back to input file, and read the last 10 usernames and run the command.
Please note, username count could vary.Sometime I may have a list of 30, sometimes it could be 55.

I've basic knowledge of shell scripting. Need your expert help on this.
Thanks in advance.

This

split --version
split (GNU coreutils) 8.30

seems to have the filter receive the split input lines on its stdin.
Try

split -l25 --filter="command-to-create-workspaces params" ws-userlist.txt 
3 Likes

Thanks RudiC for your reply.

I'll check it today and will let you know.

If your command takes 25 parameters then you can do

xargs -L25 echo "do something with" < ws-userlist.txt

(The echo command is there for demonstration.)
Or introduce a counter in your loop:

cnt=0; for u in `cat ws-userlist.txt`; do echo "do something with $u"; if [ $((cnt+=1)) -eq 25 ]; then echo "do special action"; cnt=0; fi; done
2 Likes

Hi Rudic,

Here is the example command to create workspaces : command-to-create-workspaces DirectoryId=<directoryID>,UserName=<User>,BundleId=<bundleID>

I wonder how can I supply the input to "UserName=" option in the command using the code given by you. UserName option should pick the input from ws-userlist.txt. Please advise.

 split -l25 --filter="command-to-create-workspaces params" ws-userlist.txt 

--- Post updated at 01:27 PM ---

Thanks MadeInGermany for your reply.

Could you please elaborate the code you posted? Thanks.

Hi All. Thanks for your valuable suggestions.

With the help of my colleagues, I'm able to do what I was expecting to do. Here is the code which works for me.

for userid in `awk 'NR==1, NR==25' user-ids.txt`;
do
aws workspaces command to create workspaces  UserName=$userid,BundleId=<bundleID>
done
for userid in `awk 'NR==26, NR==50' user-ids.txt`;
do
aws workspaces command to create workspaces  UserName=$userid,BundleId=<bundleID>
done

Thanks for showing it.
But...
in both loops exactly the same command? That does not make sense.
What is <bundleID> ?