Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix bash shell中的While do循环_Bash_Unix_While Loop_Do While - Fatal编程技术网

Unix bash shell中的While do循环

Unix bash shell中的While do循环,bash,unix,while-loop,do-while,Bash,Unix,While Loop,Do While,我正在尝试创建一个脚本,该脚本循环文件中的日期列表并执行特定操作。我的代码如下所示。我正在使用bashshell while read line; do #do something with $line done < file.txt 读行时;做 #用$line做点什么 完成回音$线 >完成>file.txt)中添加新行。什么是$file.txt?循环正确。使用文件$file.txt检查它是否不包含任何windows行结尾您发布的代码没有明显错误。请编辑您的问题,以显示您在循环中执行的操

我正在尝试创建一个脚本,该脚本循环文件中的日期列表并执行特定操作。我的代码如下所示。我正在使用bashshell

while read line; do
#do something with $line
done < file.txt
读行时
;做
#用$line做点什么
完成
到目前为止,我试图用两个日期测试我的脚本,但是脚本在第一个日期执行,而不是第二个日期。另外,我的脚本说它成功完成了。请帮我拿一些


现在,我正在尝试运行我的脚本4个日期。我还读到有人有一个类似的问题,其中有N行,但脚本只适用于N-1。但是我找不到这个问题的完美答案。

我试过了,这个代码结构很好。 也许问题出在循环内部

[ohad@OhadLaptop]$ cat a.txt 
a
b
c
[ohad@OhadLaptop]$ while read line; do echo $line; done < a.txt 
a
b
c
[ohad@OhadLaptop]$cat a.txt
A.
B
C
[ohad@OhadLaptop]$whilereadline;不要重复$line;完成
最后一行的末尾应该有一个换行符。我将显示错误的文件:

printf "1\n2" > file.txt
wc -l file.txt
1 file.txt
while read line; do
>   echo $line
> done < file.txt
1
printf“1\n2”>file.txt
wc-l file.txt
1.txt文件
读行时;做
>回音$线
>完成

这是你的问题吗?只需
cat file.txt
。当最后一行和命令提示符位于同一行时,您需要在file.txt(类似于
echo>>file.txt
)中添加新行。

什么是
$file.txt
?循环正确。使用
文件$file.txt
检查它是否不包含任何windows行结尾您发布的代码没有明显错误。请编辑您的问题,以显示您在循环中执行的操作,以及输入文件中包含的内容。您的文件中是否有反斜杠字符?如果是这样,您可能希望使用
-r
选项告诉
read
不要将其解释为转义,如在读取-r行时的
;执行
。通常,当使用
read
逐行读取文件时,应使用
-r
。也就是说,除非您希望bash将输入文件中的反斜杠视为转义序列的开始。它包含我要迭代的日期列表。格式为DDMMYYYY。所以我在字符之间没有任何空格。我甚至在记事本++中查找行号,结果只有两行。