Linux下將數(shù)據文件的指定域讀取到shell腳本中
這個例子說明了怎樣在Linux下shell腳本中從數(shù)據文件讀取特定的域(field)并進行操作。例如,假設文件employees.txt的格式是{employee-name}:{employee-id}:{department-name},以冒號進行劃分,如下所示。
$ 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腳本說明了如何從這個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í)行權限后執(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
【編輯推薦】















 
 
 





 
 
 
 