Linux下將數(shù)據(jù)文件的指定域讀取到shell腳本中
這個(gè)例子說明了怎樣在Linux下shell腳本中從數(shù)據(jù)文件讀取特定的域(field)并進(jìn)行操作。例如,假設(shè)文件employees.txt的格式是{employee-name}:{employee-id}:{department-name},以冒號進(jìn)行劃分,如下所示。
$ cat employees.txt
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Madison Randy:300:Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
下面的shell腳本說明了如何從這個(gè)employee.txt文件中讀取特定的域(field)。
$ vi read-employees.sh
#!/bin/bash
IFS=:
echo "Employee Names:"
echo "---------------"
while read name empid dept
do
echo "$name is part of $dept department"
done < ~/employees.txt
賦予腳本可執(zhí)行權(quán)限后執(zhí)行該腳本
$ chmod u+x read-employees.sh
$ ./read-employees.sh
Employee Names:
---------------
Emma Thomas is part of Marketing department
Alex Jason is part of Sales department
Madison Randy is part of Product Development department
Sanjay Gupta is part of Support department
Nisha Singh is part of Sales department
【編輯推薦】