BASH - затем поиск функции имени

Кто-нибудь посоветуйте, как называется в bash функция, которая читает файл и создает из него массив? Спасибо


person user2093552    schedule 02.03.2013    source источник
comment
Что вы пытаетесь достичь?   -  person Explosion Pills    schedule 02.03.2013
comment
Возможный дубликат stackoverflow.com/questions/9736202 /   -  person jitendra    schedule 02.03.2013
comment
Чтение файла и создание из файлового массива... это прямая функция, но я не могу ее найти   -  person user2093552    schedule 02.03.2013
comment
@jitendra: OP явно запрашивает встроенный вопрос, который отличается от связанного вопроса (хотя встроенный мог быть ответом на это, он не был предоставлен).   -  person rici    schedule 02.03.2013


Ответы (2)


В настоящее время я использую Windows, поэтому здесь нет bash, но вот как вы это делаете:

value=0;

while read line
do
value=`expr $value + 1`;
arr[$value]=$line;

done < "myfile"
person Chen Harel    schedule 02.03.2013
comment
что заставляет вас думать, что существует функция, которая делает это в bash? - person Chen Harel; 02.03.2013
comment
альтернативно: declare -a arr=($(cat myfile)) - person Swiss; 02.03.2013
comment
$( < myfile) более эффективен. - person chepner; 02.03.2013

Я думаю, вы ищете встроенный mapfile.

От help mapfile:

mapfile: mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
    Read lines from the standard input into an indexed array variable.

    Read lines from the standard input into the indexed array variable ARRAY, or
    from file descriptor FD if the -u option is supplied.  The variable MAPFILE
    is the default ARRAY.

    Options:
      -n count  Copy at most COUNT lines.  If COUNT is 0, all lines are copied.
      -O origin Begin assigning to ARRAY at index ORIGIN.  The default index is 0.
      -s count  Discard the first COUNT lines read.
      -t                Remove a trailing newline from each line read.
      -u fd             Read lines from file descriptor FD instead of the standard input.
      -C callback       Evaluate CALLBACK each time QUANTUM lines are read.
      -c quantum        Specify the number of lines read between each call to CALLBACK.

    Arguments:
      ARRAY             Array variable name to use for file data.

    If -C is supplied without -c, the default quantum is 5000.  When
    CALLBACK is evaluated, it is supplied the index of the next array
    element to be assigned and the line to be assigned to that element
    as additional arguments.

    If not supplied with an explicit origin, mapfile will clear ARRAY before
    assigning to it.

    Exit Status:
    Returns success unless an invalid option is given or ARRAY is readonly or
    not an indexed array.
person rici    schedule 02.03.2013