Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I tried using ps r > log | wc log but this returns Ambiguous output redirect.. Can someone explain why and provide another solution?

share|improve this question
    
Rather than using a | you could use && that should work. –  delh Nov 22 '12 at 12:10
    

3 Answers 3

up vote 4 down vote accepted

The pipe symbol ("|") redirects the output of one program to the input of another.

You however use ">" to redirect wc's output to a file named log AND at the same time want to redirect the output to STDIN of wc (which won't use it as you provide an input file for wc).

So, you want one of the following (Hint: the latter solution is better as it doesn't create extra files.)

ps r > log ; wc log

or

ps r|wc

BTW: you might want wc to count lines, so wc -l in that case.

share|improve this answer

Using ps to do this is unreliable -- a process' arguments can contain newlines. A better alternative on Linux is to use /proc/loadavg like so:

awk '{ gsub("[0-9]+/", "") ; print $4 }' /proc/loadavg
share|improve this answer
    
I'm so confused about this. top shows 74 processes, ps -A | wc -l shows 76. ps -A --no-headers | wc -l shows 75. Who is right? Who is wrong? Counting the actual number of processes from ps -A indicates that there are 74 processes. The point is, the 74th process is the ps command. –  CMCDragonkai Jun 2 at 7:39

You are mixing redirection and piping

ps r > log # redirects ps output to a file called log (over writing any contents of log)

What you want is

ps r | wc # this connects the output of ps to the input of wc

If you wanted to use your methodology, then you would need to do the following

ps r > log; wc log

meaning, ps redirects its output to a file called log, then the command wc is run on the file log.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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