You are not logged in.

#1 2016-06-08 05:54:08

Wakeman
Member
Registered: 2016-06-08
Posts: 3

another "command not found" on array assignment

Hi there,
I found lots of helpful information in the forum so I did try a lot but nothing works for this case:

my test program:

#!/bin/bash
systemname=$1

declare -a FStable${systemname}
        FStable$systemname[0]="$(date)"


./test.sh Testsys
./test.sh: line 5: FStableTestsys[0]=Wed Jun  8 07:46:32 CEST 2016: command not found

left and right side of the equal sign look good sofar, but the result of the date command does not get assigned to the array. I keep getting "command not found"


any hint appreciated

regards Wakeman

Offline

#2 2016-06-08 06:47:58

Ropid
Member
Registered: 2015-03-09
Posts: 1,069

Re: another "command not found" on array assignment

What you try to do simply does not work. You can't use something like "$systemname" on the left side of a "=".

The newer bash versions have a thing called "nameref" but it can't target an array, I think. There's also "eval", but this isn't recommended as using it safe and without mistakes is too hard. I think you need to change your idea.

If you need complicated data structures, you are usually better off writing in for example Python or Perl instead of bash.

Offline

#3 2016-06-08 07:05:09

Wakeman
Member
Registered: 2016-06-08
Posts: 3

Re: another "command not found" on array assignment

Hi Ropid,
I wouldn't agree with your statement "you can't use .... "

These assignemnts work well enough for me, also does eval for what I need.
It's for some reason just the date command itself that seems to behave different:

#!/bin/bash
systemname=Testsys
# Get and store FS information
        declare -a FStable${systemname}
        eval FStable$systemname[0]="$(echo $(date) | cut -d " " -f4)"
        temp=FStable$systemname[0]
        echo ${!temp}
# Above works (see output further down), but is not enough of what I need
# Below results in the "command not found" issue
        eval FStable$systemname[1]="$(echo $(date))"
        temp=FStable$systemname[1]
        echo ${!temp}

$ ./test.sh
09:03:54
./test.sh: line 8: Jun: command not found

regards Wakeman

Offline

#4 2016-06-08 07:26:10

Ropid
Member
Registered: 2015-03-09
Posts: 1,069

Re: another "command not found" on array assignment

It works because you are using "eval". The error is because bash is evaluating everything twice, first with "eval", then again after that. To see what's happening, try running the script with "bash -x ...". Bash will print the lines it sees and executes. You can also experiment on the command line after you do "set -x".

Here's an experiment on the command line:

$ set -x

$ systemname=hello
+ systemname=hello

$ eval table$systemname[0]="$(echo $(date) | cut -d " " -f4)"
++ cut -d ' ' -f4
+++ date
++ echo Wed Jun 8 09:20:43 CEST 2016
+ eval 'tablehello[0]=09:20:43'

You would need to escape the parts you don't want "eval" to evaluate in the first pass. This here seems to work:

$ eval table$systemname[0]='"$(echo $(date) | cut -d " " -f4)"'

In any case, this is all a pretty terrible idea, I feel. There has to be a way to simplify and avoid "eval".

Offline

#5 2016-06-08 07:51:59

Wakeman
Member
Registered: 2016-06-08
Posts: 3

Re: another "command not found" on array assignment

Hi Ropid,
thanks for the good advice to use set -x.
In conjunction with eval the using of escape characters on the right side of equal sign makes it work.

and yes, it probably would have been better to start this task with Python or similar rather than try native bash.  Well ....

regards Wakeman

Offline

#6 2016-06-08 09:56:44

Ropid
Member
Registered: 2015-03-09
Posts: 1,069

Re: another "command not found" on array assignment

I tested "namerefs" and they do work with arrays. Here's an example of experimenting on the command line with it:

$ declare -a hello

$ declare -n x=hello

$ x[0]=one

$ x[1]=two

$ echo ${hello[@]}
one two

This then doesn't need "eval".

Offline

Board footer

Powered by FluxBB