bash if file exists, go to function

Using bash scripting,...

I need the following which I will put in a loop to keep checking;

if file exists, then go to function
projectsAsked:
Who is Participating?
 
Mark OlsenConnect With a Mentor Sr. DeveloperCommented:
I just wrapped my example code and was able to get it to run:

function file_check()
{
   for filename in $(ls); do
      if [[ $filename =~ .txt$ ]]
      then
         rm $filename
      fi
   done
}

Open in new window

0
 
Mark OlsenSr. DeveloperCommented:
Try the "-e" (file exists) conditional:

if [ -e "filename" ]
then
   doFunction
fi

Open in new window

0
 
Mark OlsenSr. DeveloperCommented:
To expand on my first comment, here is a good list of file conditionals:

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
projectsAuthor Commented:
Does the trick just fine. Can you add just one small thing.
I'd like to monitor for a text file like '*.txt' for example.

I won't know what the full file name is so I need to first check it's full file name by looking for '*.txt' then using that as input for my function.

It will be the only .txt file there.

My attempt is not working.

function file_check()
{
        openfile=$( ls -la /temp/*.txt | awk '{print $9}' )
        if [ -e "/temp/... what goes here??? *" ]
        then
echo "$OPENFILE"

# If there is a *.txt file in /temp, then I want to know it's full name. Then, do something
# with the file, say delete it for testing.
# so rm -f /temp/whateverthefilenameis.txt

        fi
}
0
 
Mark OlsenSr. DeveloperCommented:
Disclaimer: my bash is a bit rusty, I'm translating how I would do this in Perl into bash.

If you are already iterating through a list of files you can add a regular expression (regex) to the test. This looks for strings that end in ".txt".

if [ $filename =~ ".txt$" -a -e $filename ]
then
   doFunction
fi

Open in new window


note: The "-a" operator is the "and" operator.
0
 
projectsAuthor Commented:
Sorry, not following that anymore.
I need to know what the file name is.
Once I know what the file name is, I need to use it as input to something.
As a test, I'd like to say delete it or rename it to something else.
0
 
projectsAuthor Commented:
I am able to get the file name and do something with it now but I still need to loop check to see if there is a file in the directory ending with *.txt
So far, what I have isn't quite working.

function file_check()
{
        if [ $filename =~ "/temp/.txt$" -a -e $filename ]
        then
        OPENFILE="$( ls -la /temp/*.txt | awk '{print $9}' )"
        cp $OPENFILE $OPENFILE.two
        fi
}

Guessing I have a mismatch between $FILENAME and $OPENFILE
0
 
Mark OlsenSr. DeveloperCommented:
Ok, let's give this a try. It will find any .txt files in the directory and give you the hook to issue a command:

for txtfile in $(ls -la /temp/*.txt | awk '{print $9}'); do
   cp $txtfile $txtfile.two
done

Open in new window

0
 
projectsAuthor Commented:
So if there is no text file, then the script will continue on right.

This works as a stand alone script but when I add it as a function, it returns an error;

ls: /temp/*.txt: No such file or directory
0
 
Mark OlsenSr. DeveloperCommented:
Seems like if there is no .txt file that ls command is failing. Let's try this approach which iterates through all of the files in the /temp directory and looks for the match with a regex.

for filename in $(ls /temp); do
   if [[ $filename =~ .txt$ ]]
   then
       cp $filename $filename.two
   fi
done

Open in new window

0
 
projectsAuthor Commented:
I am using this as a function in an existing script;

function file_check()
{
        for filename in $(ls /temp); do
        if [[ $filename =~ .txt$ ]]
        then
        rm $filename
        fi
}

When I run my script, it is terminating complaining about the last 'done' statement.
I've tried commenting out the done and fi in this outage so obviously have a syntax issue going on now.

I'm also getting the following error;

 ./script.sh
./script.sh: line 110: syntax error near unexpected token `}'
./script.sh: line 110: `}'

Line 110 is the last } above.
0
 
projectsAuthor Commented:
Either way, I'll award you this because it's just a syntax issue I need to resolve.

Thanks very much!
0
 
Mark OlsenSr. DeveloperCommented:
Thanks, it was a bit of a learning experience myself with the bash syntax. Now back to my cozy Java and Perl...
0
 
projectsAuthor Commented:
Oops. Just one small problem.
I got it to work but I need it to skip the command IF no file exists.
So if no file, then don't run the command, just skip it.
0
 
Mark OlsenSr. DeveloperCommented:
If no files exist then there is nothing to iterate on in the "ls" command's loop, thus it won't test for the filename match. To add an extra layer of checking you could do this:

function file_check()
{
   for filename in $(ls); do
      if [ -e $filename ]
      then
         if [[ $filename =~ .txt$ ]]
         then
            echo $filename
         fi
      fi
   done
}

Open in new window


Replace the "echo" with the command you wish to do, this was just in place so I could make sure it was getting to the command point.
0
 
projectsAuthor Commented:
Perfect! Thanks very much for the extra little bit :)
0
 
Mark OlsenSr. DeveloperCommented:
You're welcome, glad it all came together.
0
 
TintinCommented:
Note that

function file_check()
{
   for filename in $(ls); do
      if [[ $filename =~ .txt$ ]]
      then
         rm $filename
      fi
   done
}

Open in new window


is equivalent to

function file_check()
{
  rm -f *.txt
}

Open in new window

0
 
projectsAuthor Commented:
Oh, yes, I see what you are saying but I just used rm -f as a silly example.

It actually looks like this now, which is still not perfect but better;

function update_check()
{
        # Check for new update file. If there is one, download it.
        curl -s -k --key sum.key --cert sum.crt https://server/update.php -u "$USERNAME:$PASSWD" -o $FWFILE

        for filename in $(ls /tmp); do
        if [ -e $filename ]
            then
                if [[ $filename =~ .bin$ ]]
                then
#                cat $filename >/tmp/updates.txt
echo "There is an upgrade"
                fi
            fi
        done
}

Open in new window


You always seem to help me after the points are given out. Maybe you can just get your own with another of my questions :).

http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28487404.html
0
 
TintinCommented:
I'd write the above function as:

function update_check()
{
        # Check for new update file. If there is one, download it.
        curl -s -k --key sum.key --cert sum.crt https://server/update.php -u "$USERNAME:$PASSWD" -o $FWFILE

       if file=$(ls /tmp/*.bin 2>/dev/null) 
       then
              echo "There is an upgrade"
              echo "$file" >/tmp/updates
       fi
      
}

Open in new window

0
 
projectsAuthor Commented:
Based on the following question;
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28487404.html

My function now looks like;

function file_check()
{
        # Check for new  file
        echo "Checking for file update"
        curl -s -k --key sum.key --cert sum.crt https://server/fileupdate.php?id=open-$USERNAME.bin -u "$USERNAME:$PASSWD" -o $UPDATE

        for filename in $(ls /tmp); do
        if [ -e $filename ]
            then
                if [[ $filename =~ .bin$ ]]
                then
# do something with the file
mv $UPDATE $UPDATE.two
                fi
            fi
        done
}

Open in new window


In the end, I have no idea how safe the function is nor the php side is. Someone mentioned script kiddies in my other post for the php side of this.
0
All Courses

From novice to tech pro — start learning today.