- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- shell arithmetic operations
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2002 06:58 AM
04-02-2002 06:58 AM
tmp=$extra/100
echo "tmp is $tmp" // displayes 10/100
I also tried the following:
tmp=`expr $extra / 100` // same thing
Does anyone know the best way to perform arithmetic operations in shell?
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2002 07:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2002 07:03 AM
04-02-2002 07:03 AM
Re: shell arithmetic operations
set num1 = 8
set num2 = 2
@ num = ( ${num1} / ${num2} )
echo ${num}
result :
4
See :
man csh
Frank
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2002 07:03 AM
04-02-2002 07:03 AM
Re: shell arithmetic operations
A=300
B=10
C=$(( ${A} / ${B} ))
echo "C = ${C}"
Note: The shell does integer arithmetic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2002 07:04 AM
04-02-2002 07:04 AM
Re: shell arithmetic operations
Try this
TMP=100
TMP1=10
let TMP2=${TMP}/${TMP1}
echo $TMP2 will display 10
Thanks.
Prashant Deshpande.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2002 07:05 AM
04-02-2002 07:05 AM
Re: shell arithmetic operations
* You should have a space before & after the arithmetic sign. Examples :
expr 4 + 3 (gives 7)
expr 4 / 2 (gives 2)
expr 4 \* 3 (gives 12)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2002 07:11 AM
04-02-2002 07:11 AM
Re: shell arithmetic operations
TMP=100
TMP1=10
((TMP2=TMP/TMP1))
echo $TMP2 will display 10
Thanks.
Prashant Deshpande.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2002 08:19 AM
04-02-2002 08:19 AM
Re: shell arithmetic operations
echo "tmp is $tmp"
-raj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2002 02:52 PM
04-02-2002 02:52 PM
Re: shell arithmetic operations
As already noted, the shell provides integer arithmetic. If you want to incorporate real numbers, 'bc' provides a convenient vehicle. Choose the 'scale' to set the precision you want:
# X=1;Y=8;echo "scale=3\n $X/$Y"|bc
You can also leverage 'awk' and couple it with 'printf' to format output in different number bases:
# X=1;Y=8;echo "$X $Y"|awk '{printf "%.4f\n",$1/$2}'
# X=7;Y=8;echo "$X $Y"|awk '{printf "%4x\n",$1+$2}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2002 01:56 AM
04-03-2002 01:56 AM
Re: shell arithmetic operations
Here are the basic arithmetic operations :
1. Addition :
# expr 8 + 2
10
2. substraction :
# expr 8 - 2
6
3. Division :
# expr 8 / 2
4
4. Multiplication :
# expr 8 \* 2
16
Magdi