Skip to content

variable

Arrays

``` bash=

! /usr/bin/bash

declare -a _temperature_entities=("CPU Temp" "System Temp" "AOC_NIC Temp")

for reqsubstr in "${_temperature_entities[@]}"; do echo "reqsubstr: $reqsubstr" done

append and combine
``` bash=
fruits=("apple" "banana")
echo "Initial array: ${fruits[@]}"

fruits+=(orange)
echo "Appended array: ${fruits[@]}"

combined_value=$(IFS="," ; echo "${fruits[*]}")
echo "Combined Value: $combined_value"

pass arrays as parameters ``` bash=

!/bin/bash

declare two arrays

array1=(1 2 3 4 5) array2=("a" "b" "c" "d" "e")

function that accepts two array parameters

my_function() { local arr1=("${@:1:${#}/2}") # copy the first array into a local variable local arr2=("${@:$(( ${#}/2 + 1 ))}") # copy the second array into a local variable echo "The first array elements are: ${arr1[@]}" echo "The second array elements are: ${arr2[@]}" }

call the function with the two arrays as parameters

my_function "${array1[@]}" "${array2[@]}"

## substring
- [How to check if a string contains a substring in Bash](https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash/229585#229585)

## iterate
- [Bash: Iterating over lines in a variable - Super User](https://superuser.com/questions/284187/bash-iterating-over-lines-in-a-variable)

``` bash=
while IFS= read -r line; do
    echo "... $line ..."
done <<< "$list"

background task

``` bash=

! /usr/bin/bash

main_task() { #sleep 5 echo "main task done" exit 1 }

main_task & ret=$? echo "ret is $ret"

:::info
ret is 0
main task done
:::

``` bash=
#! /usr/bin/bash

main_task()
{
    #sleep 5
    echo "main task done"
    exit 1
}

main_task &
bgprocess1=$!
wait $bgprocess1
ret=$?
echo "ret is $ret"

:::info main task done ret is 1 :::

regular expression

=~ ``` bash= [[ "CPU1 Temp | 01h | ok | 3.1 | 84 degrees C" =~ CPU[0-9]?* ]] ; echo $?

``` bash=
#! /bin/bash

OSVER=ubuntu20.04.3
ubuntu_major_ver_list=('20' '22')
if [[ $OSVER =~ ^ubuntu([[:digit:]]{2}).([[:digit:]]{2}).([[:digit:]]{1}) ]]; then
    distribution_major_ver=${BASH_REMATCH[1]}
        echo "$distribution_major_ver"
    msgutil_r "$MASTER_IP" "debug" "Ubuntu distribution and major version is ${distribution_major_ver}" "/var/log/xcat/xcat.log" "$log_label"
    # Check if the string $distribution_major_ver is in the list
    for reqsubstr in "${ubuntu_major_ver_list[@]}"; do
        echo "reqsubstr: $reqsubstr"
        if [[ $distribution_major_ver = $reqsubstr ]]; then
            echo $distribution_major_ver
            break
        fi
    done
fi

regx group

``` bash= _ipmi_host=127.0.0.1 declare -A _phase_meta snmp_out=$(snmpwalk -v2c -c public ${_ipmi_host} .1.3.6.1.4.1.1718.4.1.5.2.1.4)

SNMPv2-SMI::enterprises.1718.4.1.5.2.1.4.1.1.1 = STRING: "AA:L1-L2"

SNMPv2-SMI::enterprises.1718.4.1.5.2.1.4.1.1.2 = STRING: "AA:L2-L3"

SNMPv2-SMI::enterprises.1718.4.1.5.2.1.4.1.1.3 = STRING: "AA:L3-L1"

while IFS= read -r line; do logDebug "Read line: $line" regex="enterprises.[0-9]{4}.([0-9].){6}([0-9].{1})([0-9].{1})([0-9]{1}) = STRING: \"AA:(L[0-9]-L[0-9])" if [[ $line =~ $regex ]] then unit=${BASH_REMATCH[2]} cord=${BASH_REMATCH[3]} phase_idx=${BASH_REMATCH[4]} phase_label=${BASH_REMATCH[5]} echo "${unit} ${cord} ${phase_idx} ${phase_label}" # _phase_meta[${phase_idx}]=${phase_label} local _phase_meta[a]=b declare -p _phase_meta else logWarn "$line doesn't match" >&2 # this could get noisy if there are a lot of non-matching files fi done <<< "$snmp_out"

## pass an associative array as an argument to a function


## others
- [looping through `ls` results in bash shell script](https://superuser.com/questions/31464/looping-through-ls-results-in-bash-shell-script)
- [bash - How do you list all functions and aliases in a specific script? - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/260627/how-do-you-list-all-functions-and-aliases-in-a-specific-script)


### Test
- [Unit Test Shell Scripts:Part One](https://www.leadingagile.com/2018/10/unit-test-shell-scripts-part-one/)
- [Unit testing for shell scripts](https://stackoverflow.com/questions/971945/unit-testing-for-shell-scripts)
- [Shell Script 也可以寫測試](https://ithelp.ithome.com.tw/articles/10139122)
    - [shunit2](https://github.com/kward/shunit2)

### Debug
- [shell - How can I debug a Bash script? - Stack Overflow](https://stackoverflow.com/questions/951336/how-can-i-debug-a-bash-script)
- [bash -x command - Stack Overflow](https://stackoverflow.com/questions/10107124/bash-x-command)
- [command line - How to list all variables names and their current values? - Ask Ubuntu](https://askubuntu.com/questions/275965/how-to-list-all-variables-names-and-their-current-values)

### Reuse
- [[Shell Script] Day28-太多共同的 function 怎麼辦](https://ithelp.ithome.com.tw/articles/10138849)
- [bash - How shall I reuse a function in multiple scripts? - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/448031/how-shall-i-reuse-a-function-in-multiple-scripts)

## example

Build shx from source code
``` bash=
src_dir=foo/bin
expired=30/04/2023
notes="Notes for foo demo purpose"
find ${src_dir} -type f -name "*.sh" | xargs -n1 'shc -e "${expired}" -m "${notes}" -r -f'
find ${src_dir} -type f -name "*.sh.x.c" | xargs rm
find ${src_dir} -type f -name "*.sh.x"