View previous topic :: View next topic |
Author |
Message |
Dominique_71 Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/8878940535065700927c31.jpg)
Joined: 17 Aug 2005 Posts: 1923 Location: Switzerland (Romandie)
|
Posted: Fri Jan 31, 2025 1:55 pm Post subject: [was shell] grep inconsistency [solved - work around] |
|
|
In my gento system, I have the following script:
Code: | $ cat ScreenLidSuspendPID
#!/usr/bin/env bash
if [ "$(pgrep -f ScreenLidSuspend)" ] ; then
echo do...
fi
|
I have no ScreenLidSuspend process running and I get:
Code: | $ pgrep -f ScreenLidSuspend
$
snip
# the if itself, at the shell do nothing, that's correct:
$ if [ "$(pgrep -f ScreenLidSuspend)" ] ; then echo do...; fi
$
snip
# the script do the echo, that's wrong:
$ ./ScreenLidSuspendPID
do...
$ |
This is the case both in X running fvwm-crystal and urxvt, and when logged in a tty, that even as root. That imply I am lost here. 6.1.112-gentoo-dist kernel and an ~amd64 system. What can I check or do to fix this? _________________ "Confirm You are a robot." - the singularity
Last edited by Dominique_71 on Fri Jan 31, 2025 8:48 pm; edited 2 times in total |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
pingtoo Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/gallery/Star Wars/movie_star_wars_storm_trooper.gif)
Joined: 10 Sep 2021 Posts: 1486 Location: Richmond Hill, Canada
|
Posted: Fri Jan 31, 2025 2:06 pm Post subject: |
|
|
[Deleted content -- misread the post]
ScreenLidSuspendPID vs ScreenLidSuspend
the pgrep -f ScreenLidSuspend, grep pattern "ScreenLidSuspend*". And the "-f" mean the full command line, which include the command name and its arguments.
you can change your test Code: | pgrep -x ScreenLidSuspend | for option "-x" is for exact pattern. |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
user Apprentice
![Apprentice Apprentice](/images/ranks/rank_rect_2.gif)
Joined: 08 Feb 2004 Posts: 216
|
Posted: Fri Jan 31, 2025 3:44 pm Post subject: |
|
|
And do not rely on stdout/output evaluation if return code / EXIT STATUS support available.
Code: | EXIT STATUS
0 One or more processes matched the criteria. For pkill and pidwait, one or more processes must also have been successfully signalled or waited for.
1 No processes matched or none of them could be signalled.
2 Syntax error in the command line.
3 Fatal error: out of memory etc.
|
Lazy example if you not want additional evaluation on != 0 exit status case
Code: | if pgrep programname >/dev/null; then echo found; fi |
|
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
Dominique_71 Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/8878940535065700927c31.jpg)
Joined: 17 Aug 2005 Posts: 1923 Location: Switzerland (Romandie)
|
Posted: Fri Jan 31, 2025 6:14 pm Post subject: |
|
|
I get in the shell:
Code: | $ if pgrep -f DesktopCheckMounts >/dev/null; then echo found; fi
found
$ |
and
Code: | $ if pgrep -x DesktopCheckMounts >/dev/null; then echo found; fi
pgrep: un motif qui cherche un nom de processus plus long que 15 caractères ne retournera aucun résultat
Essayez l'option « pgrep -f » pour obtenir une correspondance sur la ligne de commande complète.
$ |
Which imply pgrep with -x complain if the pattern is greater that 15 characters, but it works with -f.
The practical difference between the 2 being that -f works with partial pattern match in the PID process name, when -x don't match:
Code: | $ if pgrep -x DesktopCheckMo >/dev/null; then echo found; fi
$ if pgrep -f DesktopCheckMo >/dev/null; then echo found; fi
found
$ |
I have 2 gentoo machines and it do that on both. _________________ "Confirm You are a robot." - the singularity |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
Dominique_71 Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/8878940535065700927c31.jpg)
Joined: 17 Aug 2005 Posts: 1923 Location: Switzerland (Romandie)
|
Posted: Fri Jan 31, 2025 6:19 pm Post subject: |
|
|
user wrote: | And do not rely on stdout/output evaluation if return code / EXIT STATUS support available.
...snip
Lazy example if you not want additional evaluation on != 0 exit status case
Code: | if pgrep programname >/dev/null; then echo found; fi |
|
Thanks. If I put that into a script, it run fine,
But that don't explain why, the script in the first post do not work as expected into my gentoo laptop, and run fine into the gentoo desktop. EDIT: In both systems, a given bash command should give the same result at the prompt and inside a script using the same interpreter. _________________ "Confirm You are a robot." - the singularity |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
pingtoo Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/gallery/Star Wars/movie_star_wars_storm_trooper.gif)
Joined: 10 Sep 2021 Posts: 1486 Location: Richmond Hill, Canada
|
Posted: Fri Jan 31, 2025 6:54 pm Post subject: |
|
|
Dominique_71 wrote: | I get in the shell:
Code: | $ if pgrep -f DesktopCheckMounts >/dev/null; then echo found; fi
found
$ |
and
Code: | $ if pgrep -x DesktopCheckMounts >/dev/null; then echo found; fi
pgrep: un motif qui cherche un nom de processus plus long que 15 caractères ne retournera aucun résultat
Essayez l'option « pgrep -f » pour obtenir une correspondance sur la ligne de commande complète.
$ |
Which imply pgrep with -x complain if the pattern is greater that 15 characters, but it works with -f.
The practical difference between the 2 being that -f works with partial pattern match in the PID process name, when -x don't match:
Code: | $ if pgrep -x DesktopCheckMo >/dev/null; then echo found; fi
$ if pgrep -f DesktopCheckMo >/dev/null; then echo found; fi
found
$ |
I have 2 gentoo machines and it do that on both. |
Do you know what happen if your command line that have argument(s)?
the "-f" include the arguments in comparison. So if by chance your process does not have argument and you use '-f -x' then the comparison will match. however if your process does include arguments then it will fail to match. |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
Dominique_71 Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/8878940535065700927c31.jpg)
Joined: 17 Aug 2005 Posts: 1923 Location: Switzerland (Romandie)
|
Posted: Fri Jan 31, 2025 7:33 pm Post subject: |
|
|
pingtoo wrote: | Do you know what happen if your command line that have argument(s)?
the "-f" include the arguments in comparison. So if by chance your process does not have argument and you use '-f -x' then the comparison will match. however if your process does include arguments then it will fail to match. |
-f works when it is arguments, when "-f -x" works as "-x" and fail. For how I understand it, -x look for an exact match of the file name, that in most cases is the command name, but is limited to 15 characters. -f lock for a partial match, but is not limited to 15 characters, which imply you can do things like:
Code: | pgrep -f "/path/to/cmd arg1 arg2" |
If you specify arguments, you must quote the whole string and expansion will fail. When you don't specify arguments, you don't need quoting and my_long_cmd_* will work. With -x, expansion will fail. _________________ "Confirm You are a robot." - the singularity |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
Dominique_71 Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/8878940535065700927c31.jpg)
Joined: 17 Aug 2005 Posts: 1923 Location: Switzerland (Romandie)
|
Posted: Fri Jan 31, 2025 7:43 pm Post subject: |
|
|
Also, I try with "-f -x string", which works like -x, but I didn't try with "-f string -x string2" or "-x string -f string2". _________________ "Confirm You are a robot." - the singularity |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
Dominique_71 Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/8878940535065700927c31.jpg)
Joined: 17 Aug 2005 Posts: 1923 Location: Switzerland (Romandie)
|
Posted: Fri Jan 31, 2025 7:48 pm Post subject: |
|
|
That still don't explain the cause of that weird bug:
Dominique_71 wrote: | But that don't explain why, the script in the first post do not work as expected into my gentoo laptop, and run fine into the gentoo desktop. EDIT: In both systems, a given bash command should give the same result at the prompt and inside a script using the same interpreter. |
_________________ "Confirm You are a robot." - the singularity |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
pingtoo Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/gallery/Star Wars/movie_star_wars_storm_trooper.gif)
Joined: 10 Sep 2021 Posts: 1486 Location: Richmond Hill, Canada
|
Posted: Fri Jan 31, 2025 8:23 pm Post subject: |
|
|
Dominique_71 wrote: | Also, I try with "-f -x string", which works like -x, but I didn't try with "-f string -x string2" or "-x string -f string2". |
You misunderstand, you cannot do "-f string -x string2".
the "-f" long option name is "--full" mean compare entire command line including arguments.
the "-x" long option name is "--exact" mean compare pattern exact as is, no regex pattern matching. |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
Dominique_71 Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/8878940535065700927c31.jpg)
Joined: 17 Aug 2005 Posts: 1923 Location: Switzerland (Romandie)
|
Posted: Fri Jan 31, 2025 8:28 pm Post subject: |
|
|
Dominique_71 wrote: | user wrote: | And do not rely on stdout/output evaluation if return code / EXIT STATUS support available.
...snip
Lazy example if you not want additional evaluation on != 0 exit status case
Code: | if pgrep programname >/dev/null; then echo found; fi |
|
Thanks. If I put that into a script, it run fine, |
If I use "-x programname", it works as expected both at the prompt and in the script, but with "-f programname", it fail with the script and that only with the laptop. ![Evil or Very Mad :evil:](images/smiles/icon_evil.gif) _________________ "Confirm You are a robot." - the singularity |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
pingtoo Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/gallery/Star Wars/movie_star_wars_storm_trooper.gif)
Joined: 10 Sep 2021 Posts: 1486 Location: Richmond Hill, Canada
|
Posted: Fri Jan 31, 2025 8:31 pm Post subject: |
|
|
Dominique_71 wrote: | That still don't explain the cause of that weird bug:
Dominique_71 wrote: | But that don't explain why, the script in the first post do not work as expected into my gentoo laptop, and run fine into the gentoo desktop. EDIT: In both systems, a given bash command should give the same result at the prompt and inside a script using the same interpreter. |
|
There is no way to understand what happen, since there could be many thing influence the result. like different version of pgrep, different version libraries used by pgrep. different kernel setup influence process command structure.
the check exit status is best approach for examine if a match happen or not. Expect a result string is not empty (or is empty) could be risky because the "[" (test command) may mis-interpretation the string for something else. (for example if the string content "-" may lead to test do something unexpected. |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
Dominique_71 Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/8878940535065700927c31.jpg)
Joined: 17 Aug 2005 Posts: 1923 Location: Switzerland (Romandie)
|
Posted: Fri Jan 31, 2025 8:37 pm Post subject: |
|
|
pingtoo wrote: |
you can change your test Code: | pgrep -x ScreenLidSuspend | for option "-x" is for exact pattern. |
According to how the -x option is working and my testing that show that the -f option is not reliable, I think the best thing I have to do is to change the names of my scripts for them to be <= 15 characters and use grep -x when needed. _________________ "Confirm You are a robot." - the singularity
Last edited by Dominique_71 on Fri Jan 31, 2025 9:05 pm; edited 1 time in total |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
Dominique_71 Veteran
![Veteran Veteran](/images/ranks/rank_rect_5_vet.gif)
![](images/avatars/8878940535065700927c31.jpg)
Joined: 17 Aug 2005 Posts: 1923 Location: Switzerland (Romandie)
|
Posted: Fri Jan 31, 2025 8:58 pm Post subject: |
|
|
Also in both systems, the varenv used by pgrep are the same and its version and USE flags is the same: pgrep from procps-ng 4.0.4. But bash is more recent in the laptop: 5.2.37(1), than in the desktop: 5.2.26(1). _________________ "Confirm You are a robot." - the singularity |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
|