在bash中如何从文本文件中读取文件名
时间:2019-04-29 03:17:19 来源:igfitidea点击:
对于Linux和Unix系统管理员来说,在shell脚本逐行读取文件并处理数据是一个非常常见的任务。这需要使用bash while循环和read命令。
例如,假设我们要从名为input.txt的文本文件中读取文件名列表,并对每个文件名采取操作。如何从文件读取文件名,并对每个文件名进行操作呢?如何从文本文件中读取文件名并对这些文件采取某些操作?
在Bash中从文本文件中读取文件名
while IFS= read -r file; do echo "Do something on $file ..." done < "filenames.txt"
或者
input="data.txt" while IFS= read -r file; do printf '%s\n' "$file" done < "$input"
使用bash while循环从文本文件中读取文件名
#!/bin/bash
set -e
in="${1:-input.txt}"
[ ! -f "$in" ] && { echo "#!/bin/bash
set -e
in="${1:-input.txt}"
[ ! -f "$in" ] && { echo "##代码## - File $in not found."; exit 1; }
while IFS= read -r file
do
## 忽略以#号开头的文件名 ##
[[ $file = \#* ]] && continue
echo "Running rm $file ..."
done < "${in}"
- File $in not found."; exit 1; }
while IFS= read -r file
do
echo "Working on $file ..."
done < "${in}"

