Q: LS command responds: garbage option I'm trying to do a script where I need to get a program process ID,
$ var=$"ps -u pi | grep bot.py | awk '{print $1}' | head -n 1"
but instead of it I'm getting:
**error: user name does not exist**
Usage:

ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>
or 'ps --help <s|l|o|t|m|a>'
for additional help text.

For more details see ps(1).


A: If you get this sort of problem, try each part of the pipeline in turn (so try the "ps -upi" command, then "ps -upi | grep bot.py" until you find the failing step.
I assume that "bot.py" is being run by the "pi" user, and you may be running this as another user ("ps -u" in the previous answer will return processes being run by the user running the command, in a prettified format).
Also, you should be enclosing the command in parentheses, not quotes, and you don't need a sequence of grep, awk, head etc, just use awk. You end up with
$ var=$(ps -u pi | awk '/bot\.py/{print $1; exit}')

Edit: I'd missed trailing parenthesis off, and the "." in bot.py will atch any characters - escape it to match a real dot.

A: Use the following updated script code:-
$ var=$"ps -u | grep bot.py | awk '{print $2}' | head -n 1"

Here we have removed 'pi' argument from ps -u pi to ps -u command. and we are printing 2 element which is pid using awk '{print $2}'.
It will return the pid of process whose name contains script bot.py and assign it to var.
