Install automator service via command line
If Automator is closed then open Automator and select Application under Choose a type for your document, then click the Choose button. If it's already open the choose File > New and select Application under Choose a type for your document.

Add a Run Shell Script Action to it.

Copy and paste the shell code you presently have into the Run Shell Script Action.

Add the following lines of code after: open $filename

sleep 1
osascript -e 'tell application "System Events" to click button "Install" of window "Service Installer" of process "Automator"'
sleep 1
osascript -e 'tell application "System Events" to click button "Done" of window "Service Installer" of process "Automator"'


Note: You should also add an appropriate cd line above the rm -rf ./tempWorkflows line you presently have in your code, e.g. cd /some/directory/path where it's the pathname of the location your .workflow files are located for processing by your script. 

Save the Automator Application as Install Automator Service.

Next:

Open System Preferences > Security &amp; Privacy > Accessibility > Privacy

Click the Lock icon to make changes and type in your password, then click the Unlock button.

User the [+] to add the Install Automator Service or drag and drop the application bundle into the window.

This will now allow the Install Automator Service application to run your shell code and use the AppleScript code to click the Install button and subsequent Done button on the Service Installer dialog box in the process of installing your Services.