0

A script run well on R, but failed when it was executed using R q -e from bash.

The script that run well on R was:

R> sizes <- read.table(pipe("ls -l /tmp | awk '!/^total/ {print $5}'"))
R> summary(sizes)

The command pattern from bash followed a previous discussion, but generated error messages:

R -q -e "x <- read.table(pipe("ls -l /tmp | awk '!/^total/ {print $5}'"));summary(x)"
awk: line 1: extra ')'
awk: line 1: extra ')'
awk: line 1: syntax error at or near ;

What's wrong with the above command?

root@kali:~# uname -a
Linux kali 3.18.0-kali3-586 #1 Debian 3.18.6-1~kali2 (2015-03-02) i686 GNU/Linux
Community
  • 1
  • 1
Curioso
  • 343
  • 1
  • 3
  • 7
  • Assuming you are using bash. Your problem is shell quoting. You need to escape the second and third `"` in your command. Like so: `\"`. You could also have used single quotes instead od double quotes, But then you would have had to escape the single quotes surrounding the awk command. – Bhas Sep 03 '15 at 05:24
  • @Bhas Could you show the correct syntax? –  Sep 03 '15 at 05:27
  • @Pascal. Probably not. I just tried escaping the inner double quotes. That gave an error message from bash. Escaping the `^` and the `!` does seem to work but R gives an error message: `Error: '\!' is an unrecognized escape ....`. Not escaping the `!` generates a bash error message: `!/total/: event not found`. So it appears to be quite difficult to get this working. Another solution is required but I haven't got a clue. A real bash expert needs to look into this. – Bhas Sep 03 '15 at 06:58
  • @Bhas Yes, I tried also to escape, as you suggested, but no luck. –  Sep 03 '15 at 07:02

1 Answers1

0

Try this

ls -l /tmp | awk '!/^total/ {print $5}' | R --slave -e 'x <- scan(file="stdin"); summary(x)'

If you're trying to get stats on all the files in a particular directory hierarchy something like this is probably better:

find /tmp -type f -exec du {} \; | awk '{print $1}' | R --slave -e 'x <- scan(file="stdin"); summary(x)'

Jose Quinteiro
  • 469
  • 4
  • 9