BASH scripting
Links Python/OneLiners , Linux/zsh, https://devhints.io/bash
- Loop through list
cl_iplist_FWext="Int1=41.1.1.2 brInt2=166.6.3.15 brInt3=146.2.2.2 brInt4=156.2.2.62" for i in ${cl_iplist_FWext}; do fw_ip=${i##*=} fw_int=${i%%=*}
Loop counter increase
loopcount=$((loopcount+1))
Add ~/bin to path e.g.
[[ ":$PATH:" == *":$HOME/bin:"* || ":$PATH:" == *":~/bin:"* ]] && echo "~/bin is in PATH" || echo "~/bin is not in PATH"
Time/Date
Calc seconds till REPORT_TIME="now" or REPORT_TIME="21h20"
sleepseconds=$(( $(date +%s -d "$REPORT_TIME") - $( date +%s ) )) if [ "$sleepseconds" -lt "0" ]; then sleepseconds=$(( $(date +%s -d "tomorrow $REPORT_TIME") - $( date +%s ) )) fi
Time in specific timezone
$ echo "$(env TZ=UTC date -Is) / $(env TZ=Pacific/Auckland date -Is)" 2020-02-26T22:17:55+00:00 / 2020-02-27T11:17:55+13:00 env TZ="Pacific/Auckland" date +"%Y%m%d-%Hh%Mm%Ss%z" 20230124-14h05m20s+1300
Sleep with count down
SleepCountdown() { sleepseconds=$1 reportsec=${2:-60} while [ $sleepseconds -gt 0 ]; do echo -ne " $((sleepseconds))s\033[0K\r" sleep $(( $sleepseconds < $reportsec ? $sleepseconds : $reportsec )) sleepseconds=$(($sleepseconds - $reportsec)) done echo -ne " \033[0K\r" }
Function to print elapsed time in readable format
time_start=$(date +%s) function ElapsedTime() { # Prints lapsed time in user readable format, uses $1 or time since $time_start time_elapsed=${1:-$(( $(date +%s) - time_start))} eval "echo Elapsed time: $(date -ud "@$time_elapsed" +'$((%s/3600/24)) days %H hr %M min %S sec') $2" } ## call ElapsedTime # or with seconds ElapsedTime 50000
### MAC os version
# time_start=$(date +%s) function ElapsedMinutes() { # Prints lapsed time in minutes, uses $1 can append $2 t=$( awk -v t="${1:-$(( $(date +%s) - ${time_start} ))}" -v m="${2:-}" 'BEGIN {printf("%.2f%s", t / 60.0, m )}' ) echo "$t" } # ElapsedMinutes # ElapsedMinutes $(( $(date +%s) - ${time_start} )) minutes
pipe stdout and stderr to two different processes in shell script?
in bash
command1 > >(command2) 2> >(command3)
Posix
{ command 2>&1 1>&3 3>&- | stderr_command; } 3>&1 1>&2 | stdout_command
Description of bash components used in script below.
"$@"
- is all the command line parameters as a list
set -- "$@" "$1"
- adds the first command line parameter to end of list, making it one longer
shift
- shift's the command line parameters to the left, reducing them by one
$#
- Is the length or number of command lime parameters.
- One way to use all of this, is where we want to loop through all the command line parameters, filtering some of changing them is some way.
Here is an example that filters a specific parameter (e.g --values=xyz). It loops through all of them adding the ones we want to keep to the end and shifting left dropping the first one every-time.
argc=$# j=0 while [ $j -lt $argc ]; do case "$1" in --values=?*) # do nothing remove this ;; *) set -- "$@" "$1" ;; esac shift j=$((j + 1)) done
Bash arrays(list) associative arrays(maps/dict)
Indexed array eg.
declare -a indexed_array=("Baeldung is" "so much" "cool") echo "Size of array:" ${#indexed_array[@]} echo "With quotes:" for element in "${indexed_array[@]}" do echo "["$element"]" done unset indexed_array[0]
associative array e.g.
declare -A associative_array=(["one"]="Baeldung" ["two"]="is" ["three"]="cool") for key in ${!associative_array[@]} do echo "["$key"]:["${associative_array[$key]}"]" done
Print 100 blank lines
printf '\n%.s' {1..100}
...