rleyba828
asked on
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:
https://www.experts-exchange.com/questions/27290774/How-to-run-a-bash-perl-script-that-gets-triggered-when-a-file-has-been-received-in-a-directory.html
The code that I have so far is this:
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:
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/Import 2SQL.csv?
Thanks very much.
Just need some assistance modifying a script I adopted based on my requirements from an earlier post:
https://www.experts-exchange.com/questions/27290774/How-to-run-a-bash-perl-script-that-gets-triggered-when-a-file-has-been-received-in-a-directory.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
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
is evaluating as this: cp /home/usera/Import2SQL.csv /var/lib/mysql/test//home/usera/Import2SQL.csv
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
Thanks very much.
${FILE##*/}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
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.
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.