2

I'm using a batch file in Windows to connect to a Linux computer through SSH with plink to see if two certain files are up to date (they have to have today's date).

The command I use in the Windows batch file to execute plink:

plink.exe -ssh root@%THEIP% -m checkfiles.txt > temp\fileDates.txt

The "checkfiles.txt" in the Windows computer contains:

ls -l /folder/*file.dat.v* /folder/*file2.dat.v* > awk '{print $7}'

I then proceed to read "fileDates.txt" to see if both files are present and their dates.

I could delete the > awk '{print $7}' part and do it by hand on the batch file.

The problem comes when a file is missing, I get for example:

ls: cannot access /folder/*file.dat.v*: No such file or directory

I get that massage on screen and not in "fileDates.txt", and I only get the date for the second file, with no error about the first file not being found.

I'd like to have the error "No such file..." in "fileDates.txt" so I've been trying different redirection methods to no avail.

If that isn't possible, how can I tell when one file, the other or both are missing?

Thank you in advance.

heffeque
  • 21
  • 1
  • 5

2 Answers2

1

You can use 2> to capture error messages:

ls -l /folder/*file.dat.v* /folder/*file2.dat.v* > awk '{print $7}' 2> sed '"s|ls: cannot access .*|No such file $name_of_variable_with_file_you_want_to_access|"
PlasmaPower
  • 1,864
  • 15
  • 18
  • Hi! Thanks for your fast reply! --- temp\fileDates.txt still doesn't get the 2> part --- I don't really understand why you put a variable there at the end. – heffeque Mar 20 '14 at 08:29
  • 1
    The file being written is in Windows, not in Linux. Linux doesn't "see" the file. >2 has to be sent to plink, then plink (in windows machine) sends it to the file (in windows). --- What I'm looking for is that temp\fileDates.txt has this in it: `ls: cannot access /folder/*file.dat.v*: No such file or directory 20` – heffeque Mar 20 '14 at 08:43
0

I found the answer. Thanks @PlasmaPower for your help (I'd upvote your answer, but I don't have enough reputation).

What was needed was two lines of code:

ls -l /folder/*file.dat.v* /folder/*file2.dat.v* &> $HOME/checkfiles.txt
cat $HOME/checkfiles.txt  | awk '{print $7}'

The first one to do a temporary file with both stdout and stderr, then another line doing the awk command.

Thank you very much!

heffeque
  • 21
  • 1
  • 5