Re: .bash_profile: line execution based on su vs. direct login.

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

 



home user wrote:
> After coming to think that login and interactive modes are not the answer, I
> started digging into ps, and piping its output into grep.  Unfortunately,
> that generates another process "grep ksysguard ...".  Piping into "grep -v
> ..." also doesn't help.  After groping all over grep and bash today and
> trying lots of things, I got lucky.  In the GNU Grep 3.3 web page
> "http://www.gnu.org/software/grep/manual/grep.html";,
> I found this:
> -----
> 7. Why do people use strange regular expressions on |ps| output?
> 
> ps -ef | grep '[c]ron'

You could also use 'pgrep ksysguard' instead of piping ps to
grep.  That avoids both a pipe and the need to hide the grep
command from the grep.

> The main problem is solved.  But this leaves me with two follow-up
> questions:
> 1. How does the [first_letter] prevent matching the ps output line for
> grep?  (I'm almost certain I'm not the only member of this list not knowing
> the answer to this!)  It works, and I'm using it, but it would be good to
> really understand how it works (beyond what the GNU grep page says).  I saw
> no hint of an answer in the regular expression write-up.

When you run ps -ef | grep ksysguard, the ps list includes
entries for both ps -ef and grep ksysguard.  Adding the
bracets around a character in the grep command means that
the grep pattern is looking for that one character followed
by the others, in the case of 'grep [k]sysguard' the regular
expression is k followed by sysguard.

In the ps -ef output, the string [k]sysguard doesn't match
because there are literal brackets there.  In the grep
regex, those brackets have special meaning.  So this is a
good method for filtering such matches in the output of ps.

But pgrep is generally easier and purpose-built for that
particular problem.

> 2. Is there a way to have a *single* grep match one string and not another. 
> Example: find all lines containing "ksysguard" but not "grep".  Note that
> grep ksysguard | grep -v grep
> is *not* what I'm looking for.

Yes, by ensuring the regex is precise and doesn't match both
lines, which is just what using [k]sysguard does.  You might
be able to do this with something fancy like a negative
lookbehind, using 'grep -P' but I'm not sure.

Or you could use a regex which took advantage of the ps -ef
output format, like ps -ef | grep '[0-9] ksysguard'.  That
won't match the grep for ksysguard because it's only looking
for a number followed by a space, followed by ksysguard.

Both are far less elegant/readable than grep [k]sysguard. :)
Which is itself less elegant than using pgrep.  And in this
case, I think, pgrep is far less elegant than using the
desktop's autostart functions, as Ed showed for both Gnome
and KDE.

-- 
Todd

Attachment: signature.asc
Description: PGP signature

_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/users@xxxxxxxxxxxxxxxxxxxxxxx
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux