Problem with expr command in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with expr command in shell script
# 1  
Old 12-05-2012
Problem with expr command in shell script

Hi,

I have used expr command to increment the date.

for e.g.,

case 1 :
Code:
echo $(date -d $(echo `expr 20010101 + 1`))

it returns
Code:
Tue Jan 2 00:00:00 IST 2001

case 2:
Code:
echo $(date -d $(echo `expr 20010101 - 1`))

it returns
Code:
date: invalid date `20010100'


please suggest me, how to solve the problem.

Thanks and Regards
Nanthagopal A
# 2  
Old 12-05-2012
No need to use expr, try this instead:-
Code:
date -d'+1 day' +"%Y%m%d"
20121205

Code:
date -d'-1 day' +"%Y%m%d"
20121203

Modify the format as per your requirement.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-05-2012
It does so because 00 isn't a valid day. You can't just add 1 and get a sensible answer.
# 4  
Old 12-05-2012
You can try any other date other than 1 with your code. It will work.

Code:
 echo $(date -d $(echo `expr 20000102 - 1`))
Sat Jan 1 00:00:00 PST 2000

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem using cut command in shell script

I'm new to shell programming, and am having a problem in a (Korn) shell program, which boils down to this: The program reads a record from an input file and then uses a series of "cut" commands to break the record into parts and assign the parts to variables. There are no delimiters in the... (2 Replies)
Discussion started by: joroca
2 Replies

2. Shell Programming and Scripting

help with expr command in script

Hi, I am trying to code a unix function to calculate date difference between two date variables. I am stuck at a point where I am trying to convert hours into minutes. Below is the code I am doing. function get_elapsed_time { export PROPS_FILE=temp.properties export... (8 Replies)
Discussion started by: Nutan
8 Replies

3. Shell Programming and Scripting

Problem Executing Firmware Command using Shell Script

Guys, I have a script that should change one of the configuration Parameter in a http accelerator, this config change which will halt http traffic into device. So I have designed a script which should do these changes. But after executing this script, found that one of the input variable is not... (8 Replies)
Discussion started by: raghunsi
8 Replies

4. Shell Programming and Scripting

problem in exit status of the command in a shell script-FTP

Hi All, I have developed below script for FTP a file from unix machine to another machine. ftpToABC () { USER='xyz' PASSWD='abc' echo "open xx.yy.zbx.aaa user $USER $PASSWD binary echo "put $1 abc.txt" >> /home/tmp/ftp.$$ echo "quit" >> /home/tmp/ftp.$$ ftp -ivn <... (3 Replies)
Discussion started by: RSC1985
3 Replies

5. Shell Programming and Scripting

shell script problem , sudo mount command

cat test.sh sudo mount -t vfat /dev/sda7 /media/Ddrive If i double click the test.sh file and select run in terminal then the terminal prompts for password. How can i avoid typing password? Or if i double click test.sh file and select run then nothing happens. What i'm trying "Double... (3 Replies)
Discussion started by: cola
3 Replies

6. Shell Programming and Scripting

Problem with MV command in Shell Script

Hi Guru's, I 'm trying to execute the below given script in Unix. I am having an issue with the script. The output of the script is given below: #!/bin/bash File_Home="/home/essftp/Risk" cd /home/essftp/Risk rm -f FileList rm -f credit_risk* file1=`ls -lt... (4 Replies)
Discussion started by: ranjith_taurean
4 Replies

7. UNIX for Dummies Questions & Answers

problem with expr command

:) hi Unix gurus, Pls consider the following piece of code str='hello' length=echo $str|wc -c echo $length y= ` expr \( 80 - $length \) ` echo $y :confused: The last echo stmt is displaying 0 as the result. If i put direct value like 6 instead of $length in i 3rd stmt it is giving... (8 Replies)
Discussion started by: ravi raj kumar
8 Replies

8. UNIX for Dummies Questions & Answers

expr problem

Hi, in my ksh script expr 22 / 10 results as 2 but the actual result expected in 2.2. how do i get that result. Please help Thanks, (4 Replies)
Discussion started by: kotasateesh
4 Replies

9. Shell Programming and Scripting

expr problem

Hi, in my ksh script expr 22 / 10 results as 2 but the actual result expected in 2.2. how do i get that result. Please help Thanks, (2 Replies)
Discussion started by: kotasateesh
2 Replies

10. Shell Programming and Scripting

problem with sed command in shell script.

Guys, I've a problem in the "sed" command used in my shellscripts This is the problamatic line in my shell script: sed -e 's/${line1}/${line1_m}/g' prod_hier_1234.txt > test.txt It doesn't do the job of replacing the string stored in variable 'line1' to 'line1_m'. However If I replace the... (10 Replies)
Discussion started by: bhagat.singh-j
10 Replies
Login or Register to Ask a Question