Unrecognised error in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unrecognised error in script
# 1  
Old 07-14-2010
Bug Unrecognised error in script

Hi I am using the below script to calculate the yesterdays date

Code:
 
Code:
#! /bin/sh
set -x
YEAR=`date +"%Y"`
NMONTH=`date +"%m"`
DATE=`date +"%d"`
if [ $DATE -eq 01  ];then
case $NMONTH in
01|02|04|06|08|09|11)
       DATE=31
        if [[ $NMONTH == "01" || $NMONTH == "1" ]]; then
                YEAR=`expr $YEAR - 1`
                NMONTH=12;LMONTH="December"
        else
         NMONTH=`expr $NMONTH - 1`
        fi;;

03)     if [[ `expr $YEAR % 4` -eq  0 ]]; then
                DATE=29
        else
                DATE=28
        fi
        NMONTH=`expr $NMONTH - 1`;;
*)
        DATE=30
        NMONTH=`expr $NMONTH - 1` ;;
esac
else
       DATE=`expr $DATE - 1`
      echo $DATE
fi
if [ ${#NMONTH} -eq 1 ]; then
  NMONTH="0$NMONTH"
fi
if [ ${#DATE} -eq 1 ]; then
 DATE="0$DATE"
fi
case $NMONTH in # set long month name
01)     LMONTH="January";;
02)     LMONTH="February";;
03)     LMONTH="March";;
04)     LMONTH="April";;
05)     LMONTH="May";;
06)     LMONTH="June";;
07)     LMONTH="July";;
08)     LMONTH="August";;
09)     LMONTH="September";;
10)     LMONTH="October";;
11)     LMONTH="November";;
12)     LMONTH="December";;
esac
echo $DATE-$NMONTH-$YEAR #Output date-month-year
the o/p that i am getting is
Code:
+ date +%Y
YEAR=2010
+ date +%m
NMONTH=07
+ date +%d
DATE=14
+ [ 14 -eq 01 ]
+ expr 14 - 1
DATE=13
+ echo 13
13
date bad substitution



I am using a Sun Solaris 10 system

Pls advise

---------- Post updated at 06:49 AM ---------- Previous update was at 06:05 AM ----------

Hi all

Any idea why I am getting a date bad substitution error

Pls advise

Last edited by ultimatix; 07-14-2010 at 11:11 AM..
# 2  
Old 07-14-2010
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
# 3  
Old 07-15-2010
Hi ultimatix,


Instead of writing a shell script you may use the date command as follows:

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


you can can replace 1 with any number .

Last edited by radoulov; 07-15-2010 at 09:44 AM.. Reason: Code tags, please!
# 4  
Old 07-15-2010
Bug

Hi annesh

Thanks for the oneliner, but will account for leap years ( Feb ) and 30 / 31 day months ?

:-)
# 5  
Old 07-15-2010
Yes, it will. Do you really have the GNU date on Solaris 10?

If the GNU date is not available, you could use Perl:

Code:
perl -le 'print ~~localtime time - 86400'

# 6  
Old 07-15-2010
In my shell there is a syntax error. The complex condition can be replaced with a simple test by treating $NMONTH as a number.

Code:
#        if [[ $NMONTH == "01" || $NMONTH == "1" ]]; then
        if [ $NMONTH -eq 1 ]; then



Footnote. In this part of the world the parts of a DATE are DAY-MONTH-YEAR (not DATE-MONTH-YEAR).
# 7  
Old 07-16-2010
Bug

hi annesh

it gives o/p as

date +%d-%m-%Y -d "1 day ago"

16-07-2010

ie current date

---------- Post updated at 01:21 AM ---------- Previous update was at 01:18 AM ----------

@ methyk

I still get the same o/p

Code:
 

+ date +%Y
YEAR=2010
+ date +%m
NMONTH=07
+ date +%d
DATE=16
+ [ 16 -eq 01 ]
+ expr 16 - 1
DATE=15
+ echo 15
15
./date.sh: bad substitution

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script with sql script error

Hi All when I execute from psql prompt, I get the result, when I try to automate using a shell script, the query is not working # `/usr/bin/psql -U postgres -d coba1 -c "select name from users where "Date" > current_date - 30;"` ERROR: column "Date" does not exist LINE 1: select... (2 Replies)
Discussion started by: srilinux09
2 Replies

2. Shell Programming and Scripting

Calling shell script within awk script throws error

I am getting the following error while passing parameter to a shell script called within awk script. Any idea what's causing this issue and how to ix it ? Thanks sh: -c: line 0: syntax error near unexpected token `newline' sh: -c: line 0: `./billdatecalc.sh ... (10 Replies)
Discussion started by: Sudhakar333
10 Replies

3. Shell Programming and Scripting

Help with FTP Script which is causing "syntax error: unexpected end of file" Error

Hi All, Please hav a look at the below peice of script and let me know if there are any syntax errors. i found that the below peice of Script is causing issue. when i use SFTP its working fine, but there is a demand to use FTP only. please find below code and explain if anything is wrong... (1 Reply)
Discussion started by: mahi_mayu069
1 Replies

4. Shell Programming and Scripting

Error in calling a shell script from another script

HI, We are using two shell scripts, script.sh,env.sh, where env.sh will be called inside script.sh. The variable inside env.sh is used as $var in script.sh.But while running the script its not identifying that variable. Is there any permission needed to call a script inside another script. ... (3 Replies)
Discussion started by: banupriyat
3 Replies

5. UNIX for Dummies Questions & Answers

Re: Script Error [syntax error at line]

Hi , I Have Written A Simple Script To Check Greatest Of '2' Number When Execuating The Script I Am Getting The Below Error SP11: if:not found SP11: line 4:syntax error at line 5:'then' unexpexted And The Program I Have Wrriten For This #!bin/ksh echo "Enter Two Numbers"... (3 Replies)
Discussion started by: anudeepkumar123
3 Replies

6. Shell Programming and Scripting

Syntax error calling TCL script from shell script

hello everyone i am beginner on shell scripting .and i am working on my project work on ad hoc network i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem " syntax error near unexpected token `('... (1 Reply)
Discussion started by: marcoss90
1 Replies

7. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

8. Windows & DOS: Issues & Discussions

Error opening script file - location error

Hello, I know nothing about UNIX, ftp, etc. I am building an excel VBA macro which calls a .bat file. I've taken a pre-existing batch file and am trying to modify it to fit my purposes. I would be very grateful for some assistance. Here is my .bat file: echo off set... (9 Replies)
Discussion started by: starcraftbud
9 Replies

9. Shell Programming and Scripting

Script with error output but continuation in script?

I have written a basic fetching script. The script logs into an FTP site, downloads a .zip file, then unzips and moves the files to the necessary folders, then deletes them, etc. The problem I have is if one of the files no longer exists on the FTP site or another part of the script fails, then... (3 Replies)
Discussion started by: daem0n
3 Replies

10. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies
Login or Register to Ask a Question