Bash忽略我的条件:if[$variable<;10]]
我有一个简单的bash脚本,如果行数小于10,则将文件内容复制到文件a,否则将内容复制到文件B 代码如下:Bash忽略我的条件:if[$variable<;10]],bash,if-statement,terminal,conditional-statements,Bash,If Statement,Terminal,Conditional Statements,我有一个简单的bash脚本,如果行数小于10,则将文件内容复制到文件a,否则将内容复制到文件B 代码如下: # File A and B are created before, since I'm in a loop # file content is different each time. I add it either to A or B corresponding the matching. # I know I could use wc -l directly on the file
# File A and B are created before, since I'm in a loop
# file content is different each time. I add it either to A or B corresponding the matching.
# I know I could use wc -l directly on the file too but I don't think the problem came from
# it since i've tested both.
let nblines=$(cat file | wc -l)
if [[ $nblines < 10 ]]; then
cat file >> A
else
cat file >> B
fi
有人知道问题可能来自哪里吗?
感谢阅读。试试:
if [ $nblines -lt 10 ]; then
如果
let nblines=$(cat file | wc -l)
if [[ $nblines -lt 10 ]]; then
cat file >> A
else
cat file >> B
fi
为什么不呢
[[ $(wc -l < file) -lt 10 ]] && cat file >> A || cat file >> B
[[$(wc-l>A | | cat文件>>B
或
[[$(wc-l>“$x”
提示:如果您想捕获工具的输出(例如,发布到web上),设置LC_ALL=en_en
会使输出显示在英语区域设置中。如果您想检查文件是否少于10行,最好使用head-n10 file | wc-l
而不是cat file | wc-l
(或wc-l
)如果可以确定文件有10行或更多行,则提前结束wc
。
if [ "$nblines" -lt 10 ]; then
if (( nblines < 10 )); then
[[ $(wc -l < file) -lt 10 ]] && cat file >> A || cat file >> B
[[ $(wc -l < file) -lt 10 ]] && x=A || x=B
cat file >> "$x"