You need to add the shebang in scripts
#!/bin/sh
defaults write com.apple.screencapture type jpg
The shebang isn't actually the issue believe it or not. I have some older scripts in our JSS that don't have them and they run fine, since Jamf Pro understands they are scripts and assumes bash if it doesn't see a specific interpreter.
The problem is you cannot run defaults commands that don't use the full path to a plist, or are not run by the user (scripts run as "root" by default) and expect them to work on the user space. When you find defaults commands online that use the syntax defaults write com.org.domain somesetting
these are expected to be run as the user. Since the script runs as root, it doesn't do what you expect since it's actually setting the screencapture setting (in this instance) for root instead.
You can solve this by capturing the username and directing the defaults command to run as the user
#!/bin/sh
currentuser=$(stat -f%Su /dev/console)
sudo -u $currentuser defaults write com.apple.screencapture type jpg
or, capture the username and direct the defaults command at the user's plist file and then correct permissions on the plist
#!/bin/sh
currentuser=$(stat -f%Su /dev/console)
defaults write /Users/$currentuser/Library/Preferences/com.apple.screencapture.plist type jpg
chown $currentuser /Users/$currentuser/Library/Preferences/com.apple.screencapture.plist
Edit: Meant to say that it is best to always include a shebang in any script, as it removes any guesswork and potential problems that can arise from not having them there, so I do think you should include them. Just saying that even WITH the shebang, it won't solve the main problem.
Thank you both so much. I will try this today on some of my bash scripts!
@derek.ritchison Have a look at this.
It's similar to what you're trying to do, so might help. :)