User Tools

Site Tools


bash_notes

tasks

dummy

dummy

other pages in this wiki

what is the difference between "ls > dirlist 2>&1" and "ls 2>&1 > dirlist"?

The command

ls > dirlist 2>&1

directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command

ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was made a copy of the standard output before the standard output was redirected to dirlist.

To conclude, the order of redirections is significant.

Ref:- https://www.gnu.org/software/bash/manual/html_node/Redirections.html → 5th paragraph.

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: 2023/12/28 08:49 by raju