#] #] ********************* #] "$d_web"'SysMaint"'Linux/if statements.txt' # www.BillHowell.ca 31May2018 initial 08********08 # 07Aug2023 search "Linux bash [: : integer expression expected" +-----+ https://stackoverflow.com/questions/19505227/integer-expression-expected-error-in-shell-script Integer expression expected error in shell script Asked 9 years, 9 months ago Modified 3 years, 3 months ago Viewed 250k times +-----+ https://unix.stackexchange.com/questions/89824/bash-integer-expression-expected >> no help #] +-----+ 09Sep2023 https://stackoverflow.com/questions/3869072/test-for-non-zero-length-string-in-bash-n-var-or-var Test for non-zero length string in Bash: [ -n "$var" ] or [ "$var" ] Asked 12 years, 11 months ago Modified 1 year ago +--+ Viewed 202k times edited Jun 26, 2019 at 15:30 answered Oct 6, 2010 at 6:46 #] Dennis Williamson #] The following table shows that whether a variable is quoted or not, whether you use single or double brackets and whether the variable contains only a space are the things that affect whether using a test with or without -n/-z is suitable for checking a variable. #] | 1a 2a 3a 4a 5a 6a | 1b 2b 3b 4b 5b 6b #] | [ [" [-n [-n" [-z [-z" | [[ [[" [[-n [[-n" [[-z [[-z" #] -----+------------------------------------+------------------------------------ #] unset| false false true false true true | false false false false true true #] null | false false true false true true | false false false false true true #] space| false true true true true false| true true true true false false #] zero | true true true true false false| true true true true false false #] digit| true true true true false false| true true true true false false #] char | true true true true false false| true true true true false false #] hyphn| true true true true false false| true true true true false false #] two | -err- true -err- true -err- false| true true true true false false #] part | -err- true -err- true -err- false| true true true true false false #] Tstr | true true -err- true -err- false| true true true true false false #] Fsym | false true -err- true -err- false| true true true true false false #] T= | true true -err- true -err- false| true true true true false false #] F= | false true -err- true -err- false| true true true true false false #] T!= | true true -err- true -err- false| true true true true false false #] F!= | false true -err- true -err- false| true true true true false false #] Teq | true true -err- true -err- false| true true true true false false #] Feq | false true -err- true -err- false| true true true true false false #] Tne | true true -err- true -err- false| true true true true false false #] Fne | false true -err- true -err- false| true true true true false false #] #] +-----+ https://stackoverflow.com/questions/10552711/how-to-make-if-not-true-condition #] |good |bad #] +--+ #] directory exists |if [ -d "$7" ] | #] (! not) |if ! [ -d "$dir" ] | #] file exists |if [ -f "$7" ] | #] file [exists, size> 0] |if [ -s FILE ] | #] +--+ #] double squareBrackets for [multiple ifs, strings have puctuation like '"] : #] var [set, not empty] |if [[ $var ]] | #] var [not set, empty] |if [[ ! $var ]] | #] string is null |if [[ -z "$in_eqn" ]] |[ -z "$in_eqn"]] [ "$in_eqn" == '' ] #] string is non-null |if [[ -n "$VAR" ]] |[ -n "$VAR" ] [ "$in_eqn" <> "0" ] # It is better to use the more powerful [[ as far as Bash is concerned. Usual cases # if [[ $var ]]; then # var is set and it is not empty # if [[ ! $var ]]; then # var is not set or it is set to an empty string # The above two constructs look clean and readable. They should suffice in most cases. # Note that we don't need to quote the variable expansions inside [[ as there is no danger of word splitting and globbing. To prevent shellcheck's soft complaints about [[ $var ]] and [[ ! $var ]], we could use the -n option. edited May 28, 2019 at 3:01 answered Apr 13, 2018 at 21:04 codeforester #] +--+ #] strings #] equal |if [ "$s1" == "$s2" ] |if ["$s1" == "$s2"] #] s1 contains s2 |if [[ $s1 == *"$s2"* ]] | (and make sure to add spaces between the symbols) #] for string comparison, need spaces to separate from [] https://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash 31May2018 edited Dec 5 '16 at 8:26 Alexander Mills 13.2k19109222 answered Nov 25 '10 at 13:49 marcog #] +--+ #] numbers [-eq, -ge, -le, -ne] versus [==, >=, <=, <>] # https://linuxhint.com/write-bash-if-else-statements-one-line/ #] numbers only |if [ 100 -le 200 ] |if [ 100 <= 200 ] #] number, str |if [ "100" -le 200 ] |if [ 100 <= 200 ] #] str of number |if [[ "$bol" <= 200 ]] |if [ "$bol" <= 200 ] #] +--+ #] groups-of-compound-conditions-in-bash-test : https://stackoverflow.com/questions/14964805/groups-of-compound-conditions-in-bash-test answered Feb 19 '13 at 18:47, William #] Use the && (and) and || (or) operators: #] if [ expression ] && [ expression ] || [ expression ] ; then #] They can also be used within a single [[ ]]: #] if [[ expression && expression || expression ]] ; then #] And, finally, you can group them to ensure order of evaluation: #] if [ expression && ( expression || expression ) ]] ; then #] if [[ "$line" == "REFERENCES" || "$line" == "R EFERENCES" ]] #] +--+ #] conditional fork : doesn't use if!!! #] [ -f "$fer" ] && close_encrypt 'ASCII' || something else see "$d_bin"'strings run.sh' : echo 'yymmddhhmmP_to_tDeltaUnix should yield tDeltaStd=526566' tDelta=$( yymmddhhmmP_to_tDeltaUnix '211014 8h37m' '221014 18h43m' ) #] [[ 526566 == "$tDelta" ]] && echo "correct tDelta= $tDelta" || echo "wrong tDelta= $tDelta" echo 'use tDeltaStd=0' [[ 0 == "$tDelta" ]] && echo "correct tDelta= $tDelta" || echo "wrong tDelta= $tDelta" 08********08 29Mar2021 search "Linux bash and how do I test if a file is empty?" +-----+ https://stackoverflow.com/questions/9964823/how-to-check-if-a-file-is-empty-in-bash [ -s FILE ] True if FILE exists and has a size greater than zero. Thus, you get "empty.txt" if "diff.txt" is not empty. – Matthias Apr 1 '12 at 13:48 08********08 19Dec2020 https://linuxize.com/post/bash-check-if-file-exists/ The most readable option when checking whether a file exists or not is to use the test command in combination with the if statement . Any of the snippets below will check whether the /etc/resolv.conf file exists: FILE=/etc/resolv.conf if test -f "$FILE"; then echo "$FILE exists." fi Copy FILE=/etc/resolv.conf if [ -f "$FILE" ]; then echo "$FILE exists." fi Copy FILE=/etc/resolv.conf if [[ -f "$FILE" ]]; then echo "$FILE exists." fi https://www.golinuxcloud.com/bash-check-if-file-exists/ same comments ********************** +-----+ 18Apr2019 # 1= footnotesize # 2= footnote # 3= placetextbox echo "bash input 1 = $1" # Note that "[[,]]" are for double evaluations in [if,else] conditionals if [ $1 == "1" ]; then footnote_CRWN="\\\footnotesize{\\\small{978-1-7281-1985-4\/19\/31.00 USD copyright 2019 Crown}}" footnote_Euro="\\\footnotesize{\\\small{978-1-7281-1985-4\/19\/31.00 USD \(c\)2019 European Union}}" footnote_IEEE="\\\footnotesize{\\\small{978-1-7281-1985-4\/19\/31.00 USD \(c\)2019 IEEE}}" footnote_USgv="\\\footnotesize{\\\small{US Government work not protected by U.S. copyright}}" elif [ $1 == "2" ]; then footnote_CRWN="\\\footnote{\\\small{978-1-7281-1985-4\/19\/31.00 USD \(c\)2019 Crown}}" footnote_Euro="\\\footnote{\\\small{978-1-7281-1985-4\/19\/31.00 USD \(c\)2019 European Union}}" footnote_IEEE="\\\footnote{\\\small{978-1-7281-1985-4\/19\/31.00 USD \(c\)2019 IEEE}}" footnote_USgv="\\\footnote{\\\small{US Government work not protected by U.S. copyright}}" elif [[ "$1" == "3" ]]; then footnote_CRWN="\\\placetextbox{0.25}{0.045}{\\\small{978-1-7281-1985-4\/19\/31.00 USD \(c\)2019 Crown}}" footnote_Euro="\\\placetextbox{0.25}{0.045}{\\\small{978-1-7281-1985-4\/19\/31.00 USD \(c\)2019 European Union}}" footnote_IEEE="\\\placetextbox{0.25}{0.045}{\\\small{978-1-7281-1985-4\/19\/31.00 USD \(c\)2019 IEEE}}" footnote_USgv="\\\placetextbox{0.25}{0.045}{\\\small{US Government work not protected by U.S. copyright}}" fi #] +-----+ #] environmental variable checks : see "/media/bill/SWAPPER/bin/backup.sh" for examples (and rest of [start, backup] scripts) #] if [[ "$HOWELL_computer" = "RaspPi" ]]; then #] d_temp="/media/bill/RaspPi_ext4_32Gb/temp/" #] else #] d_temp="/media/bill/ramdisk/" #] fi #] +-----+ #] standard backup: pInn_archiveLocal_pDateMod : #] if [ -f "$pLoger098" ];then #] pInn_archiveLocal_pDateMod "$pLoger098" #] rm "$pLoger098" #] fi #] +-----+ # search "linux bash if and how do I specify a numeeric range?" # # enddoc