Posted on 03-14-2024 04:53 AM
I'm not actually having a problem with something but I was playing around with being able to find a substring from the output of a variable. I can't get this to give me the expected result.
#!/bin/zsh
proc=$(system_profiler SPHardwareDataType | grep "Chip:" | /usr/bin/awk '{print $2,$3,$4}')
echo $proc
if [ "$proc" = *"M2"* ]; then
echo "M2 processor installed"
else
echo "unknown"
fi
The correct result should be "M2 processor installed". I get "unknown". I looked up how to do this and found this site:
https://linuxize.com/post/how-to-check-if-string-contains-substring-in-bash/
After reading that site I changed my conditional statement to match the syntax in the article.
#!/bin/zsh
proc=$(system_profiler SPHardwareDataType | grep "Chip:" | /usr/bin/awk '{print $2,$3,$4}')
echo "$proc"
if [[ "$proc" = *"M2"* ]]; then
echo "M2 Processor installed"
else
echo "unknown"
fi
This works. The only change I made was to use "[[ ]]" instead of "[ ]". I have written a lot of conditional statements using "[ ]" and they all work. I have also used "[[ ]]" and they work too. Why did this not work when I used "[ ]"? Knowing this will save me a lot of annoyance in the future. When I can't get a script to work, it's most often the case that I am using the wrong syntax. I'm not new to scripting but I am always happy to get tips and advice.
Solved! Go to Solution.
Posted on 03-14-2024 05:54 AM
Posted on 03-14-2024 05:54 AM
Greetings Howie,
This might help:
Posted on 03-14-2024 06:02 AM
Thanks! This looks very helpful. The Bash Reference Manual linked there has a lot of great info.
Posted on 03-15-2024 08:17 AM
In addition to @thebrucecarter 's answer, I would recommend using the '-xml' option when using system_profiler so that you can extract the value without relying on output formatting. It's a bit more verbose, but should be more reliable over time.
❯ system_profiler -xml SPHardwareDataType | plutil -extract '0._items.0.chip_type' raw -
Apple M1 Max
OR
❯ system_profiler -xml SPHardwareDataType | xmllint -xpath '//key[text() = "_items"]/following-sibling::*[1]//key[text() = "chip_type"]/following-sibling::*[1]/text()' -
Apple M1 Max
Posted on 03-15-2024 12:47 PM
Thanks. The code I started the thread with was just used as an example of trying to find the right syntax to get what I needed. I like finding out new ways to get the outputs that I need so thanks for posting this.
Posted on 03-14-2024 11:38 AM
You might want to try:
/usr/sbin/sysctl -n machdep.cpu.brand_string
Faster than system_profiler as well.