简体   繁体   English

在当前目录中执行Shell脚本

[英]Executing a shell script in the current directory

I am writing a shell script to list the five folders consuming the most space. 我正在编写一个Shell脚本来列出五个占用最大空间的文件夹。 Here is what I have: 这是我所拥有的:

$ du -m --max-depth 1 | sort -rn 5

This works fine, but how do get this code to check the directory that it is called from? 这可以正常工作,但是如何获取此代码来检查调用它的目录?

Check whether this answers your question: 检查这是否回答了您的问题:

$dir=`pwd`

if [ $dir = "some_directory_you_are_expecting" ]
then
    du -m --max-depth 1 | sort -rn 5
else
    do_some_other_thing
fi

You are giving "5" as the file to be sorted, but you want to sort the output from du. 您将“ 5”作为要排序的文件,但您想对du的输出进行排序。 Perhaps your sort is different. 也许您的排序有所不同。 Mine is from http://www.gnu.org/software/coreutils/ 我的来自http://www.gnu.org/software/coreutils/

here: 这里:

$ echo 'du -sm * | sort -rn | head -n5 ' > ~/bin/top5.sh 
raptor: ~
$ chmod 755 ~/bin/top5.sh
raptor: ~
$ top
top      top5.sh  
raptor: ~
$ top5.sh 
606     src
532     svn
407     tmp
349     images
45      workspace
raptor: ~
$ cd /usr/
raptor: /usr
$ top5.sh 
du: cannot read directory `lib64/mozilla/extensions': Permission denied
du: cannot read directory `lost+found': Permission denied
du: cannot read directory `share/ggz/modules': Permission denied
1928    lib64
1842    share
1779    src
492     bin
457     lib32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM