@howie_isaacks The $n parameters inside a function represent the parameters passed to the function, not the parameters passed to the script. If you want to pass all of the parameters passed to your script to a function in your script call the function with the parameter $@ like this:
macOSVersionCheck $@
@howie_isaacks The $n parameters inside a function represent the parameters passed to the function, not the parameters passed to the script. If you want to pass all of the parameters passed to your script to a function in your script call the function with the parameter $@ like this:
macOSVersionCheck $@
Thanks for your help! Maybe I don't understand what you said to do but I added "$@" as you wrote. I have set -x at the beginning of the script so I can see all the commands running. I added in two lines to echo the values in parameters 8 and 9 to see if those values show up. This is what I see:
Running script macOS Version Check Test...
Script exit code: 0
Script result: +/Library/Application Support/JAMF/tmp/macOS Version Check Test:36> macOSVersionCheck / QXJ0PWLY2C HIsaacksJ 15 4
+macOSVersionCheck:3> local osMajor
+macOSVersionCheck:4> local osMinor
+macOSVersionCheck:5> local reqosMajor
+macOSVersionCheck:6> local reqosMinor
+macOSVersionCheck:7> local macOSVersion
+macOSVersionCheck:9> macOSVersion=+macOSVersionCheck:9> /usr/bin/sw_vers
+macOSVersionCheck:9> macOSVersion=+macOSVersionCheck:9> grep ProductVersion
+macOSVersionCheck:9> macOSVersion=+macOSVersionCheck:9> /usr/bin/awk '{ print $2 }'
+macOSVersionCheck:9> macOSVersion=15.4.1
+macOSVersionCheck:10> osMajor=+macOSVersionCheck:10> /usr/bin/sw_vers -productVersion
+macOSVersionCheck:10> osMajor=+macOSVersionCheck:10> awk -F . '{print $1}'
+macOSVersionCheck:10> osMajor=15
+macOSVersionCheck:11> osMinor=+macOSVersionCheck:11> /usr/bin/sw_vers -productVersion
+macOSVersionCheck:11> osMinor=+macOSVersionCheck:11> awk -F . '{print $2}'
+macOSVersionCheck:11> osMinor=4
+macOSVersionCheck:12> reqosMajor=''
+macOSVersionCheck:13> reqosMinor=''
+macOSVersionCheck:14> reqOS=.
+macOSVersionCheck:16> echo 'Parameter 8 is '
Parameter 8 is
+macOSVersionCheck:17> echo 'Parameter 9 is '
Parameter 9 is
+macOSVersionCheck:19> echo 'Required macOS version is .'
Required macOS version is .
+macOSVersionCheck:20> echo 'Current installed macOS version is 15.4.1'
Current installed macOS version is 15.4.1
+macOSVersionCheck:22> [ 15 -ge '' ']'
+macOSVersionCheck:22> [ 4 -ge '' ']'
+macOSVersionCheck:23> echo 'The current installed macOS version is greater than or equal to the required version. No update needed.'
The current installed macOS version is greater than or equal to the required version. No update needed.
+macOSVersionCheck:25> exit 0
Thanks for your help! Maybe I don't understand what you said to do but I added "$@" as you wrote. I have set -x at the beginning of the script so I can see all the commands running. I added in two lines to echo the values in parameters 8 and 9 to see if those values show up. This is what I see:
Running script macOS Version Check Test...
Script exit code: 0
Script result: +/Library/Application Support/JAMF/tmp/macOS Version Check Test:36> macOSVersionCheck / QXJ0PWLY2C HIsaacksJ 15 4
+macOSVersionCheck:3> local osMajor
+macOSVersionCheck:4> local osMinor
+macOSVersionCheck:5> local reqosMajor
+macOSVersionCheck:6> local reqosMinor
+macOSVersionCheck:7> local macOSVersion
+macOSVersionCheck:9> macOSVersion=+macOSVersionCheck:9> /usr/bin/sw_vers
+macOSVersionCheck:9> macOSVersion=+macOSVersionCheck:9> grep ProductVersion
+macOSVersionCheck:9> macOSVersion=+macOSVersionCheck:9> /usr/bin/awk '{ print $2 }'
+macOSVersionCheck:9> macOSVersion=15.4.1
+macOSVersionCheck:10> osMajor=+macOSVersionCheck:10> /usr/bin/sw_vers -productVersion
+macOSVersionCheck:10> osMajor=+macOSVersionCheck:10> awk -F . '{print $1}'
+macOSVersionCheck:10> osMajor=15
+macOSVersionCheck:11> osMinor=+macOSVersionCheck:11> /usr/bin/sw_vers -productVersion
+macOSVersionCheck:11> osMinor=+macOSVersionCheck:11> awk -F . '{print $2}'
+macOSVersionCheck:11> osMinor=4
+macOSVersionCheck:12> reqosMajor=''
+macOSVersionCheck:13> reqosMinor=''
+macOSVersionCheck:14> reqOS=.
+macOSVersionCheck:16> echo 'Parameter 8 is '
Parameter 8 is
+macOSVersionCheck:17> echo 'Parameter 9 is '
Parameter 9 is
+macOSVersionCheck:19> echo 'Required macOS version is .'
Required macOS version is .
+macOSVersionCheck:20> echo 'Current installed macOS version is 15.4.1'
Current installed macOS version is 15.4.1
+macOSVersionCheck:22> [ 15 -ge '' ']'
+macOSVersionCheck:22> [ 4 -ge '' ']'
+macOSVersionCheck:23> echo 'The current installed macOS version is greater than or equal to the required version. No update needed.'
The current installed macOS version is greater than or equal to the required version. No update needed.
+macOSVersionCheck:25> exit 0
Expressed another way, the $N (e.g. $1, $2,...) parameters in your script and in a function defined in your script are not the same parameters so your macOSVersionCheck function had no idea what the values for $8 and $9 which were passed into your script are because they represent the 8th and 9th parameters passed when calling the macOSVersionCheck.
Calling macOSVersionCheck with $@ as a parameter tells the shell to pass all the arguments that were passed to your script when it was called.
Here's a script to demonstrate:
#!/bin/zsh
testFunction() {
echo "In testFunction"
echo "Parameter 2: $2"
echo "Parameter 3: $3"
echo "Parameter 4: $4"
}
echo "In Body"
echo "Parameter 2: $2"
echo "Parameter 3: $3"
echo "Parameter 4: $4"
echo "Calling test function with no prarameters"
testFunction
echo "Calling test function with the \\$@ parameter"
testFunction $@
Save it to disk as ParameterDemo.sh, then run it in Terminal like this:
/Path/To/Scripts/ParameterDemo.sh "Parameter 1" "Parameter 2" "Parameter 3", "Parameter 4"
That should produce the following output:
In Body
Parameter 2: Parameter 2
Parameter 3: Parameter 3
Parameter 4: Parameter 4
Calling test function with no prarameters
In testFunction
Parameter 2:
Parameter 3:
Parameter 4:
Calling test function with the $@ parameter
In testFunction
Parameter 2: Parameter 2
Parameter 3: Parameter 3
Parameter 4: Parameter 4
Expressed another way, the $N (e.g. $1, $2,...) parameters in your script and in a function defined in your script are not the same parameters so your macOSVersionCheck function had no idea what the values for $8 and $9 which were passed into your script are because they represent the 8th and 9th parameters passed when calling the macOSVersionCheck.
Calling macOSVersionCheck with $@ as a parameter tells the shell to pass all the arguments that were passed to your script when it was called.
Here's a script to demonstrate:
#!/bin/zsh
testFunction() {
echo "In testFunction"
echo "Parameter 2: $2"
echo "Parameter 3: $3"
echo "Parameter 4: $4"
}
echo "In Body"
echo "Parameter 2: $2"
echo "Parameter 3: $3"
echo "Parameter 4: $4"
echo "Calling test function with no prarameters"
testFunction
echo "Calling test function with the \\$@ parameter"
testFunction $@
Save it to disk as ParameterDemo.sh, then run it in Terminal like this:
/Path/To/Scripts/ParameterDemo.sh "Parameter 1" "Parameter 2" "Parameter 3", "Parameter 4"
That should produce the following output:
In Body
Parameter 2: Parameter 2
Parameter 3: Parameter 3
Parameter 4: Parameter 4
Calling test function with no prarameters
In testFunction
Parameter 2:
Parameter 3:
Parameter 4:
Calling test function with the $@ parameter
In testFunction
Parameter 2: Parameter 2
Parameter 3: Parameter 3
Parameter 4: Parameter 4
I'm accepting this as the solution because you got me to rethink this whole thing. Note that I was defining these variables as local to the function. Apparently that doesn't work with Jamf parameters. I use local variables in functions when I know I need to keep the logic inside the function but in this case, I don't. I placed reqosMajor and reqosMinor outside of the function and they worked exactly as I need them to.
I'm accepting this as the solution because you got me to rethink this whole thing. Note that I was defining these variables as local to the function. Apparently that doesn't work with Jamf parameters. I use local variables in functions when I know I need to keep the logic inside the function but in this case, I don't. I placed reqosMajor and reqosMinor outside of the function and they worked exactly as I need them to.
reqosMajor and reqosMinor being function local variables is irrelevant. The reason that reqosMajor and reqosMinor did not work inside the function without passing $@ as a parameter is because you were assigning them to the 8th and 9th parameters passed into the function, not those parameters passed to the parent script. Simply calling macOSVersionCheck with no parameters resulted in reqosMajor and reqosMinor being unassigned/empty because there were nothing to assign them to. By setting them outside the function the assignment worked because you were now referencing the 8th and 9th parameters passed into the script.
reqosMajor and reqosMinor being function local variables is irrelevant. The reason that reqosMajor and reqosMinor did not work inside the function without passing $@ as a parameter is because you were assigning them to the 8th and 9th parameters passed into the function, not those parameters passed to the parent script. Simply calling macOSVersionCheck with no parameters resulted in reqosMajor and reqosMinor being unassigned/empty because there were nothing to assign them to. By setting them outside the function the assignment worked because you were now referencing the 8th and 9th parameters passed into the script.
Thanks! I will continue trying this out. I appreciate your help and advice. You have been mentioned in the notes in a lot of my scripts since you've provided so much help over the years.
Thanks! I will continue trying this out. I appreciate your help and advice. You have been mentioned in the notes in a lot of my scripts since you've provided so much help over the years.
We all learn sometime
Many years ago in the early days of Macs I called the publisher of Consulair Mac C to ask why the program I was trying to convert from Pascal to C kept crashing. Bill kindly explained the difference in how Pascal and C differentiated when incrementing a pointer, and then recommended a book on C programming I should read.