From charlesreid1

Line 314: Line 314:
|}
|}


== Basic Math Operators ==
==Basic Math==


{{Main|Bash Math#Basic Math}}
{{Main|Bash Math#Basic Math}}

Revision as of 07:13, 28 May 2011

Bash Guide

Using Bash

Setting the default shell: http://www.unix.com/shell-programming-scripting/32664-how-change-default-shell-linux.html

You can set the default shell in Mac's Terminal by going settings and specifying the login shell.

Startup Process

The order for sourcing dot files in bash is as follows:

Once per login session:

1. source /etc/profile - this contains the system-wide profile settings; these are only executed once per login session, and should define any bash variables that need to be defined for everyone; examples include the ${PATH}, the ${MANPATH}, the machine name, and/or enabling things that are not bash-specific (remember the Csh shell will also source .profile).

2. source ~/.profile - this contains user-specific profile settings; these settings are the same as above, except they are variables that should only be set for one user. The user's ${PATH} and ${MANPATH} variables, in particular, should be set here.

See my dotprofile

3. source ~/.bash_profile - (?) This one I'm not so sure about, it's not a "standard" bash file to be sourced. Plus its role is a little vague. I use this to define settings for my bash history.

See my dotbash_profile

4. source /etc/bashrc - sources the system-wide bashrc file. This will define the default appearance of the command prompt, any aliases, or any settings related to the bash history that should apply to every user.

5. source ~/.bashrc - sources the user-specific bashrc file. This contains user-specific settings for the prompt, aliases, functions, etc.

See my dotbashrc

Dot Files

Bash Scripting

Looping

Looping in bash is really simple, and convenient - you can use other unix commands (most obviously "ls") to create lists, and then loop over each element of those lists. For example, if I want to print out the name of every file in my home directory (granted, not very useful, but this is just an example), I can do this:

for i in `/bin/ls -1 $HOME`; do
    echo $i
done

To do some serious bash looping kung-fu, check out Xargs.

Logical Operators

NOTE: the use of double brackets for logical condition checks is highly recommended.

There are several logical operators available for checking conditions in bash.

These can be tested using a simple "if" statement. In bash, if statements are of the form:

if [[ condition ]]; then
    cmd
else
    cmd2
fi

The spaces between the brackets and the condition are essential.

Integer Operators

Operator Meaning Example
-eq returns true if arguments are equal
$ a=1; b=1; if [[ "$a" -eq "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=1; b=2; if [[ "$a" -eq "$b" ]]; then echo "true"; else echo "false"; fi
false
-ne returns true if arguments are not equal
$ a=1; b=1; if [[ "$a" -ne "$b" ]]; then echo "true"; else echo "false"; fi
false

$ a=1; b=2; if [[ "$a" -ne "$b" ]]; then echo "true"; else echo "false"; fi
true
-gt returns true if argument 1 is greater than argument 2
$ a=5; b=10; if [[ "$a" -gt "$b" ]]; then echo "true"; else echo "false"; fi
false
-ge returns true if argument 1 is greater than or equal to argument 2
$ a=15; b=10; if [[ "$a" -ge "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=10; b=10; if [[ "$a" -ge "$b" ]]; then echo "true"; else echo "false"; fi
true
-lt returns true if argument 1 less than argument 2
$ a=5; b=10; if [[ "$a" -lt "$b" ]]; then echo "true"; else echo "false"; fi
true
-le returns true if argument 1 less than or equal to argument 2
$ a=15; b=10; if [[ "$a" -le "$b" ]]; then echo "true"; else echo "false"; fi
false

$ a=10; b=10; if [[ "$a" -le "$b" ]]; then echo "true"; else echo "false"; fi
true
\< returns true if argument 1 less than argument 2

this needs to be escaped with a \ if it occurs inside single brackets; otherwise it can appear between double bracket or between double parentheses

$ a=10; b=10; if [ "$a" \< "$b" ]; then echo "true"; else echo "false"; fi
false

$ a=10; b=10; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
false

$ a=10; b=10; if (( "$a" < "$b" )); then echo "true"; else echo "false"; fi
false
<= returns true if argument 1 less than or equal to argument 2

this operator needs to go inside double parentheses

$ a=10; b=10; if (( "$a" <= "$b" )); then echo "true"; else echo "false"; fi
true
> returns true if argument 1 greater than argument 2

this needs to be escaped with a \ if it occurs inside single brackets; otherwise it can appear between double bracket or between double parentheses

$ z=10; zz=1000; 

$ if [ zz \> z ]; then echo "true"; else echo "false"; fi
true

$ if [[ zz > z ]]; then echo "true"; else echo "false"; fi
true
>= returns true if argument 1 greater than or equal to argument 2

this operator needs to go inside double parentheses

$ a=50; b=10; if (( $a >= $b )); then echo "true"; else echo "false"; fi
true

String Operators

Operator Meaning Example
= returns true if two strings are equal
$ a=foo; b=foo; if [[ "$a" = "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=foo; b=bar; if [[ "$a" = "$b" ]]; then echo "true"; else echo "false"; fi
false
== returns true if two strings are equal; equivalent to =
$ a=foo; b=foo; if [[ "$a" == "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=foo; b=bar; if [[ "$a" == "$b" ]]; then echo "true"; else echo "false"; fi
false

Note the difference between quoting a string and not quoting a string:

[[ $a ==  b*  ]] # returns true if $a starts with b (pattern matching)
[[ $a == "b*" ]] # returns true if $a is literally equal to b* (literal matching)
!= returns true if strings are not equal
$ a=foo; b=bar; if [[ "$a" != "$b" ]]; then echo "true"; else echo "false"; fi
true
< returns true if argument 1 is less than (alphabetically) argument 2; capitals come before non-capitals
$ a=foo; b=bar; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
false

$ a=Foo; b=bar; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=bar; b=foo; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
true
> returns true if argument 1 is greater than (alphabetically) argument 2; capitals come before non-capitals
$ a=foo; b=bar; if [[ "$a" > "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=Foo; b=bar; if [[ "$a" > "$b" ]]; then echo "true"; else echo "false"; fi
false
-z returns true if the string is null
$ a=""; b="foobar"; if [ -z "$a" ]; then echo "true"; else echo "false"; fi
true

$ a=""; b="foobar"; if [ -z "$b" ]; then echo "true"; else echo "false"; fi
false
-n returns true if the string is NOT null
$ a=""; b="foobar"; if [ -n "$a" ]; then echo "true"; else echo "false"; fi
false

$ a=""; b="foobar"; if [ -n "$b" ]; then echo "true"; else echo "false"; fi
true


Compound Operators

You can combine logical operators using and/or operators:

Operator Meaning Example
-a Logical and (both conditions must be true for condition to be true)
if [ "$expr1" -a "$expr2" ]
then
  echo "Both expr1 and expr2 are true."
else
  echo "Either expr1 or expr2 is false."
fi
-o Logical or (either condition must be true for condition to be true)
if [ "$expr1" -o "$expr2" ]
then
  echo "Either expr1 or expr2 is true."
else
  echo "Both expr1 and expr2 are false."
fi

Basic Math

Complex Math Operators

Bash Keyboard Shortcuts

Editing Shortcuts

The following are keyboard shortcuts for easy navigation/editing of commands that are on the Bash command line.

Movement

  • C-a - Move to beginning of line
  • C-e - Move to end of line


  • M-f - Move forward 1 word
  • M-b - Move backward 1 word


  • C-f - Move forward 1 character
  • C-b - Move backward 1 character

Cutting and Pasting

  • C-k - kill (cut) from cursor to end of line
  • C-w - kill (cut) from cursor to previous whitespace
  • C-y - yank (paste) previously killed text at cursor

Uncategorized

  • C-L - clear screen, reprint current line at top
  • C-u - undo last edit
  • C-d - delete character under cursor

References

Floating point math in the shell:

http://www.novell.com/coolsolutions/tools/17043.html

Fast bash math:

http://www.bytemycode.com/snippets/snippet/350/

Bash script iterate through array of values

http://www.tech-recipes.com/rx/636/bash-shell-script-iterate-through-array-values/