0

Hi I am trying to use grep with if and if the value is found under the directory it should update the file name with "_1" else copy to other directory

cd /home/inbound/ftp
f3=822220222 #ordernumber which change every time for this instance we use this 

   if [ grep -lq $f3 ]; then
   f4=`find . -name *$f3*` #trying to get the existing file name if available 
   mv "$f4" "$f4_1"        #updating existing file with "_1"
else
   cp $file /home/outbound/ftp  

1 Answers1

-2

grep command can be used to search a pattern in a file or an output. it can be connected to ls with a pipe to check this. However for your scenario the below would be an better alternative to find if the file is present

cd /home/inbound/ftp
f3=822220222 #ordernumber which change every time for this instance we use this 

   if [ -f "*$f3*" ]; then
   f4=`find . -name *$f3*` #trying to get the existing file name if available 
   mv "$f4" "$f4_1"        #updating existing file with "_1"
else
   cp $file /home/outbound/ftp

However this checks for your order number in the variable f3. However you already know the filename that you are going to copy. so you can use that instead of f3.

cd /home/inbound/ftp
filename=`basename $file` #file name of the File being FTPed

   if [ -f $filename ]; then
   mv "$filename" "$filename_1"        #updating existing file with "_1"
   fi   # closing this here will make sure the file gets copied all the time
   cp $file /home/outbound/ftp
  • Please try to clean up your answer to follow best practices. `if [ -f "$filename" ]`; `filename=$(basename "$file")`, `cp "$file" /home/outbound/ftp`, etc. (Also, `[ -f "*$f3*" ]` is checking for a file with `*`s in its name; surely that's not what is wanted). – Charles Duffy Jun 01 '20 at 19:42
  • ...and `f4=$(find ...anything...)` is inherently buggy; see discussion in [BashPitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29). `find` separates its outputs with newlines by default, but filenames can contain newlines. Moreover, `for file in $f4` splits on not just newlines but spaces, and expands glob characters, so files with glob characters or spaces in their names get mishandled. And if there's more than one file with `f3` in its name, then `mv "$f4" "$f4_1"` will fail (it'll also fail because `f4_1` is a valid variable name, but an empty one). – Charles Duffy Jun 01 '20 at 19:42
  • ...so, it would need to be `"${f4}_1"` to suffix a `_1` onto the result of expanding `$f4`. – Charles Duffy Jun 01 '20 at 19:44
  • if [ -f "$f3" ]; then f4=`find . -name *$f3*` mv "$f4" "${f4}_1" else cp $file /home/outbound/ftp fi – Rohit Shamdasani Jun 02 '20 at 08:15
  • I tried this even if the file is present in directory it does not go to mv block and always going to else block. – Rohit Shamdasani Jun 02 '20 at 08:16
  • filename=`basename $file` if [ -f $filename ]; then f4=`find . -name *$f3*` #mv "$f4" "${f4}_1" mv "$filename" "${filename}_1" else cp $file home/outbound/ftp – Rohit Shamdasani Jun 02 '20 at 08:33
  • This works partially for the first time it worked and changed the name with {filename}_1 , but with the second run when same order number used and the file name available with {filename_1} now it throws error instead of updating to {filename_1}_1 – Rohit Shamdasani Jun 02 '20 at 08:36
  • this is the error i receive find: paths must precede expression: filename_1 Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression] mv: cannot stat '': No such file or directory could any help please i am new to scripting – Rohit Shamdasani Jun 02 '20 at 08:37
  • mv "$filename" "$filename_1" instead of this line try this mv "$filename" "${filename}_$(date '+%s')" – Karthik Radhakrishnan Jun 04 '20 at 11:32