๐ String comparison
๐ก Newskategorie: Linux Tipps
๐ Quelle: reddit.com
I want to create a script that gets the hardware id and board id on a mac, then save them as variables:
hwModel=$( sysctl hw.model | awk '{ print $2 }' )boardID=$( ioreg -l | awk -F" '/board-id/ { print $4 }' )
I then want to use if else statements to check every available model and if one gets found, check all boardIDs for that specific hardware ID. But somehow i cannot get the strings to compare. it always skips over everything and goes to the bottom else error. I tried combinations of [, [[ and equal signs etc. but nothing seems to work.
Here's a shortened example part:
if [ $hwModel = "iMac4,1" ] then if [ "$boardID" == "3A103" ] then downloadCode="018-2392-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [ $hwModel = "iMac5,1" ] then if [ "$boardID" == "Mac-F4228EC8" ] then downloadCode="018-2534-A" parseDownloadCode elif [ "$boardID" == "Mac-F42786A9" ] then downloadCode="018-2533-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [ $hwModel = "iMac5,2" ] then if [ "$boardID" == "Mac-F4218EC8" ] then downloadCode="018-2535-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi else echo "We could not find a suitable download link." echo "You might have a computer that does not support AHT" fi
I think the comma might mess with it. But i don't really know what to do now... Am i looking over something? I am quite new to bash scripting... Thank you in advance!
Edit: Here's the entire code. Though i think i might be onto something, will test my theory in a minute. I think the commands might be adding some blank spaces behind the hardware model and boardIDs
```
!/bin/bash
AHTFinder.sh
Created by Maxim Coppieters on 29/07/20.
Copyright 2020 Maxim Coppieters. All rights reserved.
Some prerequisite variables
hwModel=$( sysctl hw.model | awk '{ print $2 }' ) boardID=$( ioreg -l | awk -F\" '/board-id/ { print $4 }' ) cpuTemp=$( ./osx-cpu-temp ) ver="Version 00.00.00"
parseDownloadCode () { echo "Detected Hardware Model: $hwModel" echo "Detected Board ID: $boardID" echo echo "Your download code is: $downloadCode" echo "Your download link is: http://download.info.apple.com/Apple_Hardware_Test/$downloadCode.dmg/" echo echo "Click while holding CTRL to open the link." }
mainCheckPart2 () { clear echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" echo "@ Apple Hardware Test Finder @" echo "@ Made By Maxim Coppieters @" echo "@ $ver @" echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" echo if [[ $hwModel == "iMac4,1" ]] then if [[ "$boardID" == "3A103" ]] then downloadCode="018-2392-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac5,1" ]] then if [[ "$boardID" == "Mac-F4228EC8" ]] then downloadCode="018-2534-A" parseDownloadCode elif [[ "$boardID" == "Mac-F42786A9" ]] then downloadCode="018-2533-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac5,2" ]] then if [[ "$boardID" == "Mac-F4218EC8" ]] then downloadCode="018-2535-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac6,1" ]] then if [[ "$boardID" == "Mac-F4218FC8" ]] then downloadCode="018-2579-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac7,1" ]] then if [[ "$boardID" == "Mac-F42386C8" ]] then downloadCode="018-2845-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac8,1" ]] then if [[ "$boardID" == "Mac-F226BEC8" ]] then downloadCode="022-3936-A" parseDownloadCode elif [[ "$boardID" == "Mac-F227BEC8" ]] then downloadCode="022-3937-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac9,1" ]] then if [[ "$boardID" == "Mac-F2218EA9" ]] then downloadCode="022-4297-A" parseDownloadCode elif [[ "$boardID" == "Mac-F2218EC8" ]] then downloadCode="022-4293-A" parseDownloadCode elif [[ "$boardID" == "Mac-F2218FC8" ]] then downloadCode="022-4294-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac10,1" ]] then if [[ "$boardID" == "Mac-F2268CC8" ]] then echo "Detected Hardware Model: $hwModel" echo "Detected Board ID: $boardID" echo echo "Multiple download codes have been found for this board ID." echo "Please choose one of them and report if one of them do not work." echo "This way, we can keep the best one. Thank you!" echo "Available download codes are: 022-4451-A and 022-4647-A" echo "Download links are:" echo "http://download.info.apple.com/Apple_Hardware_Test/022-4451-A.dmg/" echo "http://download.info.apple.com/Apple_Hardware_Test/022-4647-A.dmg/" echo echo "Click while holding CTRL to open the link." elif [[ "$boardID" == "Mac-F2268DC8" ]] then echo "Detected Hardware Model: $hwModel" echo "Detected Board ID: $boardID" echo echo "Multiple download codes have been found for this board ID." echo "Please choose one of them and report if one of them do not work." echo "This way, we can keep the best one. Thank you!" echo "Available download codes are: 022-4452-A and 022-4644-A" echo "Download links are:" echo "http://download.info.apple.com/Apple_Hardware_Test/022-4452-A.dmg/" echo "http://download.info.apple.com/Apple_Hardware_Test/022-4644-A.dmg/" echo echo "Click while holding CTRL to open the link." else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac11,2" ]] then if [[ "$boardID" == "Mac-F2238AC8" ]] then downloadCode="022-4703-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac11,3" ]] then if [[ "$boardID" == "Mac-F2238BAE" ]] then downloadCode="022-4776-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac12,1" ]] then if [[ "$boardID" == "Mac-942B5BF58194151B" ]] then echo "Detected Hardware Model: $hwModel" echo "Detected Board ID: $boardID" echo echo "Multiple download codes have been found for this board ID." echo "Please choose one of them and report if one of them do not work." echo "This way, we can keep the best one. Thank you!" echo "Available download codes are: 022-5090-A and 022-5344-A" echo "Download links are:" echo "http://download.info.apple.com/Apple_Hardware_Test/022-5090-A.dmg/" echo "http://download.info.apple.com/Apple_Hardware_Test/022-5344-A.dmg/" echo echo "Click while holding CTRL to open the link." else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "iMac12,2" ]] then if [[ "$boardID" == "Mac-942B59F58194171B" ]] then downloadCode="022-5091-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "MacMini1,1" ]] then if [[ "$boardID" == "3A102" ]] then downloadCode="018-2342-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "MacMini2,1" ]] then if [[ "$boardID" == "Mac-F4208EAA" ]] then downloadCode="018-2886-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi elif [[ $hwModel == "MacMini3,1" ]] then if [[ "$boardID" == "Mac-F22C86C8" ]] then downloadCode="022-4292-A" parseDownloadCode else echo "We could not determine your computer. It might not be added yet" fi
elif [[ $hwModel == "MacMini4,1" ]] then if [[ "$boardID" == "Mac-F220BEC8" ]] then while true; do read -p "Do you have a server model? Server models have no optical drive." serv case $serv in [Yy]* ) downloadCode="022-4739-A" parseDownloadCode break ;; [Nn]* ) downloadCode="022-4706-A" parseDownloadCode break ;; * ) echo "Please enter Y or N";; esac done else echo "We could not determine your computer. It might not be added yet" fi
else echo "We could not find a suitable download link." echo "You might have a computer that does not support AHT" fi }
The main check and comparison
mainCheck () { echo "Current CPU temp: $cpuTemp" echo "Mac Hardware Model: $hwModel" echo "Mac Board ID: $boardID" echo while true; do read -p "Would you like to determine the AHT download link?" yn case $yn in [Yy]* ) mainCheckPart2; break;; [Nn]* ) echo "Thank you for using my script!"; exit;; * ) echo "Please enter Y or N" esac done }
Just show HW model and BoardID
justInfo () { clear echo "Your Hardware Model is: $hwModel" echo "Your Board ID is: $boardID" echo echo echo "Thank you for using my script! Have a great day!" exit }
Step by step guide on using the modern Apple Diagnostics
getHelp () { clear echo "Step by step guide on using the modern Apple Diagnostics" }
clear echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" echo "@ Apple Hardware Test Finder @" echo "@ Made By Maxim Coppieters @" echo "@ $ver @" echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" echo echo "Please Read!" echo "This script determines the AHT version for Macs with AHT. These Macs were" echo "introduced before June 2013." echo "If your Mac was made before June 2013, this script should work and you can" echo "continue" echo "Another Note: Some Mac models could not be tested properly and have not" echo "been implemented into the script. This is mostly the PowerMac, PowerBook," echo "iBook and early Intel models. You can find the download codes for those" echo "in the README.md file on GitHub." echo read -s -n 1 -p "Press [ENTER] to continue..." echo clear echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" echo "@ Apple Hardware Test Finder @" echo "@ Made By Maxim Coppieters @" echo "@ $ver @" echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" echo PS3='Choose an option:' options=("Determine machine and links" "Just show me my Mac" "Help me diagnosing a newer Mac" "Quit") COLUMNS=12 select opt in "${options[@]}"; do case $opt in "Determine machine and links") mainCheck break ;; "Just show me my Mac") justInfo break ;; "Help me diagnosing a newer Mac") echo "Get help" getHelp break ;; "Quit") clear echo "Thank you for using. Have a great day!" exit ;; *) echo "$REPLY is not a valid option. Please try again.";; esac done
```
[link] [comments] ...