<p>I have a test.txt file that has bunch of kubernetes commands. It looks like:</p>
<pre><code>kubectl get cm -o yaml | grep &quot;test&quot;
kubectl get pods
kubectl describe pod xxx
.
.
</code></pre>
<p>bash script read the test.txt and loop thru each command should executes.
However, when I run the script, it gives the following error.
Any ideas how to fix this?</p>
<blockquote>
<p>Error from server (NotFound): configmaps &quot;|&quot; not found</p>
<p>Error from server (NotFound): configmaps &quot;grep&quot; not found</p>
<p>Error from server (NotFound): configmaps &quot;&quot;&quot; not found</p>
</blockquote>
<p>Here is the script:</p>
<pre><code>in_file=test.txt

# Loop over each line
while read -r test_case; do
    &quot;kubectl -n ${namespace} ${test_case}&quot; 
    
done &lt; &quot;$in_file&quot;
</code></pre>

<p>Change your bash script to this:</p>
<pre><code>#!/bin/bash

cat test.txt | xargs -I {} bash -c {}
</code></pre>
<p>Works like a treat:</p>
<pre><code>{&quot;apiVersion&quot;:&quot;v1&quot;,&quot;data&quot;:{&quot;api-key&quot;:&quot;abc123&quot;,&quot;database-url&quot;:&quot;postgresql://localhost/mydatabase&quot;},&quot;kind&quot;:&quot;ConfigMap&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{},&quot;name&quot;:&quot;example-config&quot;,&quot;namespace&quot;:&quot;default&quot;}}
    name: example-config
{&quot;apiVersion&quot;:&quot;v1&quot;,&quot;data&quot;:{&quot;api-key&quot;:&quot;abc123&quot;,&quot;database-url&quot;:&quot;postgresql://localhost/mydatabase&quot;},&quot;kind&quot;:&quot;ConfigMap&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{},&quot;name&quot;:&quot;example-config2&quot;,&quot;namespace&quot;:&quot;default&quot;}}
    name: example-config2
{&quot;apiVersion&quot;:&quot;v1&quot;,&quot;data&quot;:{&quot;api-key&quot;:&quot;abc123&quot;,&quot;database-url&quot;:&quot;postgresql://localhost/mydatabase&quot;},&quot;kind&quot;:&quot;ConfigMap&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{},&quot;name&quot;:&quot;example-config3&quot;,&quot;namespace&quot;:&quot;default&quot;}}
    name: example-config3
</code></pre>
<p>Got the idea from <a href="https://coderwall.com/p/vsx0yg/bash-xargs-as-a-for-loop" rel="nofollow noreferrer">here</a>.
I wasn't sure if it'd work, but it did :)</p>
<p>Hope it helps.</p>
