-1

I do got answers on this topic but it is not working still so please help.

Fallowing piece of code in which i m not able to access string array with index as variable i. I m trying to insert a value at respective index but it is not working.

i=0
declare -a str_arr

while read line

do
post=`echo $line| cut -d '|' -f2`
dept=`echo $line| cut -d '|' -f4`

str_arr[$i]="$dept|$post"

i=$(( i+1 ))
done< emp_info.dat

echo ${str_arr[@]}

I m inserting two variables with pipe operator into string array.

output is giving like this

> example.sh: 2: example.sh: declare: not found example.sh: 10:
> example.sh: str_arr[0]=aaaa|developer: not found example.sh: 10:
> example.sh: str_arr[1]=bbbb|tester: not found example.sh: 10:
> example.sh: str_arr[2]=Empower|Senior dev: not found example.sh: 10:
R.J
  • 1
  • 1
  • 7
  • What exactly is not working? I made a quick test with some dummy data and it worked as far as I can see but I don't know your expected output. Could you add your expected output to the question and what output you currently get? – DAXaholic Jul 24 '16 at 04:49
  • `str_arr[$i]="$dept|$post"` at this line it is not accessing error something like ` str_arr[0]=Empower|Analyst: not found`-- Empower|Analyst these are values of $dept and $post. – R.J Jul 24 '16 at 04:50
  • I think your index should start with 1 instead of 0 – Lee HoYo Jul 24 '16 at 04:59
  • Error is at every index. I tried with index start with 1 also it is not working. – R.J Jul 24 '16 at 05:02
  • Could you please execute your script like so 'bash -x myscript.sh' so that we can see what is actually happening as I am currently not able to reproduce your issue. Please note that the data of your file will be seen in this case so you may want to create and use a simple dummy file if your data is confidential – DAXaholic Jul 24 '16 at 05:30
  • `+ i=0 + declare -a str_arr example.sh: 2: example.sh: declare: not found + read line + cut -d | -f2 + echo abc|developer|04-11-1993|aaaa + post=developer + cut -d | -f4 + echo abc|developer|04-11-1993|aaaa + dept=aaaa + str_arr[0]=aaaa|developer **example.sh: 10: example.sh: str_arr[0]=aaaa|developer: not found** + i=1 + read line + cut -d | -f2 + echo xyz|tester|09-12-1996|bbbb + post=tester + cut -d | -f4 + echo xyz|tester|09-12-1996|bbbb + dept=bbbb + str_arr[1]=bbbb|tester **example.sh: 10: example.sh: str_arr[1]=bbbb|tester: not found **` this is output in debug mode – R.J Jul 24 '16 at 05:50
  • 3
    I'm pretty sure you're running this with a shell other than `bash`. – Gordon Davisson Jul 24 '16 at 06:05
  • I am using bash shell. I checked too by `echo $0` and I think its not an issue of shell. – R.J Jul 24 '16 at 06:17
  • @R.J Click on [edit](http://stackoverflow.com/posts/38548791/edit) and put those results in your question where they can be properly formatted. If you leave them in comments, you make us guess where the line endings are. Since you are getting free advice here, be kind enough to give us good formatting. – John1024 Jul 24 '16 at 06:56
  • 1
    Also, @GordonDavisson is correct about the shell. You need to provide details on (a) which OS or distribution you are using and (b) how you are executing the script. – John1024 Jul 24 '16 at 07:02
  • Sorry for mistakes @John1024 and I am using ubuntu 14.04 through VMware. – R.J Jul 24 '16 at 07:25
  • @R.J On Ubuntu, the default shell, meaning `/bin/sh`, is `dash` and `dash` would give you the errors that you see. Show how you are invoking the script. – John1024 Jul 24 '16 at 07:26
  • 1
    using this `sh -x example.sh` command – R.J Jul 24 '16 at 07:42
  • That's the problem - you are using sh, not bash. Use bash. – cdarke Jul 24 '16 at 08:12
  • @R.J : The default shell was changed in/from Ubuntu 6.10 to dash which is the short for `Debian Almquist Shell` – sjsam Jul 24 '16 at 08:25

2 Answers2

3

As @gordon-davisson mentioned in his comment, the default shell in Ubuntu 14.04 is dash and not bash as I can confirm from the results below :

$ ls -l /bin | grep sh
.
.
lrwxrwxrwx 1 root root       4 Feb 19  2014 sh -> dash
.
.

You need to put the shebang #!/bin/bash at the beginning of the script and execute it as

/path/to/script

or execute it like

bash /path/to/script #In this case the shebang #!/bin/sh shouldn't be present at the beginning.
Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102
1

As an aside, your code has multiple errors and stylistic issues.

i=0
declare -a str_arr

# Notice IFS to use shell to parse line
# Notice -r to disable legacy behavior
while IFS='|'  read -r _ post _ dept _
do
    # Notice indent of loop body
    str_arr[$i]="$dept|$post"
    i=$(( i+1 ))
done< emp_info.dat

# Notice quoting
echo "${str_arr[@]}"

In fact, many times you should simply process the values as you read them, inside the loop, instead of accumulate them into an array, in which case maybe you don't need Bash-only features at all; but perhaps this is part of a bigger program where you really do require random access to all the values by numeric index.

awk -F '|' '{ print $4 OFS $2 }' emp_info.dat
tripleee
  • 175,061
  • 34
  • 275
  • 318