getting an error executing a bash script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 09-25-2024 09:14 AM
I'm getting the following error when running my script "[student: command not found
/Library/Application Support/JAMF/tmp/student profile". The script checks to see who is lpgged in, and if it is "student" it needs to change the user's profile pic". script below:
"#!/bin/sh
# determines current user
consoleUser=$( /usr/bin/stat -f%Su /dev/console )
if [$consoleUser == "student"]; then
dscl . create /Users/$consoleUser Picture "/Users/Shared/THS_Files/images/Howie.png"
fi
if [$consoleUser == "howardstudent"]; then
dscl . create /Users/$consoleUser Picture "/Users/Shared/THS_Files/images/Howie.png"
fi"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 09-25-2024 09:23 AM
try changing the interpreter from sh to bash.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 09-25-2024 09:28 AM
forgive me I'm new to Jamf, where would I do that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 09-25-2024 09:31 AM
change the #!/bin/sh to #!/bin/bash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2024 09:56 AM - edited 09-25-2024 09:58 AM
All is well. This is a script thing not a Jamf thing. The first line of a macOS script or any Unix script is called the shebang, that is where you specify the interpreter the script will use. Change that first line from #!/bin/sh to #!/bin/bash. You are currently using the sh interpreter; ideally you want to use bash or zsh and occasionally python3 with some osascript (Apple Script) from time to time. The shebang determines what "language" the script needs to be written in, and a script that works with one shebang will not necessarily work with another.
Try the code this way instead.
#!/bin/bash
# determines current user
consoleUser=$( /usr/bin/stat -f%Su /dev/console )
if [$consoleUser == "student"]; then
dscl . create /Users/$consoleUser Picture "/Users/Shared/THS_Files/images/Howie.png"
fi
if [$consoleUser == "howardstudent"]; then
dscl . create /Users/$consoleUser Picture "/Users/Shared/THS_Files/images/Howie.png"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 09-25-2024 10:15 AM
Thanks but I still got the error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2024 10:22 AM - edited 09-25-2024 10:23 AM
@TonyNelson At a minimum you need a space after the [ and before the ] in your if statements. You should also take a look at https://www.shellcheck.net/ as a way to check for common errors in a shell script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 09-25-2024 10:39 AM
wow, shell check sure is neat. may install that locally, thanks for posting that.
after editing the script in shell check, this one seemed to work.
#!/bin/bash
# determines current user
consoleUser=$( /usr/bin/stat -f%Su /dev/console )
if [ "$consoleUser" == "student" ]; then
dscl . create /Users/"$consoleUser" Picture "/Users/Shared/THS_Files/images/Howie.png"
fi
if [ "$consoleUser" == "howardstudent" ]; then
dscl . create /Users/"$consoleUser" Picture "/Users/Shared/THS_Files/images/Howie.png"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 09-25-2024 10:40 AM
Thanks, and Plus one about shellCheck. However I must be missing something as the script ran with no errors but the profile picutre still didn't change.