1

hi guys i know this question has been asked several time out there.But i have tried a lot and i am not able to get what is the issue .Below is my code -:

#!/bin/bash



  #GLOBAL VARIABLE
  name=()
  browserTime=0
  browserScriptLocation=/global/z1/siebel/sweapp/public/enu
  value=0
  browserintegerDate=0

  browserscript_CreationTime(){
  # SET PATH
  cd $browserScriptLocation
  #LOOP INSIDE THE FILES PRESENT ON PARTICULAR PATH
  for entry in $(ls -t | grep  "srf*")
  do
  #APPEND SEARCHED FILE IN ARRAY AT INDEX 0
  name[value]="$entry"
  #INCREASE INDEX COUNT
  value+=1
  done
  name=${name[0]}
  echo "$name"
  #GET BROWSER SCRIPT TIME
  browserTime=$(ls -E "$name" | cut -c 52-60)
  echo "$browserTime"
  browserDate=$(ls -E "$name" | cut -c 41-51)
  echo "$browserDate"
  year=$(echo "$browserDate" | cut -c 1-5)
  month=$(echo "$browserDate" | cut -c 7-8)
  date=$(echo "$browserDate" | cut -c 10-11)
  browserintegerDate="$year$month$date"
  browserintegerDate+=$(echo $timeConversion)
   echo "$browserintegerDate"
}

#invoke your function
browserscript_CreationTim

I am getting this error-: name[value]: bad array subscript.The same code i have tested on other UNIX BOX. Both having same OSversion (SunOS 5.10) and it works fine over there.But it is not working on another one.I am not getting why is this happening?? Can anyone help??

Randomguy
  • 192
  • 1
  • 11
  • And what is `value`? Don't you mean `$value`? – Some programmer dude Feb 08 '17 at 11:54
  • i tried that as well but still same error..:( – Randomguy Feb 08 '17 at 11:54
  • Two other possible problems: `value+=1` will not increase `value` by one. Considering that `value` is initialized with `0` the result will be `01` first time around in the loop. Second, `name=${name[0]}`? Are you sure you want to use the same name for both variables? – Some programmer dude Feb 08 '17 at 12:03
  • okk that error is not coming now but why my array is printing last index value instead of giving value at index 0????? The first time loop execute i store the file name in array which is at the top at index 0.But it is printing out last indx file can yoyu tell why?? – Randomguy Feb 08 '17 at 12:28
  • how can i increment index count if not valid with value+=1?? – Randomguy Feb 08 '17 at 12:32
  • How about `let value++`? Read more about [arithmetic evaluation](http://man7.org/linux/man-pages/man1/bash.1.html#ARITHMETIC_EVALUATION) in [the Bash manual page](http://man7.org/linux/man-pages/man1/bash.1.html). – Some programmer dude Feb 08 '17 at 12:52

0 Answers0