Trouble using substr function with Bourne shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble using substr function with Bourne shell script
# 1  
Old 09-27-2005
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
# 2  
Old 09-27-2005
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?
# 3  
Old 09-29-2005
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
# 4  
Old 09-29-2005
FISCAL_CODE=`awk 'END { print substr(val,3,2) }' val=$FISCAL_YEAR < /dev/null`
# 5  
Old 09-29-2005
Trouble using substr function with Bourne shell script

Thanks so much!!! It works great, I appreciate it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problems with "exit" called from function in bourne script

Hi everyone. #!/sbin/sh EXITING() { umount /FOLDER rm -Rf /FOLDER echo "EXIT" exit 0 } EXITING echo "OK" (8 Replies)
Discussion started by: vacadepollo
8 Replies

2. Shell Programming and Scripting

help with bourne shell script

Attempting to write a script to eventually notify me via email for when there is packetloss across the backbone. I am looking for values greater than 0% in the mtr field. #!/bin/sh target=www.google.com date +"%D"_"%T" >> /home/rich/mtr.log echo "----------------------------------------" >>... (1 Reply)
Discussion started by: closedown
1 Replies

3. Shell Programming and Scripting

substr from a string in Shell script

Shell Scripting Gurus, I am having a hard time :confused: trying to figure out what I am doing wrong in my script. Below is the script snippet. It gives an error when it tries to execute the expression. a=`expr substr $stringZ 5 10` #!/bin/bash echo "Hello" stringZ="abcABC123ABCabc"... (3 Replies)
Discussion started by: yajaykumar
3 Replies

4. Shell Programming and Scripting

How to use substr to return data into a shell script variable?

I'm writing a shell script in which I need to be able to pull a portion of the file name out. I'm testing with the following code: x="O1164885.DAT" y=`ls -ltr *${x}|awk '{print substr($0,3)}'` echo ${x}|awk '{print substr($0,3)}' echo "y="$y I can echo it to the screen just fine but I... (3 Replies)
Discussion started by: ttunell
3 Replies

5. Shell Programming and Scripting

Substr in shell script

hi, i am using the following script to get the last digit of YEAR OY=`expr substr $YEAR 2 1` it is showing syntax Is this wrong or i need to change anything I am running this in sun solaris unix (7 Replies)
Discussion started by: gjithin
7 Replies

6. Shell Programming and Scripting

substr() thru awk Korn Shell Script

Hi, I am new stuff to learn substr() function through awk for writing the Korn shell script. Is there a way to copy from XXXX1234.ABCDEF to XXX1234 file names without changing both data files? I appreciate your time to response this email. Thanks, Steve (4 Replies)
Discussion started by: sbryant
4 Replies

7. Shell Programming and Scripting

cd from a Bourne Shell Script - Please Help

Dear Bourne Shell Expert, I am trying to change the current working directory from within a Bourne Shell script. Simply enough i thought ! As I am sure you are well aware, Inside the script i echo `pwd` and it seems ok, but the shell spawns another shell to execute this and as such, when my... (10 Replies)
Discussion started by: fawqati
10 Replies

8. UNIX for Dummies Questions & Answers

Bourne Shell Script

Hello, I'm throwing this out there as a novice to the Unix world...I've been working on a project that requires me to ouput (using the echo command) a list of names in a single column format, but the problem is the input is in row format followed by a blank space...If anyone could give me a... (2 Replies)
Discussion started by: dmhonor914
2 Replies

9. Shell Programming and Scripting

bourne shell script

Hi all, Can somebody answer the following query Thanks, Srinivas A shell program that takes one or any number of file directory names as input; sorts the directories given as parameters jointly in the ascending or decending order of choice For EX : dips abc etc desc will sort the files... (2 Replies)
Discussion started by: psrinivas
2 Replies

10. UNIX for Advanced & Expert Users

Bourne shell script need help please ?

i have this assignment.. and i mad this script but there is something wrong with it.. if anyone can tell me.. watz going on... i would appreciate it.. tHnX in advance.. count=1 val=$2 op=$1 ans=0 if then if then while do ... (7 Replies)
Discussion started by: dezithug
7 Replies
Login or Register to Ask a Question