Solved

Need assistance modifying a simple bash script

Posted on 2011-09-24
5
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

751 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