Substraction in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substraction in shell scripting
# 1  
Old 12-10-2012
Substraction in shell scripting

Hello friends,

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

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

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by radoulov; 12-10-2012 at 10:07 AM..
# 2  
Old 12-10-2012
Quote:
Originally Posted by sanjay833i
#!/bin/sh
current=1355147377
echo $current
last_modified=1354537347
echo $last_modified
DIFF='expr ($current - $last_modified)'
echo $DIFF
try this

Code:
DIFF=`expr $current - $last_modified`

or
Code:
DIFF=$(expr $current - $last_modified)

# 3  
Old 12-10-2012
Hi try this one

Use the symbol on that is on "~" on your key board
Code:
DIFF=`expr $current - $last_modified`

# 4  
Old 12-11-2012
its now working,error i am getting

expr: non-numeric argument
: command not found:

i am more describing my work file
Code:
#!/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 !
Code:
current=$(date +"%s")
last_modified=$(stat -c %Y ../offerDetails.php)
DIFF=$(($(($current-$last_modified))))
echo $DIFF

# 5  
Old 12-11-2012
Try:
Code:
DIFF=$((current-last_modified))

# 6  
Old 12-11-2012
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
# 7  
Old 12-11-2012
$(( .. )) 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)
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substraction of matching lines from a file.

I have 2 files: file1.txt contains /html/mybook/Charts/143712/reptiles.pdf /html/mybook/Charts/198459/spices.pdf /html/mybook/Charts/198459/fresh_nuts.pdf /html/mybook/Charts/123457/dome_anim.pdf /html/mybook/Charts/123457/vegetables.pdf /html/content/3DInteractive/174091/CSPSGGB.html ... (6 Replies)
Discussion started by: Jojan Paul
6 Replies

2. Shell Programming and Scripting

Problem in algebraic substraction

my code is like this count=`cat /filecount.txt | tail -1 |head -1| awk '{print $1}'` ###file is having value 264 #### echo "actual count = $count" exact_count=`expr $value \* 24` echo "exact_count= $exact_count" diff=`expr "$exact_count" - "$count"` a= exact_count - count ... (8 Replies)
Discussion started by: sagar_1986
8 Replies

3. Shell Programming and Scripting

date substraction

hello i have obtained the current date .. current_date=date "+%m/%d%y" and i have another date ,stored in my log file which i have already retrieved. i want to store the subtraction in a varible called diff. diff=log_date - currentdate ex: log_date=01/28/11 current_date=... (3 Replies)
Discussion started by: urfrnddpk
3 Replies

4. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

5. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

6. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

7. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

8. Solaris

Substraction in bash

Hi all, I have in one script something like this: FIRSTOCC=`grep -n ORA- alert_bill2.log |tail -"$ROWS"|head -1|cut -d: -f1` TOTAL=`more alert*|wc -l` DIFFERENCE=`$TOTAL-$FIRSTOCC` echo Total lines in alert_bill = $TOTAL echo $DIFFERENCE How do I make this substraction work? Thk (2 Replies)
Discussion started by: mclaudiu
2 Replies

9. UNIX for Dummies Questions & Answers

Date Substraction

In Unix script, how to get a "date - 1" ie, yesterday? (4 Replies)
Discussion started by: AC
4 Replies
Login or Register to Ask a Question