Bash Calculator
If you need to do quick calculations on a system that provides the
Bash shell,
such as Linux or Mac OS X, you can perform calculations by
using the
echo command and then using
$[ and
] to enclose the arithmetic calculation, i.e.,
echo $[calculation to be performed]. You can use the
standard
arithmetic operators of
+ for addition,
-
for subtraction,
* for multiplication,
/ for
division, and
** for exponentiation.
$ echo $[1+1]
2
$ echo $[9*90]
810
$ echo $[81/9]
9
$ echo $[2**3]
8
The standard
precedence for operators applies, i.e., multiplication
and division have precedence over addition and subtraction, so are performed
first, i.e., the calcuations are not simply done on a left to right basis.
$ echo $[2+3*4]
14
$ echo $[6-4/2]
4
This Bash calculator functionality even handles negative numbers
appropriately.
$ echo $[-4*5]
-20
$ echo $[-4*-5]
20
References:
-
When you need a quick & simple calculator in Bash...
TinyApps.Org, small is beautiful
-
Bash (Unix shell)
Wikipedia, the free encyclopedia
[/os/unix/bash]
permanent link
Chopping Strings in BASH
The
Bourne-again Shell (BASH)
provides built-in mechanisms for extracting substrings from a string.
You can set the value of a variable with
myvar="some text". You can view the value of the variable
using $myvar or ${myvar}. Putting the "$" in
front of the variable name causes BASH to use the value stored in
myvar.
$ myvar="some text"
$ echo $myvar
some text
$ echo ${myvar}
some text
There is sometimes an advantage to the format that encloses the variable
name in curly braces, i.e. the ${myvar} format.
$ echo "foo$myvar"
foosome text
$ echo "foo$myvarother text"
foo text
$ echo "foo${myvar}other text"
foosome textother text
In the above example, in the instance where the curly braces weren't used,
the value of myvar wasn't displayed, because BASH didn't
know I wanted myvar rather than myvarother, which
has no value assigned to it, so it just dispalyed "foo text". In the second
instance where the curly braces were used, BASH could tell I wanted the
vaue of myvar and displayed "foo some textother text".
Substrings can be extracted from a string by chopping characters from the
beginning and end of a string.
Chopping a trailing character:
You can chop a trailing character from a string in BASH by placing the
variable name inside ${ }, such as ${myvar},
and then using %<char>. E.g. suppose myvar
has the value "0064092004008999,". To remove the trailing
comma from the end of the variable, you could use myvar=${myvar%,}
$ myvar="0064092004008999,"
$ echo ${myvar%,}
0064092004008999
If you wanted to remove the last "9" and all characters
that appear after it in the line, you can use "*" in the expression.
$ myvar="0064092004008999,"
$ echo ${myvar%9*}
006409200400899
In the example above, the shortest matching substring is selected
and removed, i.e. the "9,". If you wanted to remove the longest
matching substring, e.g. every character from the first "9" onwards,
you could use
$ myvar="0064092004008999,"
$ echo ${myvar%%9*}
00640
Chopping leading characters:
You can chop leading characters from a string by using # or
##.
E.g. suppose myvar has the value
"SNMPv2-MIB::sysContact.0 = STRING: John Smith". If you only
want the name John Smith, you can use ## to
remove the longest substring containing the ":" character.
I.e., using myvar=${myvar##*:} wold work. If you instead used
only one "#", the shortest matching substring would be removed.
I.e., using myvar=${myvar#*:}
would return :sysContact.0 = STRING: John Smith, where all
characters up to and including the first ":" are removed.
$ myvar="SNMPv2-MIB::sysContact.0 = STRING: John Smith"
$ echo ${myvar##*:}
John Smith
$ echo ${myvar#*:}
:sysContact.0 = STRING: John Smith
References:
-
Bash by example, Part 1
Fundamental programming in the Bourne again shell (bash)
Date: March 1, 2000
IBM
[/os/unix/bash]
permanent link
BASH Variables
Some useful variables available in the
BASH shell.
- $$ = The PID number of the process executing the shell.
- $? = Exit status variable.
- $0 = The name of the command you used to call a program.
- $1 = The first argument on the command line.
- $2 = The second argument on the command line.
- $n = The nth argument on the command line.
- $* = All the arguments on the command line.
- $# The number of command line arguments.
Example:
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Usage: $0 filename"
else
wc -l $1
fi
The script first checks for whether any argument has been entered on the
command line, i.e. whether $# equals zero. If no arguments are present
on the command line, the script prints a usage message. The $0 variable
holds the name of the script itself. If an argument is entered on the
command line, it is presumed to be a filename and the wc command is called
to count the number of lines in the file.
So, if the script is named "example", and is called without any options,
then the following output would be printed.
# ./example
Usage: ./example filename
If a filename is entered on the command line and that file has 21 lines in
it, the following would be printed.
# ./example sample.txt
21 sample.txt
References:
-
Linux Shell Programming
[/os/unix/bash]
permanent link
Bash Tips
I normally use the bash shell
on Unix and Linux systems. A shell is the user interface to the system.
The shell on Unix and Linux systems gives you the type of interface
you get with a command prompt on Windows systems. On older versions of
Windows you would be issuing DOS commands at the command prompt. As you
have batch files with DOS, with Unix and Linux shells you can create
scripts to automate your work, though you normally get a much richer
set of commands than with DOS.
Prior to the development of the bash shell there was a Bourne shell and
the name Bourne Again Shell (bash) comes from the name of that prior shell. The bash shell was created by Brian Fox in 1988. He continued to work on it until
1993. Chet Ramey joined Brian in the development of bash in 1989 and Chet
continued the work on bash after Brian ceased his development efforts on bash.
I've posted a few bash tips in Bash
Tips
[/os/unix/bash]
permanent link