while 循环在 shell 脚本中不起作用,而 运行 来自 crontab 的脚本
while loop not working in shell script while running the script from crontab
我正在尝试使用 while 循环逐行读取文件,例如:
while read Line; do
echo Inside outer loop
while read Line2; do
.....
echo Inside inner loop
.....
done < /absolute path of file/filename2
done < /absolute path of file/filename
该脚本在 运行 独立运行时运行良好。但是当它从 crontab 获取 运行 时,它不会进入循环。
请指出可能的原因是什么。
第二个 while 循环正在读取 所有 输入("filename" 的第一行除外)。您需要重定向到单独的文件描述符:
while IFS= read -r -u3 Line; do
echo Inside outer loop
while IFS= read -r -u4 Line2; do
.....
echo Inside inner loop
.....
done 4< "/absolute path of file/filename2"
done 3< "/absolute path of file/filename"
- 使用
IFS=
和 read -r
确保从文件中逐字读取该行。
read -u3
和 3< file
使用特定的 fd
- 如果你的真实路径有space,你需要引用文件名
我正在尝试使用 while 循环逐行读取文件,例如:
while read Line; do
echo Inside outer loop
while read Line2; do
.....
echo Inside inner loop
.....
done < /absolute path of file/filename2
done < /absolute path of file/filename
该脚本在 运行 独立运行时运行良好。但是当它从 crontab 获取 运行 时,它不会进入循环。
请指出可能的原因是什么。
第二个 while 循环正在读取 所有 输入("filename" 的第一行除外)。您需要重定向到单独的文件描述符:
while IFS= read -r -u3 Line; do
echo Inside outer loop
while IFS= read -r -u4 Line2; do
.....
echo Inside inner loop
.....
done 4< "/absolute path of file/filename2"
done 3< "/absolute path of file/filename"
- 使用
IFS=
和read -r
确保从文件中逐字读取该行。 read -u3
和3< file
使用特定的 fd- 如果你的真实路径有space,你需要引用文件名