Substraction in shell scripting

Hello friends,

I am new on linux, i am facing issues on below script.

#!/bin/sh
current=1355147377
echo $current
last_modified=1354537347
echo $last_modified
DIFF='expr ($current - $last_modified)' 
echo $DIFF

try this

DIFF=`expr $current - $last_modified`

or

DIFF=$(expr $current - $last_modified)

Hi try this one

Use the symbol on that is on "~" on your key board

DIFF=`expr $current - $last_modified`

its now working,error i am getting

expr: non-numeric argument
: command not found:

i am more describing my work file

#!/bin/sh
current=$(date +"%s")
echo $current
last_modified=$(stat -c %Y franchise_form.html)
echo $last_modified
DIFF=$(expr $current - $last_modified) 
echo $DIFF

---------- Post updated 12-11-12 at 05:07 PM ---------- Previous update was 12-10-12 at 09:15 PM ----------

its resolved !

current=$(date +"%s")
last_modified=$(stat -c %Y ../offerDetails.php)
DIFF=$(($(($current-$last_modified))))
echo $DIFF

Try:

DIFF=$((current-last_modified))

As a note, $(( )) does not work in some /bin/sh. You should specify /bin/bash or /bin/ksh when using features that require /bin/bash or /bin/ksh

$(( .. )) is part of the POSIX specification, so in practice it should work with /bin/sh on almost any modern Unix / Linux (On Solaris <=10 use /usr/xpg4/bin/sh )

1 Like

Yes and no. The POSIX Standards and the Single UNIX Specifications require that the standards-conforming shell be named sh and that it support arithmetic expansions (i.e., $(()) ), but the standard makes absolutely no requirement that it (or any other standard utility) exist as a file in any particular directory. There is no requirement in the standard that there be a directory named /bin nor that any utility in the standard exist in that directory. The POSIX standards leave a chicken versus egg question about how you find the standard utilities. The documentation for a conforming implementation is required to document how to find conforming utilities on that implementation. Once you know the absolute pathname of the conforming getconf utility, the command:

getconf PATH

will return a set of directories that can be placed at the front of your setting of $PATH that will find all of the standard utilities. If your implementation concurrently supports more that one standards conforming environment (such as Solaris systems), there will be more than one standards conforming version of getconf (at least one for each environment supporting standards with conflicting requirements). On Solaris systems, /usr/bin/getconf gives you an environment that conforms to System V Interface Definition Issue 3 (SVID3) and X/Open Portability Guide Version 3 (XPG3); /usr/xpg4/bin/getconf gives you an environment that conforms to POSIX.2, POSIX.2a, Single UNIX Specification original version (SUS), SUSv2, and XPG4; and /usr/xpg6/bin/getconf gives you an environment that conforms to POSIX.1-2001 and SUSv3. More details on how Solaris Systems support multiple standards concurrently can be found in the OpenSolaris STANDARDS(5) man page in the man page sets provided on this site.

Hi, yes, but that is why I wrote "in practice". Of all the current Unixen that aspire to be POSIX compliant, Solaris < Version 11 is one of the few (how many others are there?) that does not have a POSIX compliant shell (or a near compliant one) as /bin/sh (even though this is not required or even specified by the standard). So I agree it is best to not rely on this and not specify a shebang and use the proper getconf, but in practice this should work (with the exception mentioned above)..