===== tasks ===== ==== dummy ==== * [[list files within a specific range]] ===== dummy ===== ==== other pages in this wiki ==== * [[bash scripting]] * [[shell scripting notes]] ==== useful links ==== * https://explainshell.com/ - useful for understanding complex shell commands * https://mywiki.wooledge.org/ArithmeticExpression - Arithmetic Expansion * https://misc.flogisoft.com/bash/tip_colors_and_formatting - escape characters, color prompts, echo colored strings * http://ezprompt.net/ - Easy bash prompt generator ==== documentation links ==== * ignoredups, erasedups - https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html search for ignoredups . ==== 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 ==== * https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/ - Parsing bash script options with getopts ===== Shell ===== ==== what is the use of histappend? ==== See * https://unix.stackexchange.com/questions/6501/why-would-anyone-not-set-histappend-in-bash ===== code snippets ===== ==== getopts shift OPTIND ==== shift "$((OPTIND-1))" Notes: * The quotes are important for reasons explained in https://unix.stackexchange.com/questions/214141/explain-the-shell-command-shift-optind-1/ ==== 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 ==== stackoverflow links I came across ==== * https://stackoverflow.com/questions/18668556/how-can-i-compare-numbers-in-bash * check if a number is greater than something * compare numbers ==== 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:- * https://linuxhint.com/bash_append_array/ - well written but the information density is low. So I just summarized the points here and renamed the variables a bit. * https://stackoverflow.com/questions/1951506/add-a-new-element-to-an-array-without-specifying-the-index-in-bash