Bash basics: Difference between revisions

From Bitpost wiki
No edit summary
No edit summary
Line 15: Line 15:
* To capture output
* To capture output
<pre>myout=`program1 param`;</pre>
<pre>myout=`program1 param`;</pre>
* Note the difference a single quote makes to output...
<pre>$ eval echo "abc  def"
abc def
$ eval 'echo "abc  def"'
abc  def
</pre>
* To check for an error
* To check for an error
<pre>mkdir "$d2"
<pre>mkdir "$d2"

Revision as of 15:01, 11 January 2012

Bash certainly has its set of snafus.

  • To store commands in variables:
# NOTE don't put spaces around equals sign!
# NOTE don't use ampersands within command strings, they won't work
# use a blank command if needed, that's fine
mycmd1="command param param"
mycmd2="cmd2 param"
if [ -e /myspecialplace ]
mycmd2=""
fi
mycmd3="cmd3 param"
$mycmd1 && $mycmd2 && $mycmd3
  • To capture output
myout=`program1 param`;
  • Note the difference a single quote makes to output...
$ eval echo "abc   def"
abc def
$ eval 'echo "abc   def"'
abc   def
  • To check for an error
mkdir "$d2"
if [ $? -gt 0 ]; then
   echo "mkdir error, try that again..."
   exit 3
fi
  • To read input from user
echo "Create the subdirectory?  [$d2]"
if read answer; then
    if [ ${answer} = "y" ] || [ ${answer} = "yes" ]; then
        {whatever}
    else
        {whatever}
    fi
fi