0

Trying to create an if statement which will search with a username in a location to see if a tar has been done already. If not create said tar in a location. For some reason, my find is running through the then echo command regardless if there is a file in that location or not.

USER_LIST="$(awk '{print $3}' usernamefile.txt)" 
for USER_NAME in $USER_LIST;do
   echo $USER_NAME
   if find /location/to/store/tarfile -type f -iname $USER_NAME; 
    then
        echo "tar file has been found for" $USER_NAME "/location/to/store/tarfile" `date` >> /logfile/log.txt   
    else
    FILE_LOC="$(awk -v $USER_NAME=$3 '{print $5;}' usernamefile.txt)" 
    tar -czvf ${USER_NAME}.tar.gz /location/to/put/tar/file $FILE_LOC
        echo "tar exit code:" $? $USER_NAME "has been archived" `date` >> /logfile/log.txt

    fi
done

I'm not sure why but if the find doesn't find anything. Surely it should move onto the else part of the script? the plan is creating tar files such as <username>.tar.gz

102254563
  • 77
  • 1
  • 1
  • 9
  • Sounds too broad. Please limit the scope of your question to a single, clear problem and provide a [mre]. – oguz ismail Aug 24 '20 at 09:46
  • Not sure how much more i can limit it, thought it was pretty obvious ... I'm trying to work out why the find is always pushing to then and not moving on to else regardless if it can find the file or not. – 102254563 Aug 24 '20 at 09:51
  • 1
    **find**'s exit status doesn't depend on whether it has found a file or not, read the manual, especially the **EXIT STATUS** section. Besides, if there is a problem with the **find** command, you don't need to include the whole script; and *My program is working, but I want to know if there is a better way to do this?* is off-topic unless you clearly state what's wrong with your program. – oguz ismail Aug 24 '20 at 09:55
  • Your `if` statement should be something like that: `if find ...args... | grep -q . ; then ...` or `if [ -n "$(find ...args...)" ]; then ...` – M. Nejat Aydin Aug 24 '20 at 10:12

2 Answers2

1

It appears that using test command would work best for my needs with either a -f or a -e flag.

102254563
  • 77
  • 1
  • 1
  • 9
0

The point is that you don't know how to capture the result of find: this gives a list of files, obeying the conditions you add in the find statement. So, best thing to do, is to count them: if there are none, then there are no such files, so you get something like:

if $(find /location/to/store/tarfile -type f -iname $USER_NAME | wc -l) 
then
  ...

The | wc -l reads the number of found files. If a file has been found, the result is not equal to 0, so it gets interpreted as TRUE.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • If this is what OP was looking for, then this question is a duplicate of [this one](https://stackoverflow.com/questions/25156902/how-to-check-if-find-command-didnt-find-anything), you've been around long enough to know you shouldn't answer duplicates. Hence the downvote – oguz ismail Aug 24 '20 at 10:00
  • @oguzismail Surely SOF is a place for communication and collaboration. If there is a duplicate i didn't find it when I previously searched for my question. Hence where we are here.Thanks to you and Dominique i now know what my issue is. – 102254563 Aug 24 '20 at 10:03
  • There is nothing wrong with **asking** duplicate questions, they can be deleted easily. But **answering** duplicates is harmful in that it fragments valuable information. – oguz ismail Aug 24 '20 at 10:06
  • This answer isn't correct. The `if` statement won't work like that. – M. Nejat Aydin Aug 24 '20 at 10:07
  • Apart from checking out exit status' can you recommend a different command – 102254563 Aug 24 '20 at 10:12