<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Bash Scripting on D. Sébastien Taggart</title>
    <link>https://www.sebastientaggart.com/tags/bash-scripting/</link>
    <description>Recent content in Bash Scripting on D. Sébastien Taggart</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Sun, 04 Oct 2026 23:02:59 -0400</lastBuildDate>
    <atom:link href="https://www.sebastientaggart.com/tags/bash-scripting/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Read a Filtered List of Files from a Directory and Ask the User to Choose One (Bash)</title>
      <link>https://www.sebastientaggart.com/writing/read-a-filtered-list-of-files-from-a-directory-and-ask-the-user-to-choose-one-bash/</link>
      <pubDate>Mon, 30 Sep 2019 14:40:05 -0500</pubDate>
      <guid>https://www.sebastientaggart.com/writing/read-a-filtered-list-of-files-from-a-directory-and-ask-the-user-to-choose-one-bash/</guid>
      <description>&lt;p&gt;One of the standout features of Docksal is its extensibility through Bash scripting, making it a valuable tool for automating various tasks. A common scenario is the need to import databases when initializing projects. The snippet below serves this purpose by scanning a directory for database files and allowing the user to select which one to import.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Get a list of *.mysql | *.sql.gz | *.sql in /db/ and let the user choose which to import&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nb&#34;&gt;unset&lt;/span&gt; options i
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;while&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;IFS&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;read&lt;/span&gt; -r -d &lt;span class=&#34;s1&#34;&gt;$&amp;#39;\0&amp;#39;&lt;/span&gt; f&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  options&lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;i++&lt;span class=&#34;o&#34;&gt;]=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$f&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;done&lt;/span&gt; &amp;lt; &amp;lt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;find &lt;span class=&#34;si&#34;&gt;${&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;PROJECT_ROOT&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;}&lt;/span&gt;/db/ -maxdepth &lt;span class=&#34;m&#34;&gt;1&lt;/span&gt; -type f &lt;span class=&#34;se&#34;&gt;\(&lt;/span&gt; -name &lt;span class=&#34;s2&#34;&gt;&amp;#34;*.mysql&amp;#34;&lt;/span&gt; -o -name &lt;span class=&#34;s2&#34;&gt;&amp;#34;*.sql.gz&amp;#34;&lt;/span&gt; -o -name &lt;span class=&#34;s2&#34;&gt;&amp;#34;*.sql&amp;#34;&lt;/span&gt; &lt;span class=&#34;se&#34;&gt;\)&lt;/span&gt; -print0&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nb&#34;&gt;echo&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Press [Enter] to choose a DB to import&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nb&#34;&gt;read&lt;/span&gt; -t5 -p &lt;span class=&#34;s2&#34;&gt;&amp;#34;(times out in 5 seconds) &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$?&lt;/span&gt; -gt &lt;span class=&#34;m&#34;&gt;0&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nb&#34;&gt;echo&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nb&#34;&gt;echo&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Timed out waiting for response. Skipping DB import...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;k&#34;&gt;select&lt;/span&gt; opt in &lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;${&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[@]&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Skip DB Import&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$opt&lt;/span&gt; in
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;      *.mysql &lt;span class=&#34;p&#34;&gt;|&lt;/span&gt; *.sql.gz &lt;span class=&#34;p&#34;&gt;|&lt;/span&gt; *.sql&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;nb&#34;&gt;echo&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Importing &lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$opt&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt; ...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        fin db import &lt;span class=&#34;nv&#34;&gt;$opt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;nb&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;      &lt;span class=&#34;s2&#34;&gt;&amp;#34;Skip DB Import&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;nb&#34;&gt;echo&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Skipping DB Import...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;nb&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;      *&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;nb&#34;&gt;echo&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Invalid choice, please enter the number of your choice above.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;esac&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;k&#34;&gt;done&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;fi&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This script effectively searches a specified directory for files that match certain extensions and prompts the user to make a selection. The inclusion of a timeout is a thoughtful touch, especially for scripts designed to run unattended or as part of a larger automation workflow.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>One of the standout features of Docksal is its extensibility through Bash scripting, making it a valuable tool for automating various tasks. A common scenario is the need to import databases when initializing projects. The snippet below serves this purpose by scanning a directory for database files and allowing the user to select which one to import.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1"># Get a list of *.mysql | *.sql.gz | *.sql in /db/ and let the user choose which to import</span>
</span></span><span class="line"><span class="cl"><span class="nb">unset</span> options i
</span></span><span class="line"><span class="cl"><span class="k">while</span> <span class="nv">IFS</span><span class="o">=</span> <span class="nb">read</span> -r -d <span class="s1">$&#39;\0&#39;</span> f<span class="p">;</span> <span class="k">do</span>
</span></span><span class="line"><span class="cl">  options<span class="o">[</span>i++<span class="o">]=</span><span class="s2">&#34;</span><span class="nv">$f</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">done</span> &lt; &lt;<span class="o">(</span>find <span class="si">${</span><span class="nv">PROJECT_ROOT</span><span class="si">}</span>/db/ -maxdepth <span class="m">1</span> -type f <span class="se">\(</span> -name <span class="s2">&#34;*.mysql&#34;</span> -o -name <span class="s2">&#34;*.sql.gz&#34;</span> -o -name <span class="s2">&#34;*.sql&#34;</span> <span class="se">\)</span> -print0<span class="o">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;Press [Enter] to choose a DB to import&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">read</span> -t5 -p <span class="s2">&#34;(times out in 5 seconds) &#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="o">[</span> <span class="nv">$?</span> -gt <span class="m">0</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
</span></span><span class="line"><span class="cl">  <span class="nb">echo</span> <span class="s2">&#34;&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="nb">echo</span> <span class="s2">&#34;Timed out waiting for response. Skipping DB import...&#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">else</span>
</span></span><span class="line"><span class="cl">  <span class="k">select</span> opt in <span class="s2">&#34;</span><span class="si">${</span><span class="nv">options</span><span class="p">[@]</span><span class="si">}</span><span class="s2">&#34;</span> <span class="s2">&#34;Skip DB Import&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="k">do</span>
</span></span><span class="line"><span class="cl">    <span class="k">case</span> <span class="nv">$opt</span> in
</span></span><span class="line"><span class="cl">      *.mysql <span class="p">|</span> *.sql.gz <span class="p">|</span> *.sql<span class="o">)</span>
</span></span><span class="line"><span class="cl">        <span class="nb">echo</span> <span class="s2">&#34;Importing </span><span class="nv">$opt</span><span class="s2"> ...&#34;</span>
</span></span><span class="line"><span class="cl">        fin db import <span class="nv">$opt</span>
</span></span><span class="line"><span class="cl">        <span class="nb">break</span>
</span></span><span class="line"><span class="cl">        <span class="p">;;</span>
</span></span><span class="line"><span class="cl">      <span class="s2">&#34;Skip DB Import&#34;</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">        <span class="nb">echo</span> <span class="s2">&#34;Skipping DB Import...&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="nb">break</span>
</span></span><span class="line"><span class="cl">        <span class="p">;;</span>
</span></span><span class="line"><span class="cl">      *<span class="o">)</span>
</span></span><span class="line"><span class="cl">        <span class="nb">echo</span> <span class="s2">&#34;Invalid choice, please enter the number of your choice above.&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="p">;;</span>
</span></span><span class="line"><span class="cl">    <span class="k">esac</span>
</span></span><span class="line"><span class="cl">  <span class="k">done</span>
</span></span><span class="line"><span class="cl"><span class="k">fi</span>
</span></span></code></pre></div><p>This script effectively searches a specified directory for files that match certain extensions and prompts the user to make a selection. The inclusion of a timeout is a thoughtful touch, especially for scripts designed to run unattended or as part of a larger automation workflow.</p>
<h3 id="why-it-matters">Why It Matters</h3>
<p>Understanding how to leverage Bash for such tasks not only enhances your efficiency but also empowers you to create more dynamic and responsive scripts. This approach illustrates the blend of technical skill and strategic foresight that is crucial in today&rsquo;s digital landscape.</p>
<h3 id="insights-to-consider">Insights to Consider</h3>
<p>Bash scripting, while often seen as a technical skill, is inherently about problem-solving. By framing your scripts to engage users, you add a layer of interaction that can significantly improve the user experience.</p>
<h3 id="moving-forward">Moving Forward</h3>
<p>Consider how you can implement similar patterns in your own scripts. Whether it’s adding user interactivity or building in safeguards like timeouts, these small enhancements can lead to more robust and user-friendly automation solutions. Embrace the iterative process—after all, improvement comes from curiosity and experimentation.</p>
]]></content:encoded>
    </item>
    <item>
      <title>How to Use the AWS CLI to List IP Addresses and Other Instance Information</title>
      <link>https://www.sebastientaggart.com/writing/how-to-use-the-aws-cli-to-list-ip-addresses-and-other-instance-information/</link>
      <pubDate>Tue, 05 Mar 2019 15:07:07 -0500</pubDate>
      <guid>https://www.sebastientaggart.com/writing/how-to-use-the-aws-cli-to-list-ip-addresses-and-other-instance-information/</guid>
      <description>&lt;p&gt;When it comes to managing cloud infrastructure, clarity is paramount. Security officers, managers, and other stakeholders often seek a high-level summary of instances within their hosting environment. While visualizations can be enlightening, there are times when raw data is simply more useful. Enter the AWS Command Line Interface (CLI) — a powerful tool that streamlines this process with the &lt;code&gt;describe-instances&lt;/code&gt; command.&lt;/p&gt;
&lt;p&gt;The basic command is straightforward and well-documented. To retrieve a comprehensive overview of your EC2 instances, you can run:&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>When it comes to managing cloud infrastructure, clarity is paramount. Security officers, managers, and other stakeholders often seek a high-level summary of instances within their hosting environment. While visualizations can be enlightening, there are times when raw data is simply more useful. Enter the AWS Command Line Interface (CLI) — a powerful tool that streamlines this process with the <code>describe-instances</code> command.</p>
<p>The basic command is straightforward and well-documented. To retrieve a comprehensive overview of your EC2 instances, you can run:</p>
<pre tabindex="0"><code>aws ec2 describe-instances
</code></pre><p>This command will return a treasure trove of information in JSON format. However, if your goal is to zero in on specific details—say, the IP addresses—you&rsquo;ll want to refine your query.</p>
<p>To list only the Instance IDs along with their public and private IP addresses, use the following command:</p>
<pre tabindex="0"><code>aws ec2 describe-instances \
  --query &#34;Reservations[*].Instances[*].[InstanceId,PublicIpAddress,PrivateIpAddress]&#34; \
  --output=table
</code></pre><p>This will yield a neatly formatted table that highlights the essential information:</p>
<p><img alt="AWS CLI Instance Information" loading="lazy" src="images/How-to-use-the-AWS-CLI-to-list-IP-Addresses-and-other-Instance-Information-a25bef3e.png"></p>
<p>But the AWS CLI’s power doesn’t stop there. If you want to include additional fields—like instance names—you can adjust your command accordingly:</p>
<pre tabindex="0"><code>aws ec2 describe-instances \
  --query &#34;Reservations[*].Instances[*].[InstanceId,PublicIpAddress,PrivateIpAddress,Tags[?Key==&#39;Name&#39;].Value[]]&#34; \
  --output=text
</code></pre><p>With this command, you&rsquo;re not just pulling data; you’re creating a customized view that speaks directly to your needs. The <code>query</code> option is incredibly flexible, allowing for a range of applications beyond what we&rsquo;ve discussed here.</p>
<p>In essence, the AWS CLI can serve as a bridge between complex data and actionable insights. By mastering these commands, you empower yourself to make informed decisions swiftly — a crucial capability in today’s fast-paced digital landscape.</p>
<p>So, the next time you&rsquo;re tasked with providing an overview of your EC2 instances, remember: clarity in your command leads to clarity in your understanding. Happy scripting!</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
