[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: notify-send script messed up my environment



On Tue, May 07, 2019 at 11:23:46PM +0200, Esteban L wrote:
> pid=$(pgrep -u $username nautilus)

This may produce more than one value.

> dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed
> 's/DBUS_SESSION_BUS_ADDRESS=//' )

This one tries to use the value of $pid but if that variable contains
a space, tab or newline, the result of $pid is expanded unquoted, which
results in more than one argument word.

Let's say the output of your pgrep is two lines:

1700
25836

Your command gets expanded like this:

dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/1700 25836/environ | sed '...')

So, grep is given the following argument words:

<-z> <DBUS_SESSION_BUS_ADDRESS> </proc/1700> <25836/environ>

The -z is an option with no argument.  So the word after that is the
pattern to be searched for.  And the two words after that are the files
that grep will try to open.

Therefore, grep tries to open the file /proc/1700 and the file
25836/environ to read them and search for your pattern.

The result is

> grep: /proc/1700: Is a directory
> grep: 25836/environ: No such file or directory

The script you've got is not designed to handle the case where pgrep finds
more than one process and reports more than one PID.  It has to be
rewritten to handle that.


Reply to: