no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


Previous revision
Next revision
fuss:bash [2017/02/22 18:30] – external edit 127.0.0.1
Line 1: Line 1:
 +====== Glob ======
 +
 +''Glob'' is a fast shell-based equivalent of regular expressions.
 +
 +^ Glob ^ Explanation ^
 +| ''*'' | Matches any string, of any length |
 +| ''foo*'' | Matches any string beginning with ''foo'' |
 +| ''*x*'' | Matches any string containing an ''x'' (beginning, middle or end) |
 +| ''*.tar.gz'' | Matches any string ending with ''.tar.gz'' |
 +| ''foo?'' | Matches ''foot'' or ''foo$'' but not ''fools'' |
 +
 +===== Range =====
 +
 +^ Glob   ^ Explanation ^
 +| ''[abcd]'' | Matches ''a'', ''b'', ''c'' and ''d'' |
 +| ''[a-d]'' | The same as above, if your locale is ''C'' or ''POSIX''. Otherwise, implementation-defined. |
 +| ''[!aeiouAEIOU]'' | Matches any character except ''a'', ''e'', ''i'', ''o'', ''u'' and their uppercase counterparts |
 +| ''<nowiki>[[:alnum:]]</nowiki>'' | Matches any alphanumeric character in the current locale (letter or number) |
 +| ''<nowiki>[[:space:]]</nowiki>'' | Matches any whitespace character |
 +| ''<nowiki>[![:space:]]</nowiki>'' | Matches any character that is not whitespace |
 +| ''<nowiki>[[:digit:]_.]</nowiki>'' | Matches any digit, or ''_'' or ''.'' |
 +
 +
 +====== Increment in Bash Arrays ======
 +
 +<code bash>
 +array[$[${#array[@]}+1]] = 3
 +</code>
 +
 +is homologous to:
 +
 +<code bash>
 +array[elements_in(array)+1] = 3
 +</code>
 +
 +====== String Substitute ======
 +
 +''Input'':
 +<code bash>
 +T="test.svg"
 +echo ${T/svg/png}
 +</code>
 +
 +''Output'':
 +<code bash>
 +test.png
 +</code>
 +
 +====== Check if String Contains a Substring ======
 +
 +<code bash>
 +CHECK_STRING="google"
 +if [[ "$CHECK_STRING" != *o* ]]; then
 +  echo "character o is in the string"
 +fi
 +
 +</code>
 +
 +====== Split a String to an Array and other Array Manipulations ======
 +
 +The following code:
 +<code bash>
 +IFS=' ' read -ra VAR_ARRAY <<< "$input"
 +</code>
 +
 +where:
 +  * ''IFS'' will switch the separator (the character to split on) to a space.
 +  * ''read'' will read-in the input
 +  * ''VAR_ARRAY'' is the array that will be filled with the values from the input split by the separator
 +  * ''$input'' is the input to split
 +
 +will split the input to an array.
 +
 +In order to iterate over the elements of the array, one would write:
 +<code bash>
 +for e in "${VAR_ARRAY[@]}"; do
 +    echo $e
 +done
 +</code>
 +
 +which will print out all the elements in the array.
 +
 +To get both the index and the value while iterating over the array, one would write:
 +<code bash>
 +for i in "${!VAR_ARRAY[@]}"; do
 +    echo "$i ${VAR_ARRAY[i]}"
 +done
 +</code>
 +
 +where on every iteration ''$i'' is the index and ''${VAR_ARRAY[i]}'' will expand to the array element at ''i''.
 +
 +To get the number of elements in the array, one would write:
 +<code bash>
 +echo "${VAR_ARRAY[@]}"
 +</code>
 +
 +====== Get the CPU Core Temperature File ======
 +
 +If you use ''coretemp'' to retrieve the CPU sensors temperature, a bunch of files are created in ''/sys/devices/platform/coretemp.*/hwmon/hwmon*/'' such as ''temp1_label'', ''temp2_label'', etc... Each of them represent the temperature of one of the cores but there is no guarantee that the numbering will start at ''1''. The following bash function reads the files in ''/sys/devices/platform/coretemp.*/hwmon/hwmon*/'' and returns the file corresponding to a certain core file:
 +<code bash>
 +###########################################################################
 +##  Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3      ##
 +###########################################################################
 +function getCoreTemperatureFile {                                          
 +    for i in /sys/devices/platform/coretemp.*/hwmon/hwmon*/*_label; do     
 +        IFS=' ' read -ra CORE <<< `cat $i`                                 
 +        if [ "${CORE[0]}" == "Core" ] && [ "${CORE[1]}" == $1 ]; then     
 +            echo "${i//label/input}"
 +        fi
 +    done
 +}
 +</code>
 +
 +an example call, would then be:
 +<code bash>
 +CPU0_TEMPERATURE_FILE=$(getCoreTemperatureFile 0)
 +echo $CPU0_TEMPERATURE_FILE
 +</code>
 +
 +in order to retrieve the temperature of the first core (''0'').
 +
  

fuss/bash.txt · Last modified: 2023/10/15 10:16 by office

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.