@ysongzybl
2015-04-30T18:33:16.000000Z
字数 864
阅读 2597
bash
array=()
# Read the file in parameter and fill the array named "array"
getArray() {
i=0
while read line # Read a line
do
array[i]=$line # Put it into the array
i=$(($i + 1))
done < $1
}
getArray "file.txt"
How to use your array :
# Print the file (print each element of the array)
getArray "file.txt"
for e in "${array[@]}"
do
echo "$e"
done
#!/bin/bash
IFS=$'\n' read -d '' -r -a lines < /etc/passwd
# Now just index in to the array lines to retrieve each line, e.g.
printf "line 1: %s\n" "${lines[0]}"
printf "line 5: %s\n" "${lines[4]}"
# all lines
echo "${lines[@]}"
#!/usr/bin/env bash
IN="bla@some.com;john@home.com"
arr=$(echo $IN | tr ";" "\n")
for x in $arr
do
echo "> [$x]"
done
1 http://stackoverflow.com/questions/11393817/bash-read-lines-in-file-into-an-array
2 http://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash