bash 使用 shell 脚本删除目录内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10794236/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:23:34  来源:igfitidea点击:

Deleting a directory contents using shell scripts

bashshell

提问by Dinesh

I am a newbie to Shell scripting. I want to delete all the contents of a directory which is in HOME directory of the user and deleting some files which are matching with my conditions. After googled for some time, i have created the following script.

我是 Shell 脚本的新手。我想删除用户 HOME 目录中的目录的所有内容,并删除一些符合我的条件的文件。谷歌搜索一段时间后,我创建了以下脚本。

#!/bin/bash
#!/sbin/fuser


PATH="$HOME/di"

echo "$PATH";

if [ -d $PATH ]
then
 rm -r $PATH/*
 fuser -kavf $PATH/.n*
 rm -rf $PATH/.store
 echo 'File deleted successfully :)'
fi

If I run the script, i am getting error as follows,

如果我运行脚本,我会收到如下错误,

/users/dinesh/di
dinesh: line 11: rm: command not found
dinesh: line 12: fuser: command not found
dinesh: line 13: rm: command not found
File deleted successfully :)

Can anybody help me with this?

有人可以帮我解决这个问题吗?

Thanks in advance.

提前致谢。

回答by nhahtdh

You are modifying PATHvariable, which is used by the OS defines the path to find the utilities (so that you can invoke it without having to type the full path to the binary). The system cannot find rmand fuserin the folders currently specified by PATH(since you overwritten it with the directory to be deleted), so it prints the error.

您正在修改PATH操作系统使用的变量,它定义了查找实用程序的路径(以便您可以调用它而无需键入二进制文件的完整路径)。系统在当前指定的文件夹中找不到rm和(因为您用要删除的目录覆盖了它),因此它会打印错误。fuserPATH

tl;dr DO NOT use PATHas your own variable name.

tl;dr 不要PATH用作您自己的变量名。

回答by Gordon Davisson

PATH is a special variable that controls where the system looks for command executables (like rm, fuser, etc). When you set it to /users/dinesh/di, it then looks there for all subsequent commands, and (of course) can't find them. Solution: use a different variable name. Actually, I'd recommend using lowercase variables in shell scripts -- there are a number of uppercase reserved variable names, and if you try to use any of them you're going to have trouble. Sticking to lowercase is an easy way to avoid this.

PATH是一个特殊的变量,它控制在系统查找命令可执行文件(像rmfuser等)。当您将其设置为 时/users/dinesh/di,它会在那里查找所有后续命令,并且(当然)找不到它们。解决方案:使用不同的变量名。实际上,我建议在 shell 脚本中使用小写变量——有许多大写的保留变量名称,如果您尝试使用它们中的任何一个,都会遇到麻烦。坚持小写是避免这种情况的简单方法。

BTW, in general it's best to enclose variables in double-quotes whenever you use them, to avoid trouble with some parsing the shell does after replacing them. For example, use [ -d "$path" ]instead of [ -d $path ]. $path/*is a bit more complicated, since the *won't work inside quotes. Solution: rm -r "$path"/*.

顺便说一句,通常最好在使用变量时用双引号将变量括起来,以避免在替换它们后解析 shell 时遇到麻烦。例如,使用[ -d "$path" ]代替[ -d $path ]$path/*有点复杂,因为*在引号内不起作用。解决办法:rm -r "$path"/*

Random other notes: the #!/sbin/fuserline isn't doing anything. Only the first line of the script can act as a shebang. Also, don't bother putting ;at the end of lines in shell scripts.

随机其他注释:该#!/sbin/fuser行没有做任何事情。只有脚本的第一行可以充当shebang。另外,不要费心;在 shell 脚本中放在行尾。

#!/bin/bash

path="$HOME/di"

echo "$path"

if [ -d "$path" ]
then
 rm -r "$path"/*
 fuser -kavf "$path"/.n*
 rm -rf "$path/.store"
 echo 'File deleted successfully :)'
fi

回答by Jonathan Leffler

This line:

这一行:

PATH="$HOME/di"

removes all the standard directories from your PATH (so commands such as rmthat are normally found in /binor /usr/binare 'missing'). You should write:

从您的 PATH 中删除所有标准目录(因此rm,通常会在其中找到/bin/usr/bin“丢失”的命令)。你应该写:

PATH="$HOME/di:$PATH"

This keeps what was already in $PATH, but puts $HOME/diahead of that. It means that if you have a custom command in that directory, it will be invoked instead of the standard one in /usr/binor wherever.

这会保留 $PATH 中已有的内容,但会$HOME/di提前。这意味着如果您在该目录中有自定义命令,它将被调用,而不是在其中/usr/bin或任何地方调用标准命令。

If your intention is to remove the directory $HOME/di, then you should not be using $PATH as your variable. You could use $path; variable names are case sensitive. Or you could use $diror any of a myriad other names. You do need to be aware of the key environment variables and avoid clobbering or misusing them. Of the key environment variables, $PATHis one of the most key ($HOME is another; actually, after those two, most of the rest are relatively less important). Conventionally, upper case names are reserved for environment variables; use lower case names for local variables in a script.

如果您打算删除该目录$HOME/di,则不应使用 $PATH 作为变量。你可以使用$path; 变量名区分大小写。或者您可以使用$dir或 无数其他名称中的任何一个。您确实需要了解关键的环境变量并避免破坏或滥用它们。在关键的环境变量中,$PATH是最关键的一个($HOME 是另一个;实际上,在这两个之后,其余的大部分都相对不那么重要)。通常,大写名称是为环境变量保留的;在脚本中对局部变量使用小写名称。