Bash: Difference between revisions

From XPUB & Lens-Based wiki
Line 82: Line 82:
== Loops ==
== Loops ==


* [[BashForLoops | For Loops]]
C-style counting loop
 
<source lang="bash">
for ((i=0; i<10; i++))
do
echo $i
done
</source>
 
A for loop is a way to ''consider the possibilities'' of a list of things. The code placed ''inside'' the "body" of the loop gets performed for each of the things in the list of possibilities.
 
<source lang="bash">
for n in bob carol ted alice
do
  echo $n
done
</source>
 
would produce the output:
 
<source lang="bash">
bob
carol
ted
alice
</source>
 
Technically, a for loop is the same as assigning a variable (you provide the name) to each value in the list in turn, and each time running the code inside the loop. If we were to "unwrap the loop" it would look like this:
 
<source lang="bash">
n=bob
echo $n
n=carol
echo $n
n=ted
echo $n
n=alice
echo $n
</source>
 
Some variations of the above using a variable, and the contents of a file for the list of possibilities, both would produce the same output as above.
 
<source lang="bash">
names="bob carol ted alice"
for n in $names
do
echo $n
done
 
for n in `cat names.txt`
do
echo $n
done
</source>
 
A ''nested loop'' allows you to consider all possible 'combinations'.
 
<source lang="bash">
for n1 in $names
do
  for n2 in $names
  do
    echo $n1 and $n2
  done
done
</source>
 
would produce the output:
 
<source lang="bash">
bob and bob
bob and carol
bob and ted
bob and alice
carol and bob
carol and carol
carol and ted
carol and alice
ted and bob
ted and carol
ted and ted
ted and alice
alice and bob
alice and carol
alice and ted
alice and alice
</source>
 
other examples of for loops:
 
<source lang="bash">
#!/bin/bash
 
for x in one two three
do
: echo $x
done
 
echo ---
 
for x in *.sh
do
: echo $x
done
 
echo ---
 
for x in `cat colors.txt`
do
: echo $x
done
</source>
 
for the seasoned programmer, the (( )) construct allows for "C-like" loops: [[BR]]
http://tldp.org/LDP/abs/html/loops1.html#FORLOOPC
 
* [[BashWhileLoops | While Loops]]
* [[BashWhileLoops | While Loops]]



Revision as of 00:40, 12 October 2010

Bash is the program that is the command line itself. BASH stands for the "Bourne-Again" Shell, the default shell for Linux and Mac OS X (Bourne-again in reference an original UNIX shell written by Stephen Bourne; BASH is a free software rewrite, part of the GNU project).

BASH scripts are, in the simplest form, just a sequence of command line commands saved in a file (or files). In this way, you can save a complicated series of steps in a single file (or BASH script). Using variables, you can turn a script into a tool that can, for instance, be applied to different input files, or perform an image transformation to a variable degree.

Variables

name="Francesco"
echo Hello there name
echo Hello there $name

Unlike most scripting languages, Bash is not forgiving of extra spaces and gets confused if you add them. Notice also how unlike most programming languages, in Bash text is text first, and interpreted as an expression by exception. It may be helpful to think of the $ as meaning substitute the value.

The use of curly braces around the name is optional, but is helpful to sometimes mark the end of a name if necessary.

name=Michael
echo Hello there $name!
echo $name123
echo ${name}123

There are useful special forms of variable substitution in Bash:

${name:-Joe} Return a default value if the variable is undefined
If name exists and isn't null, return its value; otherwise, return 'Joe'.
${name:offset:length} Substring
String starting from offset, up to length chars. Length can be omitted in which case you get the rest of the text. A negative offset is relative to the end (-1 means last character).
${#name} Length
Returns the length of a variable.
${name:=Joe} Sets a default value if the variable is undefined
Return the value of name if it exists and isn't null; otherwise, set it to 'Joe' and return that.
${name:+value} Test for a variable being set
Return the value if name is defined
${name:?message} Catches errors from a variable being undefined
Return the value of name if it exists and isn't null; otherwise, prints the message and stops the script (though not guaranteed to stop -- depends on shell?)

"Pattern-strippers" (with ShellWildcards, note that these are different from a *Regular Expression*)

${variable#pattern} Delete pattern \from start (smallest match)
${variable##pattern} Delete pattern \from start (longest match)
${variable%pattern} Delete pattern \from end (smallest match)
${variable%%pattern} Delete pattern \from end (longest match)

Arithmetic

You can do some basic arithmetic in the Bash. Note that all match is whole-number based -- doing maths with fractional numbers requires a different scripting language (like Python!)... but for basics, Bash is fine.

echo $((1+2))
echo $((243-800))
echo $((10/3))
echo $((10*234))

See also: [1]

Command Substitution

Command substitution is very powerful, allowing you to run a command and capture the result in a variable, to then use in another command. There are two forms for doing this, $() and "backticks".

Hey there $(whoami).

Backticks (angled-backward single quotes -- find them on your keyboard!) also work:

today=`date +%A`
echo Hello, today is $today

Loops

C-style counting loop

for ((i=0; i<10; i++))
do
echo $i
done

A for loop is a way to consider the possibilities of a list of things. The code placed inside the "body" of the loop gets performed for each of the things in the list of possibilities.

for n in bob carol ted alice
do
  echo $n
done

would produce the output:

bob
carol
ted
alice

Technically, a for loop is the same as assigning a variable (you provide the name) to each value in the list in turn, and each time running the code inside the loop. If we were to "unwrap the loop" it would look like this:

n=bob
echo $n
n=carol
echo $n
n=ted
echo $n
n=alice
echo $n

Some variations of the above using a variable, and the contents of a file for the list of possibilities, both would produce the same output as above.

names="bob carol ted alice"
for n in $names
do
	echo $n
done

for n in `cat names.txt`
do
	echo $n
done

A nested loop allows you to consider all possible 'combinations'.

for n1 in $names
do
  for n2 in $names
  do
    echo $n1 and $n2
  done
done

would produce the output:

bob and bob
bob and carol
bob and ted
bob and alice
carol and bob
carol and carol
carol and ted
carol and alice
ted and bob
ted and carol
ted and ted
ted and alice
alice and bob
alice and carol
alice and ted
alice and alice

other examples of for loops:

#!/bin/bash

for x in one two three
do
: echo $x
done

echo ---

for x in *.sh
do
: echo $x
done

echo ---

for x in `cat colors.txt`
do
: echo $x
done

for the seasoned programmer, the (( )) construct allows for "C-like" loops: BR http://tldp.org/LDP/abs/html/loops1.html#FORLOOPC

If-Then-Else Braches

Functions

Resources

Other tutorials for BASH scripting:

Related pages

MediaBashing MoreMediaBashing grep