Bash 在当前目录中执行shell脚本

Bash 在当前目录中执行shell脚本,bash,shell,Bash,Shell,我正在编写一个shell脚本,列出占用空间最多的五个文件夹。以下是我所拥有的: $ du -m --max-depth 1 | sort -rn 5 这很好,但是如何让这段代码检查调用它的目录?检查这是否回答了您的问题: $dir=`pwd` if [ $dir = "some_directory_you_are_expecting" ] then du -m --max-depth 1 | sort -rn 5 else do_some_other_thing fi 您将

我正在编写一个shell脚本,列出占用空间最多的五个文件夹。以下是我所拥有的:

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

这很好,但是如何让这段代码检查调用它的目录?

检查这是否回答了您的问题:

$dir=`pwd`

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

您将5作为要排序的文件,但您希望对du的输出进行排序。也许你的种类不同。我的是从

在这里:

$ 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

我有一种感觉,这个问题还有很多;以这种方式直接通过命令行、shell脚本或shell函数执行du只能在当前工作目录中执行。你在看什么?你想看什么?