Skip to main content
Solved

Help with finding a substring from the output of a variable


howie_isaacks
Forum|alt.badge.img+23

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. 

Best answer by thebrucecarter

Greetings Howie,

This might help:

https://stackoverflow.com/questions/13542832/difference-between-single-and-double-square-brackets-in-bash

View original
Did this topic help you find an answer to your question?

5 replies

Forum|alt.badge.img+14

howie_isaacks
Forum|alt.badge.img+23
  • Author
  • Esteemed Contributor
  • 772 replies
  • March 14, 2024
thebrucecarter wrote:

Thanks! This looks very helpful. The Bash Reference Manual linked there has a lot of great info.


pete_c
Forum|alt.badge.img+16
  • Honored Contributor
  • 251 replies
  • March 14, 2024

You might want to try:

/usr/sbin/sysctl -n machdep.cpu.brand_string

Faster than system_profiler as well.


Forum|alt.badge.img+9
  • Contributor
  • 83 replies
  • March 15, 2024
thebrucecarter wrote:

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

 


howie_isaacks
Forum|alt.badge.img+23
  • Author
  • Esteemed Contributor
  • 772 replies
  • March 15, 2024
TrentO wrote:

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

 


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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings