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 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 programming array trouble!!!

  • 04-08-2011 10:08pm
    #1
    Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭


    Hi,

    I am working on a script which needs to SSH into a remote location and return a list of .tgz file names from a directory.

    I will then need to choose one file from the list of .tgz files and will copy that locally to uncompress it and do different tasks with it.

    While I have not completed the SSH bit just yet I'm just trying to choose the .tgz file. I'm trying to do this locally first.

    I thought the best way would to be return the results of a command like "ls *.tgz" to an array and then print it together with its array key numbers on the screen. The user would then insert one of the key numbers and the file name with corresponds to that key in the array would be copied etc.

    I am having trouble adding the results of the command to an array. I have done lots of googling but so far had no luck, Any help?

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 255 ✭✭boblong


    Does something like this not work?
    arr=( `ls *.tgz` )
    
    echo item number 1:
    echo ${arr[0]}
    
    echo item number 2:
    echo ${arr[1]}
    

    Notice the backticks around the ls. Those are not inverted commas :)


  • Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭Peter B


    boblong wrote: »
    Does something like this not work?
    arr=( `ls *.tgz` )
    
    echo item number 1:
    echo ${arr[0]}
    
    echo item number 2:
    echo ${arr[1]}
    

    Notice the backticks around the ls. Those are not inverted commas :)

    No way! I was trying inverted commas for ages and couldn't understand why it didn't work.

    Thanks a million!!!!


  • Registered Users, Registered Users 2 Posts: 2,353 ✭✭✭Galway K9


    Good advice.:)


  • Registered Users, Registered Users 2 Posts: 35 mobitron


    Peter B wrote: »
    No way! I was trying inverted commas for ages and couldn't understand why it didn't work.

    Thanks a million!!!!

    To avoid this kind of confusion I always use $(command) for command substitution in bash and ksh scripts.

    In this case it would be
    arr=( $(ls *.tgz) )
    


Advertisement