domingo, 27 de outubro de 2013

10 Tips To Write Expeditious Bash Scripts


10 Tips To Write Expeditious Bash Scripts
Bash is indeed one powerful tool, and here are some tips to make bash scripts more efficient!
Bash the command line interface for many Linux distros is one is one powerful tool. Here we bring to you 10 niche tips which will help you keep your Bash scripts efficient and lean.
  1. Avoid Full Paths to Bash Built-ins
    Bash has many built-ins that can be used instead of calling external commands. You should leverage the built-in commands whenever possible since it avoids calling a sub-command from the system.
    Since Bash has built-ins for some commands found in /bin and /usr/bin (such as echo), avoid using the full path for these commands and the built-in will be used.
    # avoid this
    /bin/echo "hello"

    Use the Bash built-in instead:
    echo "hello"
    Other bash built-ins include: test, read, declare, eval, let pushd and popd. You can refer the bash man page a full listing of built-ins.
  2. Avoid External Commands for Integer Math
    Bash also provides built-ins that can be used for integer arithmetic. Only use /usr/bin/bc if you need to do floating point arithmetic. Integer calculations can be made with these Bash built-ins:
    four=$(( 2 + 2 ))
    four=$[ 2 + 2 ]
    let four="2 + 2"

  3. Avoid using Cat
    Tools like Grep, Awk and Sed will take files as arguments. There is rarely a need to use /bin/cat. For instance, the following is unnecessary:
    # avoid this
    cat /etc/hosts | grep localhost

    Instead, use Grep's native ability to read files:
    grep localhost /etc/hosts
  4. Avoid Piping Grep to Awk
    If using Awk, you can often eliminate the need for grep. Try not to pipe Grep to Awk:
    # avoid this
    grep error /var/log/messages | awk '{ print $4 }'

    Use Awk's native ability to parse text and save yourself a command.
    awk '/error/ { print $4 }' /var/log/messages
  5. Avoid Piping Sed to Sed
    Sed can take more than one command in a single execution. Avoid piping sed to sed.
    # avoid this
    sed 's/hello/goodbye/g' filename | sed 's/monday/friday/g'

    Instead, use sed -e or delimit the sed expressions with a semicolon (;)
    sed -e 's/hello/goodbye/g' -e 's/monday/friday/g' filename
    sed -e 's/hello/goodbye/g; s/monday/friday/g' filename

  6. Use Double Brackets for Compound and Regex Tests
    The [ or test built-ins can be used to test expressions, but the [[ built-in operator additionally provides compound commands and regular expression matching.
    if [[ expression1 || expression2 ]]; then do_something; fi
    if [[ string =~ regex ]]; then do_something; fi

  7. Use Functions for Repetitive Tasks
    Break your script up into pieces and use functions to conduct repetitive tasks. Functions can be declared like so:
    function_name() {
      do_something
      return $?
    }

    Make your functions usable by more than one shell script by sourcing a functions file from the various scripts. You can source another file in Bash using the . built-in.
    #!/bin/bash
    . /path/to/shared_functions

    See the Bash man page.
  8. Use Arrays Instead of Multiple Variables
    Bash arrays are very powerful. Avoid using unnecessary variables:
    # avoid this
    color1='Blue'
    color2='Red'
    echo $color1
    echo $color2

    Instead, use Bash arrays.
    colors=('Blue' 'Red')
    echo ${colors[0]}
    echo ${colors[1]}
  9. Use /bin/mktemp to Create Temp Files
    Need a temporary file? Use /bin/mktemp to create temporary files or folders.
    tempfile=$(/bin/mktemp)
    tempdir=$(/bin/mktemp -d)

  10. Use /bin/egrep or /bin/sed for Regex Pattern Matching
    Think you need Perl? Check out Sed or Egrep (grep -e) for regex pattern matching.
    # grep for localhost or 127.0.0.1 in /etc/hosts
    egrep 'localhost|127\.0\.0\.1' /etc/hosts

    # print pattern localhost.* in /etc/hosts
    sed -n 's/localhost.*/&/p' /etc/hosts
Courtesy: hacktux
 Fonte: 10 Tips To Write Expeditious Bash Scripts

Nenhum comentário:

Postar um comentário