bash linux shell脚本中使用read命令解析du -s输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/619432/
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
Using the read command to parse du -s output in linux shell script
提问by Jared
I'm trying to write a shell script to perform actions if a users home directory if over a certain size. Unfortionately when I try to use the read command to split up the du -soutput I get 'command not found' since it tries to pass the number through to the shell, not into a variable like I want. Here's what I have so far for the script.
如果用户主目录超过特定大小,我正在尝试编写一个 shell 脚本来执行操作。不幸的是,当我尝试使用 read 命令拆分du -s输出时,我得到“找不到命令”,因为它试图将数字传递给 shell,而不是像我想要的那样传递给变量。到目前为止,这是我对脚本的了解。
#!/bin/bash
cd /home
for i in `ls`
do
j=`du -s $i`
#this line gives me the error
k=`$j |read first;`
done
I get output like the following:
我得到如下输出:
./takehome.sh: line 6: 3284972: command not found
where 3284972 is the size of the directory.
其中 3284972 是目录的大小。
回答by Andy
I think you'll want something along the lines of
我想你会想要一些类似的东西
#!/bin/bash
for i in *
do
j=`du -s "$i" | cut -f 1`
echo $i size is $j
done
Reading into another variable, read, and playing with that seems redundant.
读入另一个变量,读取并使用它似乎是多余的。
I believe a more elegant solution is Jonathan Leffler's second method (copy-pasted here)
我相信一个更优雅的解决方案是乔纳森莱夫勒的第二种方法(复制粘贴在这里)
#!/bin/bash
cd /home
du -s * |
while read size name
do
...
done
回答by Jonathan Leffler
There are at least two ways to do this:
至少有两种方法可以做到这一点:
Method 1:
方法一:
#!/bin/bash
cd /home
for i in `ls`
do
set -- `du -s $i`
size=
name=
...
done
Method 2:
方法二:
#!/bin/bash
cd /home
du -s * |
while read size name
do
...
done
回答by Dave Webb
It's understandable that's happening as that's what you're telling it to. Backticks mean: evaluate this string, run it as a command and give me the output.
这是可以理解的,因为这就是你告诉它的。反引号的意思是:评估这个字符串,将它作为命令运行并给我输出。
Also, read namemeans read data in to the variable name.
此外,read name意味着将数据读入变量name。
Try:
尝试:
echo $j | read k
Or even removing the previous line:
甚至删除前一行:
du -s $i | read k
(Edit- Testing this properly, it works fine in kshbut not in bashfor some reason. I think it might be something to do with subshells. Use the backticks and cutsolution below if you're using bash.)
(编辑- 正确测试它,它可以正常工作,ksh但bash由于某种原因不能正常工作。我认为这可能与子外壳有关。cut如果您使用的是 bash,请使用下面的反引号和解决方案。)
You could keep your backticks and use cutrather than read:
您可以保留反引号并使用cut而不是read:
k=`$du -s $i | cut -f1`
In this case it probably doesn't give you much more, but cutis more powerful than readwhich might help you sometime in the future.
在这种情况下,它可能不会给你更多,但cut比read将来某个时候可能对你有帮助的更强大。
回答by Cantillon
If you just want to limit the amount of disk space that users can use, you might want to check out the concept of disk quotas.
如果您只想限制用户可以使用的磁盘空间量,您可能需要查看磁盘配额的概念。
Starting point: Quota @ LQWiki
起点:配额@LQWiki

