Results 1 to 5 of 5

Thread: Trying to store multi-line outputs in a variable or array

  1. #1
    Join Date
    Mar 2007
    Beans
    1,052

    Trying to store multi-line outputs in a variable or array

    I've found several links but, none of them worked.

    Here is the one that seems to be the best to me (but, it is still not good enough).:
    Code:
    #!/bin/bash
    
    i=1
    ls -la /tmp | while read line
    do
        array[ $i ]="$line"
        (( i++ ))
    done
    I'm trying to do this for two reasons.:
    (1) To learn more about Linux shell scripting.
    (2) To, ultimately, create a script which allows me to remove absolutely everything containing a certain string in it (or to move it to a directory to confirm that there is nothing being removed that I need prior to removing the bunch of files).

    What I am trying to figure out, using this thread, is how to store each individual line of the multi-line input of
    Code:
    ls -la /tmp
    into its respective array element.

    Running the syntax I gave above prints out a new line character (by which I mean it just jumps a line).

    Any input in figuring out how to get the syntax above working as I intend it to would be greatly appreciated!
    Last edited by s3a; June 24th, 2013 at 04:28 AM.
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

  2. #2
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Trying to store multi-line outputs in a variable or array

    I'm not a bash expert, but I bet if you're seeing double-spaced output it's because of the printing logic, not how you're reading it in. What you have looks fine to me.

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Trying to store multi-line outputs in a variable or array

    I'm not a bash expert either, but you could also consider using the readarray builtin in place of your for loop - something like

    Code:
    $ readarray -t array < <(ls -la /tmp)
    might work for you - e.g.

    Code:
    $ readarray -t array < <(ls -la test)
    $ 
    $ for f in "${array[@]}"; do echo "$f"; done
    total 12
    drwxr-xr-x  3 steeldriver steeldriver 4096 Jun 23 19:31 .
    drwxr-xr-x 40 steeldriver steeldriver 4096 Jun 22 19:30 ..
    drwxr-xr-x  2 steeldriver steeldriver 4096 Jun 14 21:27 dir
    -rw-r--r--  1 steeldriver steeldriver    0 Jun 23 19:31 file with space
    $
    $ echo ${array[3]}
    drwxr-xr-x  2 steeldriver steeldriver 4096 Jun 14 21:27 dir
    Last edited by steeldriver; June 24th, 2013 at 01:39 AM.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Trying to store multi-line outputs in a variable or array

    readarray would be the way to go...
    ... but using ls output for anything is considered harmful as it doesn't guarantee the true representation of the filename in case of nasty characters (newline, unprintable). Kosher way would utilize native globs or null delimited find command

    btw, bash has += operator that allows to add element to array without managing indices by hand
    Code:
    $ array=( "abc" "def" )
    $ array+=( "ghi" ) #array is now "abc" "def" "ghi"
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Mar 2007
    Beans
    1,052

    Re: Trying to store multi-line outputs in a variable or array

    trent.josephsen:
    Oh my god! I forgot to print it out! Lol!

    Looking at this link ( http://www.thegeekstuff.com/2010/06/...rray-tutorial/ ), I added
    Code:
    echo ${array[ $i ]}
    between the setting-each-array-element and the element variable incrementation steps and, it works! Thanks!

    steeldriver:
    Thanks for this as well!

    Vaphell:
    Thank you for yet another useful thing!
    Last edited by s3a; June 24th, 2013 at 04:29 AM.
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •