Mohammedz.com

For Linux and Shell scripting.

Bash, version 3

Leave a comment


Here are some good newly added features to the Bash, version 3.

A new, more generalized {a..z} brace expansion operator.
—————————————-
#!/bin/bash

for i in {1..10}
# Simpler and more straightforward than
#+ for i in $(seq 10)
do
echo -n “$i ”
done

echo

# 1 2 3 4 5 6 7 8 9 10

# Or just . . .

echo {a..z}       # a b c d e f g h i j k l m n o p q r s t u v w x y z
echo {e..m}     # e f g h i j k l m
echo {z..a}      # z y x w v u t s r q p o n m l k j i h g f e d c b a
# Works backwards, too.
echo {25..30} # 25 26 27 28 29 30
echo {3..-2}    # 3 2 1 0 -1 -2
echo {X..d}    # X Y Z [ ] ^ _ ` a b c d
# Shows (some of) the ASCII characters between Z and a,
#+ but don’t rely on this type of behavior because . . .
echo {]..a} # {]..a}
# Why?

# You can tack on prefixes and suffixes.
echo “Number #”{1..4}, “…”
# Number #1, Number #2, Number #3, Number #4, …

# You can concatenate brace-expansion sets.
echo {1..3}{x..z}” +” “…”
# 1x + 1y + 1z + 2x + 2y + 2z + 3x + 3y + 3z + …
# Generates an algebraic expression.
# This could be used to find permutations.

# You can nest brace-expansion sets.
echo {{a..c},{1..3}}
# a b c 1 2 3
# The “comma operator” splices together strings.

# Unfortunately, brace expansion does not lend itself to parameterization.
var1=1
var2=5
echo {$var1..$var2} # {1..5}

—————————————-

The ${!array[@]} operator, which expands to all the indices of a given array.
—————————————-
#!/bin/bash

Array=(element-zero element-one element-two element-three)

echo ${Array[0]} # element-zero
# First element of array.

echo ${!Array[@]} # 0 1 2 3
# All the indices of Array.
for i in ${!Array[@]}
do
echo ${Array[i]} # element-zero
# element-one
# element-two
# element-three
#
# All the elements in Array.
done

—————————————-

The =~ Regular Expression matching operator within a double brackets test expression. (Perl has a similar operator.)
—————————————-
#!/bin/bash

variable=”This is a fine mess.”

echo “$variable”
# Regex matching with =~ operator within [[ double brackets ]].
if [[ “$variable” =~ “T………fin*es*” ]]
#    ^                    ^
# NOTE: Quoting not necessary, as of version 3.2 of Bash.
then
echo “match found”
# match found
fi

—————————————-

Or, more usefully:
—————————————-
#!/bin/bash

input=$1

if [[ “$input” =~ “[1-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]” ]]
# ^ NOTE: Quoting not necessary, as of version 3.2 of Bash.
# NNN-NN-NNNN (where each N is a digit). Initial digit must not be 0.
then
echo “Social Security number.”
# Process SSN.
else
echo “Not a Social Security number!”
# Or, ask for corrected input.
fi

—————————————-

Note: All the above examples are taken from Advanced Bash Scripting Guide and I kept here just for my reference.

Thank you,
Mohammed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s