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
[[:alnum:]] Matches any alphanumeric character in the current locale (letter or number)
[[:space:]] Matches any whitespace character
[![:space:]] Matches any character that is not whitespace
[[:digit:]_.] Matches any digit, or _ or .

Increment in Bash Arrays

array[$[${#array[@]}+1]] = 3

is homologous to:

array[elements_in(array)+1] = 3

String Substitute

Input:

T="test.svg"
echo ${T/svg/png}

Output:

test.png

Check if String Contains a Substring

CHECK_STRING="google"
if [[ "$CHECK_STRING" != *o* ]]; then
  echo "character o is in the string"
fi

Split a String to an Array and other Array Manipulations

The following code:

IFS=' ' read -ra VAR_ARRAY <<< "$input"

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:

for e in "${VAR_ARRAY[@]}"; do
    echo $e
done

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:

for i in "${!VAR_ARRAY[@]}"; do
    echo "$i ${VAR_ARRAY[i]}"
done

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:

echo "${VAR_ARRAY[@]}"

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:

###########################################################################
##  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
}

an example call, would then be:

CPU0_TEMPERATURE_FILE=$(getCoreTemperatureFile 0)
echo $CPU0_TEMPERATURE_FILE

in order to retrieve the temperature of the first core (0).


fuss/bash.txt · Last modified: 2017/02/22 18:30 (external edit)

Access website using Tor Access website using i2p


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