- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- String to numeric
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
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-13-2008 09:09 AM
04-13-2008 09:09 AM
String to numeric
I have a simple script as below
dt=20081125
dttb="20081126"
if [ `expr "$dt" - "$dtb"` -eq 1 ]; then
echo "converting"
else
echo "not converting"
fi
How do i convert the variable "dttb" to a numeric,i tried something like this sed 's/"//g' but still no success.
I want the bash script if statement to evaluate to true.How do i do this? I searched the forum but did not get any positive result.
Regards,
Gyan
- Tags:
- expr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2008 11:12 AM
04-13-2008 11:12 AM
Re: String to numeric
First, you mis-spelled one of your variables --- once it is 'dttb' and once it is 'dtb'.
Second, it appears that you are subtracting the larger value from the smaller and then expecting the result to be a postive value.
Try something like:
#!/usr/bin/sh
dt=20081125
dttb="20081126"
if [ `expr "$dttb" - "$dt"` -eq 1 ]; then
echo "converting"
else
echo "not converting"
fi
...which when run, outputs"
converting
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2008 11:28 AM
04-13-2008 11:28 AM
Re: String to numeric
It is helpful in scripts to add 'set -u' which causes the shell to treat unset parameters as an error when substituting them. Had you done this with this script:
# cat ./myexpr
#!/usr/bin/sh
set -u
dt=20081125
dttb="20081126"
if [ `expr "$dtb" - "$dt"` -eq 1 ]; then
echo "converting"
else
echo "not converting"
fi
...when you ran it, your output would be:
#./myexpr
./myexpr[5]: dtb: Parameter not set.
./myexpr[5]: test: Specify a parameter with this command.
not converting
This tells you that something is amiss since a "Parameter [is] not set". As noted, originally, the correct line is:
if [ `expr "$dttb" - "$dt"` -eq 1 ]; then
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2008 05:35 PM
04-13-2008 05:35 PM
Re: String to numeric
If using a real shell, you should use the Arithmetic Evaluation operator:
if [ $((dt - dttb)) -eq 1 ]; then
You can also use it for the whole expression:
if (( dt - dttb == 1 )); then
(Unfortunately I can't remember if the result of this expression is 1 for C, so that the "if" will then be false?)
>JRF: it appears that you are subtracting the larger value from the smaller and then expecting
I assume Gyankr cares about is the answer 1. Perhaps with that negative result, Gyankr wants to test the false case? :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2008 10:58 PM
04-13-2008 10:58 PM
Re: String to numeric
Regards,
Gyan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 04:51 PM
04-14-2008 04:51 PM
Re: String to numeric
>(Unfortunately I can't remember if the result of this expression is 1 for C,
It works fine. The exit status of let is 0 if the result is non-zero.