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

Bash programming array trouble!!!

Options
  • 04-08-2011 11:08pm
    #1
    Registered Users 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 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 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 Posts: 2,353 ✭✭✭Galway K9


    Good advice.:)


  • Registered Users 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