below please find a script I use to create a launch agent or launch daemon, depending om how the script variables are customized. I also added logic to get the current logged in user, if you need that. Hope this is helpful.
M
--------------
#!/bin/bash
# launchDaemon and Agent creator
# feed it with;
# launcher type = Daemon or Agent
# launchItem = path to the app being launched, the full path to the executable
# launcherPlistName = unique bit of the plist name. it will be com.mycorp.{unique bit}.plist
# isScriptIn = is this a script yes or no. defines which plist will be used
#1) $4 launcher type = Daemon or Agent
#2) $5 launchItem = path to the app being launched, the full path to the executable or script
#3) $6 launcherPlistName = unique bit of the plist name. it will be com.mycorp.{unique bit}.plist
#4) $7 isScriptIn = is this a script yes or no. defines which plist type will be used
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
launcherTypeIn="Agent"
launchItem="<commandtorunoritremtolaunchgoeshere>"
launcherPlistName="<namegoeshere>"
isScriptIn="no"
# set input strings to lower to avoid case failure
launcherType=$(echo $launcherTypeIn | tr '[:upper:]' '[:lower:]')
isScript=$(echo $isScriptIn | tr '[:upper:]' '[:lower:]')
#echo "$launchItem"
#
case "$launcherType" in
"daemon" )
#echo "**** Daemon ****"
launchFolder="LaunchDaemons"
;;
"agent" )
#echo "**** Agent ****"
launchFolder="LaunchAgents"
;;
*)
echo "**** no launcher type selected. Exiting *****"
exit 1
;;
esac
case "$isScript" in
"no" )
cat << EOF > /Library/${launchFolder}/com.mycorp.${launcherPlistName}.plist
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.mycorp.${launcherPlistName}</string>
<key>ProgramArguments</key>
<array>
<string>${launchItem}</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
;;
"yes" )
cat << EOF > /Library/${launchFolder}/com.mycorp.${launcherPlistName}.plist
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.mycorp.${launcherPlistName}</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>${launchItem}</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
;;
*)
echo "**** no type selected. Exiting *****"
exit 1
;;
esac
/usr/sbin/chown root:wheel /Library/${launchFolder}/com.mycorp.${launcherPlistName}.plist
/bin/chmod 644 /Library/${launchFolder}/com.mycorp.${launcherPlistName}.plist