Evaluating expressions
You can use expr utility to evaluate expressions in both of the command line or shell script. Here are a few examples:
expr 6 + 4
10
Note the spaces. Without spaces, you get the following:
expr 6+4
6+4
If you are using "*" you will need a "\" before it:
expr 10 \* 10
100
This also work for variables:
var1=2
var1=`expr $var1 \* 2`
echo $var1
4
You can get the cosine(.23):
var1=`echo "c(.23)" | bc -l`
echo $var1
.97366639500537483696
You can also do substrings:
expr substr "BigBear" 4 4
Bear
And length of strings:
mstr="12345"
expr length $mstr
5
Regular expressions:
expr "a3" : [a-z][1-9]
2
Or you can get a bit fancy:
myexpr="[a-z][1-9]"
expr "a3" : $myexpr
2
This may not be the best way to find out if it is Friday, but it seems to work. It's more of an exercise in xargs.
date
Fri Dec 31 16:44:47 EST 2004
date | xargs -i expr {} : "[Fri]"
1
- 0 Comment
- Mike Chirico
- 07 Dec 2008, 10:34
-
You must be login first or sign-up for an account to post comments.
USERBOX
CATEGORIES
MOST READ TODAY
- Syntax highlighting in less
- XFS Filesystem has duplicate UUID problem
- Disabling reverse dns lookups in ssh
- Redirecting tcp / udp ports with socat
- Enabling remote desktop on a VirtualBox Machine
- Remove all frozen messages from exim mail queue
- Users with /bin/false shell to login on vsftpd
- Disabling ssh password authentication
- dropbear rsa key problem
- Passwordless sudo setup
LAST ADDED
- Using iPhone internet sharing over bluetooth under Linux
- Using USB sound card with amarok
- Multi-conditional search and replace (clearing a ftp trojan script example)
- Disabling ipv6 functionality
- How to convert a mp3 file
- How to choose the fastest Debian mirror
- Disabling reverse dns lookups in ssh
- Rewriting destination ip address
- Deleting A File By It's Inode Value
- Learning which libraries are used for a binary
