1

I'm trying to add the below string manipulation commands in my existing shell script.

#!/bin/ksh
y=${#text}
echo "Length of text: $y"
echo "Last _ is in column $((${#text} - ${#end}))"
echo "Our position is till column $((${#text} - ${#end}-$x))"

len=`expr $((${#text} - ${#end}-$x))`
echo $len

val=$( echo $text $len|awk '{print substr($0,0,$2)}')

echo $val
count=`ls $val*|wc -l`
echo $count

The above commands works fine when executed in terminal. but when i add to my existing shell script which is running as a concurrent program, the program errors out throwing the error "Bad substitution". And i'm able to identify that the it errors out at the code added recently for string manipulations.

I can see the code is interpreted as korn shell and I've used korn shell commands only which works in terminal but when added to existing concurrent program's unix script it errors out.

Below is the code in my existing script along with newly added commands

#Parameters : Takes the following input parameters.
# 1) Input base directory   -- parameter 5
# 2) File Pattern           -- Parameter 6
# 3) Appl short name        -- parameter 7
# 4) appl. resp.            -- parameter 8
# 5) Debug                  -- parameter 9
#=======================================================================
#!/bin/ksh
AppsUser="$3"
BaseDir="$5"
FilePattern="$6"
AppShortName="$7"
AppResp="$8"
DebugProgram="$9"

echo "Apps User=$AppsUser"
echo "Base Directory=$BaseDir"
echo "File Pattern=$FilePattern"
echo "App Short Name=$AppShortName"
echo "App Responsibility=$AppResp"
echo "Debug Program=$DebugProgram"

IncomingDir="$BaseDir/incoming"
ProcessedDir="$BaseDir/processed"
ProcessingDir="$BaseDir/processing"
TempFile="$BaseDir/incoming/absn_psoft_glfile_search_results.txt"
ConcProgram="ABSN_PEOPLESOFT_OGL_INTF_MAIN"
echo $ConcProgram
echo $TempFile
IFS="
"
export IFS

cd $IncomingDir
ls *_GL_*.dat 1> /dev/null 2> /dev/null
if [ $? = 0 ]
then
    #for i in `cat $TempFile`
    for file in `ls *_GL_*.dat`
     do
      echo "File being processed $file"
       FILENAME=$file
       #echo $FILENAME
       len=${#FILENAME} -->Error thrown for this command
       echo "The length is $len"
       if [ -s $FILENAME ]
        then
      echo "Moving file $FILENAME to $ProcessingDir"
      mv $IncomingDir/$FILENAME $ProcessingDir/$FILENAME
      echo "Submitting concurrent Request using CONCSUB for File=$FILENAME"
      CONCSUB $FCP_LOGIN $AppShortName $AppResp $AppsUser WAIT=N CONCURRENT ABSN $ConcProgram  $FILENAME 0 $DebugProgram $ProcessingDir
      echo "Submitted Request for File=$FILENAME"
      sleep 60
      else
          echo "File size is 0 bytes, moving file to processed directory"
          mv $IncomingDir/$FILENAME $ProcessedDir/$FILENAME.rmv
      fi
    done
else
  echo "No GL PSoft files to process."
fi
#echo "After if"
echo "Completed Job, Exiting program with code 0"
exit 0

The script is attached as an executable to an oracle concurrent program and executed using the concurrent program

  • You are adding it literally into the existing script? Or calling it as a command? – RealSkeptic Apr 13 '16 at 9:48
  • i'm adding it to the existing script. In the existing script i'm trying to find the length of a filename. **************************************************************************** for file in ls *_GL_*.dat do echo "File being processed $file" FILENAME=$file len=${#FILENAME} ---> error thrown here echo "The length is $len" – Dhivya Apr 13 '16 at 10:12
  • 1
    Please don't put code in comments. Instead, edit the question and add the extra information in the question, and use the comments just for short sentences, or to inform a person that you made a change. – RealSkeptic Apr 13 '16 at 11:10
  • 1
    When you add code, select it and press the {} button to format it as code. – RealSkeptic Apr 13 '16 at 11:47
  • 1
    @RealSkeptic: done as mentioned – Dhivya Apr 13 '16 at 11:59
1

works fine when executed in terminal. but when i add to my existing shell script which is running as a concurrent program, the program errors out throwing the error "Bad substitution".

Syntax errors in a script? That usually indicates that you aren't using the same shell for the script and interactively.

#Parameters : Takes the following input parameters.
…

This script has no shebang, so it's executed with sh, not with ksh. On your system, sh is evidently an old Bourne shell that doesn't understand arithmetic expansion. You apparently meant to execute the script with ksh, but the #!/bin/ksh line must be the first line in the file. Move it before the usage comments.

Also, always put double quotes around variable substitutions: cd "$IncomingDir" etc. And don't parse the output of ls: for file in *_GL_*.dat.

  • Thanks for pointing out....Now i understand why half of the script got executed with output and the newly added ones were throwing error. – Dhivya Apr 15 '16 at 7:49
  • And nice to know the symbol #! has got a name too...Shebang... – Dhivya Apr 15 '16 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.