@ysongzybl
2015-04-30T18:33:16.000000Z
字数 864
阅读 2683
bash
array=()# Read the file in parameter and fill the array named "array"getArray() {i=0while read line # Read a linedoarray[i]=$line # Put it into the arrayi=$(($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[@]}"doecho "$e"done
#!/bin/bashIFS=$'\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 linesecho "${lines[@]}"
#!/usr/bin/env bashIN="bla@some.com;john@home.com"arr=$(echo $IN | tr ";" "\n")for x in $arrdoecho "> [$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