0

I would like to substitute the number of some process to find out its commandline as follows:

> cat /proc/"`fuser /dev/ttyS0`"/cmdline
cat: /proc/5231 /cmdline: No such file or directory  

but it looks like fuser adds an extra whitespace to the PID. Any idea how to get this right, or solve otherwise?

Expected behavior: I want to get a process ID using fuser and in a second step display /proc/<id>/cmdline.

Alex
  • 41,580
  • 88
  • 260
  • 469
  • If your only problem is having extra whitespace, could you just pass it through a `sed` substitution that strips whitespace? – drewmm Mar 21 '13 at 07:41
  • See http://stackoverflow.com/questions/2854655/command-to-escape-a-string-in-bash – oikku Mar 21 '13 at 07:41
  • But on my Debian Squeeze, `fuser /dev/tty1` outputs `/dev/tty1: 1038` :) – pynexj Mar 21 '13 at 08:16

1 Answers1

0

I think echo \"`fuser /dev/ttyS0`\" will show you that there is a leading space. If you split your operation in two steps, you could do the following

u=`fuser /dev/ttyS0`
cat /proc/${u// /}/cmdline

So first you assign the result of fuser to the variable u (which will still contain the space), and then you delete all spaces in u (starting a pattern substitution with // replaces all occurrences of patterns, if you only use / just the first occurrence will be replaces -- which would not be a problem in your example).

chris
  • 4,988
  • 20
  • 36
  • 2
    Or just like this: `u=( $( fuser /dev/ttyS0 ) ); cat /proc/$u/cmdline` – pynexj Mar 21 '13 at 08:10
  • 1
    `arr=()` is defining an indexed array. It'll ignore leading/trailing unquoted white spaces. And `$arr` w/o a subscript is the same as `${arr[0]}`. – pynexj Mar 21 '13 at 08:21