Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If the whole <code>var.txt</code> file contains only Bash-compatible variable assignments as you indicated, you might just be able to <a href="http://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html" rel="noreferrer">source</a> it, to make those variables available in a new Bash script:</p> <pre><code>source var.txt useradd ${input[1]} </code></pre> <p>This, however, will overwrite any existing variable with the same name. <a href="http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution" rel="noreferrer">Command substitution</a> can be used to avoid this, by selecting specific variables:</p> <pre><code>input[1]="$(grep '^input\[1\]=' var.txt | sed "s|[^=]*='\(.*\)'|\1|")" </code></pre> <p>It allows for renaming variables, although you will have to do this for each variable of interest. It essentially extracts the value of the variable from the <code>var.txt</code> file and assigns it to a new variable. See the <a href="http://linux.die.net/man/1/grep" rel="noreferrer">grep manual page</a> and the <a href="http://www.gnu.org/software/sed/manual/html_node/index.html" rel="noreferrer">sed info page</a> for more information on their use.</p> <p><a href="http://www.gnu.org/software/bash/manual/bashref.html#Process-Substitution" rel="noreferrer">Process substitution</a> may allow for simpler expressions:</p> <pre><code>source &lt;(grep '^input\[[0-9]*\]=' var.txt) useradd ${input[1]} </code></pre> <p>This would allow you to import only definitions of interest, although you have to watch for unwanted variable overwrites.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload