<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arash</title>
    <description>The latest articles on DEV Community by Arash (@araverse).</description>
    <link>https://dev.to/araverse</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1506236%2Fad45b7cb-a115-4229-a2c7-206c02581d36.jpeg</url>
      <title>DEV Community: Arash</title>
      <link>https://dev.to/araverse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/araverse"/>
    <language>en</language>
    <item>
      <title>Executing bash script commands in a sub-shell to manage status code and output</title>
      <dc:creator>Arash</dc:creator>
      <pubDate>Mon, 20 May 2024 15:23:16 +0000</pubDate>
      <link>https://dev.to/araverse/executing-bash-script-commands-in-a-sub-shell-to-manage-status-code-and-output-ggi</link>
      <guid>https://dev.to/araverse/executing-bash-script-commands-in-a-sub-shell-to-manage-status-code-and-output-ggi</guid>
      <description>&lt;p&gt;Hello Community👋,&lt;/p&gt;

&lt;p&gt;This is my first post on Dev.to! For a long time, I thought it would be a good idea to share something that might help others, but perfectionism always got in the way. Each time I tried, I ended up with drafts and nothing more.&lt;/p&gt;

&lt;p&gt;This time, I promised myself to start small, taking baby steps. I hope to keep this up, so here's my very small step into the blogging world.&lt;/p&gt;

&lt;p&gt;I still believe in bash. I prefer to rely on bash wherever it's the best and most suitable option. I have code written 10 years ago that's still working. I created a complete deployment system 8 years ago in bash, which I believe is still in production. Over time, I've used some functions that have stuck with me like old friends. Today, I want to introduce one of them: &lt;code&gt;run()&lt;/code&gt; function. &lt;br&gt;
We've been friends for about 9-10 years now, and it's one of the best.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;run&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt; 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;&lt;span class="nv"&gt;$out&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;out&lt;span class="o"&gt;}&amp;gt;&lt;/span&gt;&amp;amp;1
    &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$?&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$status&lt;/span&gt; &lt;span class="nt"&gt;-ne&lt;/span&gt; 0 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
        &lt;/span&gt;messages+&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$error&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="nv"&gt;failed&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true
        echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$messages&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="nb"&gt;exit &lt;/span&gt;1
    &lt;span class="k"&gt;fi
    return&lt;/span&gt; &lt;span class="nv"&gt;$status&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://gist.github.com/Ara4Sh/7a5d96cfcddb557d430e17dca18e55ab"&gt;Link to gist&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;run()&lt;/code&gt; function in the given code executes a command in subshell, captures its output, logs any errors, and handles failure conditions. Here's a detailed breakdown of it's most important part of the function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt; 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;&lt;span class="nv"&gt;$out&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;out&lt;span class="o"&gt;}&amp;gt;&lt;/span&gt;&amp;amp;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{ error=$($@ 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;$out); }&lt;/code&gt;: This captures the combined standard output and standard error of the command into the variable error from a subshell.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{out}&amp;gt;&amp;amp;1&lt;/code&gt;: This redirects the output back to standard output.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;P.S Since I'm not a native English speaker, I used writing tools to help me with grammar.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Thank you.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>shell</category>
      <category>linux</category>
      <category>sre</category>
    </item>
  </channel>
</rss>
