bash bash中带有while循环的一元运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11125718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unary Operator with a while loop in bash
提问by Robert
I'm trying to get a bash script setup so it'll move files up to a specified size limit(in this case 1GB) from one directory to another. I'm trying to get it to enter the loop but I'm having issues with the while statement. It keeps returning the below output and I'm not sure why. If I try to use "$currentSize" in the while bash says it's expecting an integer comparison. But variables in bash are supposed to be untyped, so I can't cast it to an intiger, right? Any help is appropriated.
我正在尝试设置 bash 脚本,以便将文件从一个目录移动到指定的大小限制(在本例中为 1GB)到另一个目录。我试图让它进入循环,但我的 while 语句有问题。它不断返回以下输出,我不知道为什么。如果我尝试在 while bash 中使用 "$currentSize" 说它期待一个整数比较。但是 bash 中的变量应该是无类型的,所以我不能将它转换为整数,对吗?任何帮助都是适当的。
Output
输出
54585096
1048576
./dbsFill.bsh: line 9: [: -lt: unary operator expected
Code
代码
#!/bin/bash
currentSize= du -s /root/Dropbox | cut -f 1
maxFillSize=1048576
echo $currentSize
echo $maxFillSize
cd /srv/fs/toSort/sortBackup
while [ $currentSize -lt $maxFillSize ]
do
#fileToMove= ls -1
#rsync -barv --remove-source-files $fileToMove /root/Dropbox/sortme
# mv -t /root/Dropbox/sortme $(ls -1 | head -n 1)
#find . -type f -exec mv -t /root/Dropbox/sortme {} \;
#currentSize= du -s /root/Dropbox | cut -f 1
# sleep 5
echo Were are here
done
回答by Eric Smith
What you're trying to achieve by:
您试图通过以下方式实现的目标:
currentSize= du -s /root/Dropbox | cut -f 1
is to capture the current size of /root/Dropboxin currentSize. That isn't happening. That's because whitespace is significant when setting shell variables, so there's a difference between:
是捕获/root/Dropboxin的当前大小currentSize。这不会发生。这是因为在设置 shell 变量时空格很重要,所以有以下区别:
myVar=foo
and
和
myVar= foo
The latter tries to evaluate foowith the environment variable myVarset to nothing.
后者尝试foo在环境变量myVar设置为空的情况下进行评估。
Bottom line: currentSizewas getting set to nothing, and line 9 became:
底线:currentSize被设置为空,第 9 行变为:
while [ -lt 1048576 ]
and of course, -ltisn't a unary operator which is what the shell is expecting in that situation.
当然,-lt它不是一元运算符,这是 shell 在这种情况下所期望的。
To achieve your intent, use Bash command substitution:
要实现您的意图,请使用Bash 命令替换:
currentSize=$( du -s /root/Dropbox | cut -f 1 )
回答by Ignacio Vazquez-Abrams
currentSize= du -s /root/Dropbox | cut -f 1
currentSize= du -s /root/Dropbox | cut -f 1
This does not do what you think it does.
这不会像你认为的那样做。
currentSize=$(du -s /root/Dropbox | cut -f 1)
回答by shellter
you need to re-write your 'currentSize ...' line as
您需要将“currentSize ...”行重新编写为
currentSize=$(du-s /root/Dropbox | cut -f 1)
Your code left the value of currentSize empty.
您的代码将 currentSize 的值留空。
You can spot such problems (with a little practice), but using the shell debugging feature
您可以发现此类问题(稍加练习),但使用 shell 调试功能
set -vx
at the top of your script, or if you think you're pretty sure where there's a problem, surround your suspiocus code like:
在脚本的顶部,或者如果您认为自己很确定问题出在哪里,请将可疑代码括起来,例如:
set -vx
myProblematicCode
set +vx
(the set +vxturns off debug mode.)
(set +vx关闭调试模式。)
I hope this helps.
我希望这有帮助。

