Frying Bacon (Bash Newbies)

From UCLUG
Jump to: navigation, search

Example 1: fry1[edit]

echo "Frying bacon."

Example 2: fry2[edit]

#!/bin/sh

# Shell script to fry bacon
# introducing shebang, comments, vars, quoting

MEAT=bacon  # The meat variable
METHOD=Fry  # The method variable

echo $METHODing $MEAT.

Example 3: fry3[edit]

#!/bin/bash

# Shell script to fry bacon
# introducing cmdline args, if, test, whitespace, backslash, command separators

MEAT="$1"
METHOD="$0"
ADVERB=$2

#if [ "fry3x" == "${METHOD}x" ]
#if [ "fry3x" = "${METHOD}x" ]
if test "fry3x" = "${METHOD}x"
then
       METHOD='Fry'
fi

echo                   ${METHOD}ing       $MEAT       ${ADVERB}

if [ -z $ADVERB ] ; then
       echo "(there was no \$ADVERB)"
fi

Example 4: fry4[edit]

#!/bin/bash

# Shell script to fry bacon
# introducing source, functions

#./fry.conf
#source fry.conf
. fry.conf

fry_bacon ${MEAT} "$METHOD" $ADVERB

Example: fry.conf[edit]

# Fry bacon config file
MEAT=bacon
METHOD="Fry"
ADVERB=boldly

#function fry_bacon {
#function fry_bacon () {
fry_bacon() {
    echo                       ${1}ing       $2       ${3}
    if [ -z $3 ] ; then
        echo "(there was no \"ADVERB\")"
    fi
}