<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Zsh on Cesar Gimenes</title><link>https://crg.eti.br/tags/zsh/</link><description>Recent content in Zsh on Cesar Gimenes</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>crg@crg.eti.br (Cesar Gimenes)</managingEditor><webMaster>crg@crg.eti.br (Cesar Gimenes)</webMaster><lastBuildDate>Sun, 11 Feb 2024 14:13:47 -0300</lastBuildDate><atom:link href="https://crg.eti.br/tags/zsh/index.xml" rel="self" type="application/rss+xml"/><item><title>ps - process hierarchy</title><link>https://crg.eti.br/pub/hierarquia-de-processos/</link><pubDate>Sun, 11 Feb 2024 14:13:47 -0300</pubDate><author>crg@crg.eti.br (Cesar Gimenes)</author><guid>https://crg.eti.br/pub/hierarquia-de-processos/</guid><description>&lt;p>When I started using &lt;a href="https://github.com/crgimenes/compterm">compterm&lt;/a>, a problem soon appeared: sometimes I forgot that compterm was loaded. So I wrote a small function to detect whether I was in a session started by compterm or just a regular zsh session.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#cf222e">function&lt;/span> is_parent_compterm &lt;span style="color:#0550ae">{&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#6639ba">local&lt;/span> &lt;span style="color:#953800">pid&lt;/span>&lt;span style="color:#0550ae">=&lt;/span>&lt;span style="color:#953800">$$&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#6639ba">local&lt;/span> parent_pid
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#cf222e">while&lt;/span> &lt;span style="color:#0550ae">[&lt;/span> &lt;span style="color:#0a3069">&amp;#34;&lt;/span>&lt;span style="color:#953800">$pid&lt;/span>&lt;span style="color:#0a3069">&amp;#34;&lt;/span> -ne &lt;span style="color:#0550ae">1&lt;/span> &lt;span style="color:#0550ae">]&lt;/span>&lt;span style="color:#1f2328">;&lt;/span> &lt;span style="color:#cf222e">do&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#953800">parent_pid&lt;/span>&lt;span style="color:#0550ae">=&lt;/span>&lt;span style="color:#cf222e">$(&lt;/span>ps -p &lt;span style="color:#953800">$pid&lt;/span> -o &lt;span style="color:#953800">ppid&lt;/span>&lt;span style="color:#0550ae">=&lt;/span>&lt;span style="color:#cf222e">)&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#953800">parent_pid&lt;/span>&lt;span style="color:#0550ae">=&lt;/span>&lt;span style="color:#0a3069">${&lt;/span>&lt;span style="color:#953800">parent_pid&lt;/span>&lt;span style="color:#1f2328">//[[:&lt;/span>&lt;span style="color:#953800">blank&lt;/span>&lt;span style="color:#1f2328">:]]/&lt;/span>&lt;span style="color:#0a3069">}&lt;/span> &lt;span style="color:#57606a"># Remove espaços&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#57606a"># Verifica se o nome do comando do processo pai é &amp;#39;compterm&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#cf222e">if&lt;/span> ps -p &lt;span style="color:#953800">$parent_pid&lt;/span> -o &lt;span style="color:#953800">comm&lt;/span>&lt;span style="color:#0550ae">=&lt;/span> &lt;span style="color:#1f2328">|&lt;/span> grep -q &lt;span style="color:#0a3069">&amp;#34;^compterm&lt;/span>$&lt;span style="color:#0a3069">&amp;#34;&lt;/span>&lt;span style="color:#1f2328">;&lt;/span> &lt;span style="color:#cf222e">then&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#cf222e">return&lt;/span> &lt;span style="color:#0550ae">0&lt;/span> &lt;span style="color:#57606a"># Retorna true se encontrar &amp;#39;compterm&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#cf222e">fi&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#953800">pid&lt;/span>&lt;span style="color:#0550ae">=&lt;/span>&lt;span style="color:#953800">$parent_pid&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#cf222e">done&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#cf222e">return&lt;/span> &lt;span style="color:#0550ae">1&lt;/span> &lt;span style="color:#57606a"># Retorna false se &amp;#39;compterm&amp;#39; não for encontrado&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#0550ae">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This function walks through all the parent processes of the current process, and if any of them is named compterm, it returns true. With that I can customize the prompt and tell whether compterm is loaded or not.&lt;/p></description></item></channel></rss>