daily pastebin goal
82%
SHARE
TWEET

Untitled

a guest Dec 20th, 2012 36 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I would like to be able to put the file names in a directory into an
  2. array in bash. I have a script that works as long as there are no
  3. spaces in the file name. When there is a space in a name, each part of
  4. the file name ends up in a separate element.
  5.  
  6. The script:
  7.  
  8.    #!/bin/bash
  9.    # List files, ignoring directories.
  10.    ls -1pQ | grep -v '/'
  11.    echo
  12.  
  13.    # Put the file names in an array.
  14.    declare -a fn
  15.    fn=( `ls -1pQ | grep -v '/'` )
  16.  
  17.    # Print the number of elements in the array.
  18.    echo 'count='${#fn[*]}
  19.    echo
  20.  
  21.    # Print all array elements, one per line.
  22.    n=0
  23.    for name in ${fn[@]}
  24.    do
  25.         let "n=$n+1"
  26.         echo $n':' $name
  27.    done
  28.    exit
  29.  
  30.  
  31. The terminal output:
  32.  
  33.    "new file.txt"
  34.    "test"
  35.  
  36.    count=3
  37.  
  38.    1: "new
  39.    2: file.txt"
  40.    3: "test"
  41.  
  42. I would like to be able to do it without creating a temporary disk
  43. file. It is possible to make it work by using sed to replace each
  44. space with some other character, then reversing the substitution
  45. later. However, that seems kind of heavy-handed.
  46.  
  47. Any other thoughts on how to make this work?
RAW Paste Data
Top