-1

I have a problem with cat. I want to write script doing the same thing as ps -e. In pid.txt i have PID of running processes.

ls /proc/ | grep -o "[0-9]" | sort -h > pid.txt

Then i want use $line like a part of path to cmdline for evry PID.

cat pid.txt | while read line; do cat /proc/$line/cmdline; done

i try for loop too

for id in 'ls /proc/ | grep -o "[0-9]\+" | sort -h'; do 
cat /proc/$id/cmdline;
done

Don't know what i'm doing wrong. Thanks in advance.

beginner
  • 3
  • 2
  • What does the output of just `ls /proc/` present you with...? –  Feb 08 '21 at 00:19
  • If you must use `ls`, then use `ls -1 /proc/` and don't use `grep -o` but just `grep '[0-9]'` –  Feb 08 '21 at 00:23

2 Answers2

0

You seem to be in a different current directory when running cat pid.txt... command compared to when you ran your ls... command. Run both your commands on the same terminal window, or use absolute path, like /path/to/pid.txt

Other than your error, you might wanna remove -o from your grep command as it gives you 1 digit for a matching pid. For example, you get 2 when pid is 423. @Roadowl also pointed that already.

Harsh
  • 395
  • 2
  • 7
0

I think what you're after is this - there were a few flaws with all of your approaches (or did you really just want to look at process with a single-digit PID?):

for pid in $(ls /proc/ | grep -E '^[0-9]+$'|sort -h); do cat /proc/${pid}/cmdline; tr '\x00' '\n'; done
tink
  • 14,342
  • 4
  • 46
  • 50