getting an error executing a bash script

TonyNelson
New Contributor

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"

8 REPLIES 8

AJPinto
Honored Contributor III

try changing the interpreter from sh to bash.

forgive me I'm new to Jamf, where would I do that?

change the #!/bin/sh to #!/bin/bash

AJPinto
Honored Contributor III

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

 

 

 

 

Thanks but I still got the error.

sdagley
Esteemed Contributor II

@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.

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

 

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.