如何使该脚本将结果输出到文件中?
#!/bin/bash
#Use awk to do the heavy lifting.
#For lines with UID>=1000 (field 3) grab the home directory (field 6)
usrInfo=$(awk -F: '{if ($3 >= 1000) print $6}' < /etc/passwd)
IFS=$'\n' #Use newline as delimiter for for-loop
for usrLine in $usrInfo
do
#Do processing on each line
echo $usrLine
done
答案1
./path/to/my/script.bash > output.txt
顺便说一句,除非您有一些主目录中包含空格(如果是这样的话,这不是个好主意!),否则不需要将字段分隔符设置为$'\n'
想想看,for 循环根本没有什么意义(除非您打算添加到这个脚本中?)。你可以运行:
awk -F: '{if ($3 >= 1000) print $6}' /etc/passwd > output.txt