User Tools

Site Tools


bash_notes

tasks

dummy

dummy

getopts

useful articles

Shell

what is the use of histappend?

code snippets

getopts shift OPTIND

shift "$((OPTIND-1))"

Notes:

check if an integer is in an interval

    current_minute=`date +'%M'`
    clean_up_start_minute=40
    clean_up_end_minute=50
    if (( $current_minute >= $clean_up_start_minute )) &&
       (( $current_minute < $clean_up_end_minute )) ;
    then
        /* do something */
    fi
$ (( 5 >= 15 )) && (( 5 < 30 ))  && echo 1 || echo 0
0

$ (( 15 >= 15 )) && (( 15 < 30 ))  && echo 1 || echo 0
1

$ (( 20 >= 15 )) && (( 20 < 30 ))  && echo 1 || echo 0
1

$ (( 30 >= 15 )) && (( 30 < 30 ))  && echo 1 || echo 0
0

$ (( 35 >= 15 )) && (( 35 < 30 ))  && echo 1 || echo 0
0

check if an integer is equal to another integer

tags | equality

    current_minute=`date +'%M'`
    clean_up_minute=15
    if [ "$current_minute" -eq "$clean_up_minute" ];
    then
        /* do something */
    fi

It works for both positive and negative integers.

$ [ 15 -eq 2 ] && echo 1 || echo 0
0

$ [ 15 -eq 15 ] && echo 1 || echo 0
1

$ [ -15 -eq 15 ] && echo 1 || echo 0
0

$ [ -15 -eq -15 ] && echo 1 || echo 0
1

But not for floating point numbers

$ [ 1.5 -eq 2 ] && echo 1 || echo 0
bash: [: 1.5: integer expression expected
0

$ [ 15 -eq 2.0 ] && echo 1 || echo 0
bash: [: 2.0: integer expression expected
0

$ [ 2.0 -eq 2.0 ] && echo 1 || echo 0
bash: [: 2.0: integer expression expected
0

Scripting

echo a string with multiple spaces

Put the variable in double quotes to prevent field splitting

echo "$a"

Example:

$ a='^  foo|'

$ echo $a
^ foo|

$ echo "$a"
^  foo|

Ref:- https://unix.stackexchange.com/questions/273660/how-do-i-echo-a-string-with-multiple-spaces-in-bash-untouched/273663

search tags | string concatenation multiple spaces

add an element to an array

appliances=("AC" "TV" "Mobile" "Fridge" "Oven" "Blender")
appliances+=("Dish Washer")
for appliance in "${appliances[@]}"
do
     echo $appliance
done
languages=("PHP" "MySQL" "Bash" "Oracle")
languages[${#languages[@]}]="Python"
for language in "${languages[@]}"
do
     echo $language
done
fruits=("Banana" "Mango" "Watermelon" "Grape")
fruits=(${fruits[@]} "Jack Fruit")
for fruit in "${fruits[@]}"
do
     echo $fruit
done
men=("John" "Watson" "Micheal")
women=("Lisa" "Ella" "Mila")
people=(${men[@]} ${women[@]})

for person in "${people[@]}"
do
     echo $person
done

Ref:-

bash_notes.txt · Last modified: 2022/04/22 16:44 by raju