The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Substr in shell script gjithin Shell Programming and Scripting 7 05-10-2008 01:40 PM
substr() thru awk Korn Shell Script sbryant Shell Programming and Scripting 4 01-24-2008 08:16 AM
cd from a Bourne Shell Script - Please Help fawqati Shell Programming and Scripting 10 05-25-2006 12:26 AM
Bourne Shell Script dmhonor914 UNIX for Dummies Questions & Answers 2 12-10-2003 09:25 AM
bourne shell script psrinivas Shell Programming and Scripting 2 12-06-2001 12:38 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-27-2005
Registered User
 

Join Date: Jul 2005
Posts: 5
Trouble using substr function with Bourne shell script

Hi,

I'm a newbie to UNIX scripting and I'm having some trouble compiling my script. I'm using the Bourne Shell and cannot seem to use the substr function correctly. I'm trying to extract the last two digits of a year that's stored in a variable based off of a condition. I've searched the forums and google trying suggestions/formatting, but I keep getting a syntax error that I can't seem to fix. Does anyone see any syntax issues with my substr code (FISCAL_CODE) below?

#!/usr/bin/sh

CURRENT_DATE=`date +%m%d%Y`
CURRENT_YEAR=`date +%Y`
START_YEAR=`expr $CURRENT_YEAR - 1`

#Concatenate the static fiscal year's start/end
#month and day with the formatted years.
START_FY='0701'$START_YEAR
END_FY='0630'$CURRENT_YEAR

#Define the actual fiscal year depending on where
#the current date falls within the fiscal year range.
if [ $CURRENT_DATE -gt $START_FY ]
then
if [ $CURRENT_DATE -lt $END_FY ]
then
FISCAL_YEAR=$CURRENT_YEAR
else
FISCAL_YEAR=`expr $CURRENT_YEAR + 1`
fi
fi

#Define the fiscal code by substringing the last two
#digits of the acutal fiscal year.
FISCAL_CODE=substr($FISCAL_YEAR 3 2)
#FISCAL_CODE=`expr substr $FISCAL_YEAR 3 2`


echo Today is $CURRENT_DATE
echo Current Year is $CURRENT_YEAR
echo Start FY $START_FY
echo End FY $END_FY
echo Fiscal Year is $FISCAL_YEAR
echo Fiscal Code is $FISCAL_CODE

-------------------------------------------------------------------
This is the output I'm viewing:

expr: syntax error
<or> syntax error at line 26: `FISCAL_CODE=substr' unexpected
Today is 09272005
Current Year is 2005
Start FY 07012004
End FY 06302005
Fiscal Year is 2006
Fiscal Code is

If there's anything else I can answer or provide, please let me know. Any assistance would be greatly appreciated.

Thanks,
Eric
Reply With Quote
Forum Sponsor
  #2  
Old 09-27-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
You commented out the right syntax and added the wrong syntax. You seemed to have picked up the awk syntax but you are not using awk.

Something like:

awk 'END { print substr(val,3,2) }' val=$YEAR < /dev/null

might work.

You use a modern shell anyway like bash or ksh. Why use Bourne?
Reply With Quote
  #3  
Old 09-29-2005
Registered User
 

Join Date: Jul 2005
Posts: 5
Trouble using substr function with Bourne shell script

Perderabo,

I used that statement and it works good, but it seems to only print the value to the screen. I'd really like to store that value in a variable for another part of code I will use. I tried to use the following statement and got an error when I tried to display the value using echo:

FISCAL_CODE=awk 'END { print substr(val,3,2) }' val=$FISCAL_YEAR < /dev/null

error:
: END { print substr(val,3,2) }: not found

Is there a way to store that value in a variable? Not sure if my syntax is incorrect. Does awk only work to output to the screen?

As for using a bash or ksh script instead, this is part of a bourne script that I am modifying.

Please let me know what you think when you can.

Thanks,
Eric
Reply With Quote
  #4  
Old 09-29-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
FISCAL_CODE=`awk 'END { print substr(val,3,2) }' val=$FISCAL_YEAR < /dev/null`
Reply With Quote
  #5  
Old 09-29-2005
Registered User
 

Join Date: Jul 2005
Posts: 5
Trouble using substr function with Bourne shell script

Thanks so much!!! It works great, I appreciate it.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0