Solved

Need assistance modifying a simple bash script

Posted on 2011-09-24
5
188 Views
Last Modified: 2012-05-12
Hi team,

   Just need some assistance modifying a script I adopted based on my requirements from an  earlier post:

http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_27290774.html

The code that I have so far is this:

#!/bin/bash

UPLOAD_DIR=/home/usera/*.csv

for FILE in `find $UPLOAD_DIR -maxdepth 1 -type f -mmin -10`  # find the file created in past 10 minutes
do
  echo $FILE
  lsof  $FILE   # check if the file still lock by any process
  LOCK=$?
  if [ "$LOCK" -ne "0" ]   # if the file is free
  then
      cp $FILE  /var/lib/mysql/test/$FILE    #copy the file to another folder
  fi
done

Open in new window


My problem is that the $FILE variable contains the whole path name of the file...for example filename Import2SQL.csv would be stored in the $FILE variable as /home/usera/Import2SQL.csv.

Therefore, when it comes to the part where I have to copy the file to another directory:

This line:
      cp $FILE  /var/lib/mysql/test/$FILE 

Open in new window

is evaluating as this:

      cp /home/usera/Import2SQL.csv   /var/lib/mysql/test//home/usera/Import2SQL.csv 

Open in new window


How do I modify the script to strip off the directory part of the filename so that I can copy the file correctly from /home/usera/Import2SQL.csv    to the directory /var/lib/mysql/test/Import2SQL.csv?

Thanks very much.
0
Comment
Question by:rleyba828
  • 3
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 36592141
${FILE##*/}
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 36592144
cp $FILE  /var/lib/mysql/test/${FILE##*/}
0
 
LVL 8

Assisted Solution

by:pilson66
pilson66 earned 100 total points
ID: 36592204
#!/bin/bash

UPLOAD_DIR="/home/usera"
FMASK="*.csv"

for FILE in $(find ${UPLOAD_DIR}/$FMASK -maxdepth 1 -type f -mmin -10)  # find the file created in past 10 minutes
do
  echo $FILE
  FNAME=$(echo $FILE | sed -e "s/$(echo ${UPLOAD_DIR}/ | sed -e "s/\//\\\\\//g")//g") #"
  lsof  $FILE   # check if the file still lock by any process
  LOCK=$?
  if [ "$LOCK" -ne "0" ]   # if the file is free
  then
      cp $FILE  /var/lib/mysql/test/$FNAME    #copy the file to another folder
  fi
done

Open in new window

0
 

Author Comment

by:rleyba828
ID: 36592375
Hi Team,   Thanks for the help.

The suggestion from ozo is very elegant in its simplicity and actually worked for me.

The solution of iplson66 I believe would have worked also.

Thanks very much.

*PS OZO, could you pls. explain what ${FILE##*/}  acutally did?
0
 
LVL 84

Expert Comment

by:ozo
ID: 37199024
man bash
   Parameter Expansion
       ${parameter#word}
       ${parameter##word}
              The word is expanded to produce a pattern just  as  in  pathname
              expansion.  If the pattern matches the beginning of the value of
              parameter, then the result of  the  expansion  is  the  expanded
              value of parameter with the shortest matching pattern (the ``#''
              case) or the longest matching pattern (the ``##'' case) deleted.
              If parameter is @ or *, the pattern removal operation is applied
              to each positional parameter in turn, and the expansion  is  the
              resultant  list.   If parameter is an array variable subscripted
              with @ or *, the pattern removal operation is  applied  to  each
              member  of the array in turn, and the expansion is the resultant
              list.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now