0

I have a simple shell script that reads a log of files I've uploaded, and I want to output the file size and line count (less one for the CSV header). I'm looping correctly, but when I try to use find, wc, or stat to get the size of the file it errors out with "No such file or directory". When I try the same command in the terminal, it reports back the size as expected.

fileSizeBytes=$(sudo wc -c "$fileName" | awk {'print $1'})
fileSizeKb=$((fileSizeBytes/1024))
lineCount=$(sudo wc -l "$fileName" | awk {'print $1'})
lineCount=$(($lineCount-1))

the fileName variable contains the full path to the file, and I checked that all users have read/write access. Whether I run with script with my account or as root the result is the same.

wc: /plan/files/Today.csv: No such file or directory
wc: /plan/files/Today.csv: No such file or directory
wc: /plan/files/ERMS_STOCK_05052020.csv: No such file or directory
wc: /plan/files/ERMS_STOCK_05052020.csv: No such file or directory
wc: /plan/files/ERMS_RES_05052020.csv: No such file or directory
wc: /plan/files/ERMS_RES_05052020.csv: No such file or directory

Any ideas what might be causing this? I can a similar script on my Mac (have to change some commands), but on my RHEL 7.7 VM I'm encountering this error.

Cheers

Jesse
  • 7
  • 1
  • 5
  • So do the files exists? `Any ideas what might be causing this?` The files might do not exist. You posted 6 error messages, yet the code you posted only depends on some variable `fileName` that is unset in the code you posted. You change `fileName` manually? How is `fileName` changed? – KamilCuk May 05 '20 at 08:36
  • Do you really have an *absolute* path of `/plan/...` where `plan` is off the root directory? – David C. Rankin May 05 '20 at 08:38
  • Yeah, there's a directory /plan/ off the root directory, and within it I created the /files directory where the upload files are stored. The files are there, and if I run "wc -c /plan/files/ERMS_STOCK_05052020.csv" I get the output I expect, but when the script executes, I get the no such file error. I'm using sed to extract the path and filename from the list of uploaded files and assign to the variable fileName. – Jesse May 05 '20 at 11:12
  • Have any of your files come from Windows? Or been within 2metres of Windows? – Mark Setchell May 05 '20 at 11:44
  • These files have only been on *nix as far as I know. They've been on RHEL and Mac, but if they had been on Win what would you suggest? I'm pretty willing to try anything at this point. I've been wrestling with bugs in this script in one form or another since I migrated it to RHEL for the last 9 days. – Jesse May 05 '20 at 14:15
  • Try adding a line into your script to see if there are any *"mad"* characters in your filename like this `echo "$fileName" | cat -vet` – Mark Setchell May 05 '20 at 14:21

1 Answers1

0

thanks for the input. It finally dawned on me what might have been happening, the ever-maddening trailing whitespace. The way I parsed the logfile, I got a whitespace at the end of the path. A little sed magic stripped it and everything is working!

Jesse
  • 7
  • 1
  • 5