#!/bin/bash
############################################################
#### if-then-else ladder structure and tests            ####
#### while read example with standard input redirection ####
#### named pipes                                        ####
#### using find and cmp                                 ####
############################################################

############################
#### Simple minimal example
for ff in * ; do
    if [ -f "$ff" ] ; then      #### Equivalent by using the OS program 'test' if test -f "$ff" ; then
         echo "$ff --- is a regular file"
    elif [ -d "$ff" ] ; then
         echo "$ff --- is a folder"
    else echo "Unknown file type"
    fi
done

####################################################################################################################################
#### We are checking all regular files in current folder and subfolders against the backup folder ../Arxiu-Anterior for consistency
while read ff ; do
    OtDir="../Arxiu-Anterior"
    if [ -e "${OtDir}/$ff" ] ; then
       if cmp -s "$ff" "${OtDir}/$ff" ; then          #### if [ "`cmp -s "$ff" "${OtDir}/$ff"`" -eq 0 ] ; then #### Worse equivalent alternative with a test implementation
            echo "==== Both versions of file $ff concide; deleting"
            rm -f "$ff"
       else echo "==== WARNING: the two versions of file $ff are different!!!!"
       fi
    else
       echo "ERROR: $ff not found in the backup. Comparing aborted..."
    fi
done < <( find . -type f -printf "%P\n" )


find . -type f -printf "%P\n" > pepito.lis
done < pepito.lis
pepito.lis | while read ff ; do


#### OBSERVATION: $? is the built-in Bash variable that stores the return value of the last executed program
####              It can be printed by echo $?

###################################################################################################################
#### Replace previous "cmp if" by a more complete one dealing with all possible return codes of the cmp program
#### Indeed replace:
####   if cmp -s "$ff" "${OtDir}/$ff" ; then echo "==== Both versions of file $ff concide; deleting" ; rm -f "$ff"; else echo "==== WARNING: the two versions of file $ff are different!!!!" ; fi
#### by
       rescmp=$( cmp -s "$ff" "${OtDir}/$ff" ) ### Since we have to use the return code of the 'cmp' program twice
       if [ "$rescmp" -eq "0" ] ; then
            echo "==== Both versions of file $ff concide; deleting"
            rm -f "$ff"
       elif [ "$rescmp" -eq "1" ] ; then
            echo "==== WARNING: the two versions of file $ff are different!!!!"
       else echo "ERROR indeterminate in 'cmp' program "
       fi

####################################################################################################################################
#                 Tests short table (not complete --- man bash --> <shift>/-e file<enter>)                                         #
#--------------------------|-------------------------------------------------------------------------------------------------------#
#          Test            |        Evaluates to 'true' when                                                                       #
#--------------------------|-------------------------------------------------------------------------------------------------------#
# [ -d file ]              | file exists and is a directory                                                                        #
# [ -e file ]              | file exists                                                                                           #
# [ -f file ]              | file exists and is a regular file                                                                     #
# [ -r file ]              | file exists and is readable                                                                           #
# [ -w file ]              | file exists and is writable                                                                           #
# [ -x file ]              | file exists and is executable                                                                         #
# [ file1 -nt file2 ]      | file1 is newer (according  to  modification  date)  than file2, or if file1 exists and file2 does not #
# [ file1 -ot file2 ]      | file1 is older than file2, or if file2 exists and file1 does not                                      #
# [ -z string ]            | the length of string is zero                                                                          #
# [ string ] [ -n string ] | the length of string is non-zero                                                                      #
# [ string1 == string2 ]   | the strings are equal                                                                                 #
# [ string1 != string2 ]   | the strings are not equal                                                                             #
# [ string1 < string2 ]    | string1 sorts before string2 lexicographically                                                        #
# [ string1 > string2 ]    | string1 sorts after string2 lexicographically                                                         #
# [ arg1 -eq arg2 ]        | arg1 is equal to arg2                                                                                 #
# [ arg1 -ne arg2 ]        | arg1 is not equal to arg2                                                                             #
# [ arg1 -lt arg2 ]        | arg1 is less than arg2                                                                                #
# [ arg1 -le arg2 ]        | arg1 is less than or equal to arg2                                                                    #
# [ arg1 -gt arg2 ]        | arg1 is greater than arg2                                                                             #
# [ arg1 -ge arg2 ]        | arg1 is greater than or equal to arg2                                                                 #
###########################|########################################################################################################
