I'm learning bash from a book and just wanted to see if there is a more efficient way to do this.
The output of cat /proc/acpi/wakeup is multiple lines, but I only care about this one:
GPP0 S4 *disabled pci:0000:00:01.1
So I can grep for just that line no problem, but I only need the value under the Status column, that is, I only need to know whether its value is enabled or disabled.
This part of my script is conditional. If the user tries to Suspend the system, and the status is set to disabled, it should go ahead and Suspend. Otherwise, if the status is enabled, it should disable it first before allowing the system to Suspend.
I have come up with three different approaches to this:
First approach (using cut):
cat /proc/acpi/wakeup | grep GPP0 | cut -d "*" -f 2 | cut -d " " -f 1 # 'disabled'
Second appraoch (using awk):
cat /proc/acpi/wakeup | grep GPP0 | awk '{print $3}' | cut -d "*" -f 2
Third approach:
cat /proc/acpi/wakeup | grep GPP0 | awk '{if ($3 == "*disabled") print "Already disabled"; else print "DISABLE IT"}'
I'm leaning toward the third approach, but I was wondering if there is a standard way to grab the value from the Status column. For example using GPP0 as the "key" and enabled or disabled would be the "value."
I could not find this in the manpages or tldr tool.
Also, I realize that the choice to use cat could be the problem with the script's efficiency, so if there is a better tool that is standard for grabbing values of keys, please let me know.
awk '$1=="GPP0" {print $3}' /proc/acpi/wakeup(avoidingcatandgrepaltogether) orawk '$1=="GPP0" {print substr($3,2)}' /proc/acpi/wakeup(to also cut off the first character).if grep -q "GPP0.*disabled" /proc/acpi/wakeup; then echo already disapled; else echo ask 4 disabling,..;ficat <file> | grep <pattern> | awk <program>can be shortened to just one command:awk '/<pattern>/ <program>' <file>;) .GPU0. So it should be a tristate test, not boolean. However, if "not present" should have the same behaviour as one of "enabled" or "disabled", then, eithergrep -q 'GPU0.*disabled' /proc/acpi/wakeup(succeeds if disabled, fails if not present or enabled) or! grep -q 'GPU0.*enabled' /proc/acpi/wakeup(suceeds if not present or disabled, fails if enabled) could be used*enabledor*disabled)