Learning Bash, by example and counter-example

What is Bash?

Bash is an interactive shell. You type in commands and Bash executes them.

At the same time, Bash is also a scripting language.

Bash as a scripting language

To create a Bash script, place #!/bin/bash at the top of the file.

Then, change the permission of the file:

$ chmod u+x scriptname

To execute the script run ./scriptname from the current directory and pass parameters if required.

Here’s an example of a Bash script that uses an argument

#!/bin/bash

# Use $1 to get the first argument

echo $1

Variables and Arrays

Variables in Bash act both as a variable and also as an array.

To set a variable use ‘=’:

foo=3     #sets foo to 3

You cannot use spaces while instantiating a variable

foo = 3     #error: invokes command ‘foo’ with arguments ‘=’ and ‘3’

You can delete a variable using unset :

foo=42

echo $foo     #prints 42

unset foo

echo $foo     #prints nothing

You can use any variable as an array:

foo[0]=”first”     #sets the first element to “first”

foo[1]=”second”     #sets the second element to  “second”

String Replacement

Bash can replace one string with another:

foo=”I am a cat.”

echo $(foo/cat/dog)     #prints “I am a dog.”

Double slashes replace all instances of a string:

foo=”I am a cat and she is a cat.”

echo $(foo/cat/dog)     #prints “I am a dog and she is a cat.”

echo $(foo//cat/dog)     #prints “I am a dog and she is a dog.”

To delete an instance, use slash without replacement:

foo=”I like meatballs.”

echo $(foo/balls)     #prints “I like meat.”

The prefix operator # counts the number of characters in a string or the number of elements in an array.

ARRAY=(a b c)

echo ${#ARRAY[0]}     #prints 1

echo ${ARRAY[@]}     #prints 3

Scope

In Bash, scope depends on the processes, i.e. each process has its own copy of variables:

foo=42

bash somescript     #somescript cannot see foo

export foo

bash somescript     #somescript can see foo

echo “foo = ” $foo     #prints foo = 42

Expressions and Arithmetic

The command expr prints the result of all arithmetic expression:

expr 3 + 12     #prints 15

expr 3 \* 12     #prints 36

Similar results can be obtained using the ((assignment = expression)).

((x = 3 + 12)); echo $x    #prints 15

((x = 3 * 12)); echo $x     #prints 36

Use declare –i variable to create an explicit integer variable:

declare -i number

number=2+4*10

echo $number     #prints 42

Pipes

Pipes are used to chain commands together.

The general form of pipe operator is as follows:

outputting-command | inputing-command

Following example depicts the usage of pipes:

#A one liner to find hogs in the current working directory:

# du – cks *     #prints out the space usage of the files in the current directory

# sort -rn     #sorts STDIN, numerically, by the first column in reverse order

# head     #prints the first 10 entries from STDIN

du -cks * | sort -rn | head

Conditionals

If else type of conditional exists in Bash, just like in other programming languages.

An exit status of success is ‘true’ while an exit status of fail (non-zero) is ‘false’.

# this will print:

if true

then

echo printed

fi

Iterations

In Bash while command; do command;  done executes until the test command completes with non-zero exit status.

#automatically restart http in case it crashes:

while true

do

httpd

done

Subroutines

There are two different ways of defining subroutines in Bash.

function name {

commands

}

and

name() {

commands

}

Once declared, the subroutines almost act like a separate script.

The following example shows the working of a subroutine.

count=20

function showcount {

echo $count

count=30

}

showcount     #prints 20

echo $count     #prints 30

That’s all folks.

 

 

 

 

 

 

 

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