Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Bash: Assign the output of a command to an array element?

  • 24-10-2013 10:53pm
    #1
    Closed Accounts Posts: 4,763 ✭✭✭


    I am writing a script where I interactively receive input from the user. I'd ideally like to read the answers into an array. Code in question:
        for q in $(seq 0 ${#img_question[@]}); do
            until [[ $[img_answer[$q]} == '^[0-9]+([.][0-9]+)?$' ]]; do
                echo -ne ${img_question[$q]}
                read ${img_answer[$q]}
            done
        done
    

    I'm taking in straight numerical values. The thing is, values recorded by the array element are null, empty. I've worked through a few permutations of this, but nothing seems to work. I've tried to indirectly assign to the array element through an intermediary variable, but it is treated as a command. Example:
    [me][~] # read ans
    Testing
    [me][~] # echo $ans
    Testing
    [me][~] # {test_array[0]}=$ans
    -bash: {test_array[0]}=Testing: command not found
    

    What's the best way (if any) to accomplish this?


Comments

  • Moderators, Arts Moderators Posts: 35,508 Mod ✭✭✭✭pickarooney


    In the second part, should you use a 'let' for the assignment of the $ans variable to the first slot in the array?


  • Closed Accounts Posts: 4,763 ✭✭✭Fenster


    I think I might have it now. Your suggestion was wrong, but it starkly illuminated my mistake. :)


  • Moderators, Arts Moderators Posts: 35,508 Mod ✭✭✭✭pickarooney


    Glad to be the blacklight :D


  • Closed Accounts Posts: 4,763 ✭✭✭Fenster


    I was using completely the wrong syntax:
            until [[ $img_answer[$q] == $is_valid ]]; do
                echo -ne ${img_question[$q]}
                read img_answer[$q]
                # Validation
            done
    

    I was simultaneously trying to call the variable, and assign to it, which Bash couldn't handle.


Advertisement