• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Need assistance modifying a simple bash script

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
rleyba828
Asked:
rleyba828
  • 3
2 Solutions
 
ozoCommented:
${FILE##*/}
0
 
ozoCommented:
cp $FILE  /var/lib/mysql/test/${FILE##*/}
0
 
pilson66Commented:
#!/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
 
rleyba828Author Commented:
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
 
ozoCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now