1

I am creating ftp.log.processid at run time via my script

now i need to grep this file for a word 'get'

grep -i "get" $ftp_log.$!

I am using the above expression - but it always shows no such file or directory and i see a dot (.) created at the end at run time as below grep -i ftp.log.1234. why is there a dot at the end of grep - and how to avoid getting this created at run time and grep correctly

  • And why is the underscore being changed to a dot? – Jeff Schaller Jun 29 '18 at 11:48
  • The only reason I can think for the trailing dot is that $ftp_log has some nasties in it (for example, try ftp_log=$'...............\rftp.log' and then echo "$ftp_log.$!"). Best advice in these situations is to run your script with set -x to see exactly what's happening line-by-line. – steeldriver Jun 29 '18 at 12:01
1

The command

grep -i "get" $ftp_log.$!

uses a variable called ftp_log. If you have no such variable, the value substituted will be empty.

You might have meant

grep -i "get" "ftp_log.$!"

assuming $! is the process ID of the most recently started background process.

  • Since OP is seeing grep -i ftp.log.1234., the variable seems to be substituted fine, except for the . at the end which they're complaining about. – muru Jun 29 '18 at 10:58
  • yes your understanding is correct- I am trying to grep this inside shell script as below – Dev Jun 29 '18 at 11:04
  • 'gre p -i "get" "ftp_log.$!"` but on running the script it's showing as grep -i "get" ftp.log.1234. where 1234 is the process id – Dev Jun 29 '18 at 11:07
  • @Dev It is unclear what the issue is and what is wrong. – Kusalananda Jun 29 '18 at 13:43
  • @Dev You have to tell us exactly what you are doing. You have used ftp_log and ftp.log interchangeably, and you have not told us what it is that displays grep -i "get" ftp.log.1234. when you say "it shows". One possible issue is that the script file was written on a Windows machine in which case it has carriage returns before each newline. Use dos2unix on the file to convert it to a Unix text file. – Kusalananda Jun 29 '18 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.