Linux下将数据文件的指定域读取到shell脚本中
Linux下將數據文件的指定域讀取到shell腳本中
這個例子說明了怎樣在Linux下shell腳本中從數據文件讀取特定的域(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
賦予腳本可執行權限后執行該腳本
$ 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
轉載于:https://blog.51cto.com/fccwcom/1258252
總結
以上是生活随笔為你收集整理的Linux下将数据文件的指定域读取到shell脚本中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【联盛德W806上手笔记】二、GPIO
- 下一篇: html加载gif动画效果,html –