Bash 脚本“[!: not found”错误,以及如何编写 or 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1821236/
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
Bash Scripting " [!: not found " errors, and how to write an or statement
提问by ???
EDIT: Here is my updated code:
编辑:这是我更新的代码:
#!/bin/sh
files=`ls`
if [ $# -ne 1 -o -f ]
then
echo "Usage: #!/bin/sh
files=`ls`
if [ $# -ne 1 ]
then
echo "Usage: #!/bin/sh
files=`ls`
if [ $# -ne 1 ]
then
echo "Usage: if [! -d $f]
<directory>"
exit 1
fi
if [ ! -e ]
then
echo " not found"
exit 1
elif [ -d ]
then
cd
for f in $files
do
if [! -d $f]
then
if [ ! -s $f ]
then
rm-r $f
echo "File: $f was removed."
else
continue
fi
fi
done
echo "Name\t\tLinks\t\tOwner\t\tDate"
for f in $files
do
find $f -type f -printf "%f\t\t %n\t\t %u\t %TH %Tb %TY\n"
done
exit 0
fi
<directory>"
exit 1
fi
if [ ! -e ]
then
echo " not found"
exit 1
elif [ -d ]
then
cd
for f in $files
do
if [! -d $f]
then
if [ ! -s $f ]
then
rm-r $f
echo "File: $f was removed."
else
continue
fi
fi
done
echo "Name\t\tLinks\t\tOwner\t\tDate"
for f in $files
do
find $f -type f -printf "%f\t\t %n\t\t %u\t %TH %Tb %TY\n"
done
exit 0
fi
<directory>"
exit 1
fi
if [ ! -e ]
then
echo " not found"
exit 1
elif [ -d ]
then
cd
for f in $files
do
if [ ! -d "$f" ]
then
if [ ! -s "$f" ]
then
rm -r "$f"
echo "File: $f was removed."
else
continue
fi
fi
done
echo "Name\t\tLinks\t\tOwner\t\tDate"
for f in $files
do
find "$f" -type f -printf "%f\t\t %n\t\t %u\t %TH %Tb %TY\n"
done
exit 0
fi
I fixed all of the spacing issues, changed #!bin/sh to #/bin/bash, and added quotes to "$f". However I'm still getting lots of errors.
我修复了所有间距问题,将 #!bin/sh 更改为 #/bin/bash,并将引号添加到“$f”。但是我仍然收到很多错误。
ava@kosh:~/test$ ./delDir d1 rm: cannot remove
d1': No such file or directory File: d1 was removed. rm: cannot removedelDir': No such file or directory File: delDir was removed. rm: cannot removedelDir2': No such file or directory File: delDir2 was removed. rm: cannot removee1': No such file or directory File: e1 was removed. rm: cannot removee2': No such file or directory File: e2 was removed. rm: cannot removemake_d1': No such file or directory File: make_d1 was removed. Name\t\tLinks\t\tOwner\t\tDate find: d1: No such file or directory find: delDir: No such file or directory find: delDir2: No such file or directory find: e1: No such file or directory find: e2: No such file or directory find: make_d1: No such file or directory ne1 2
ava 22 Nov 2009 ne2
2 ava 22 Nov 2009
ava@kosh:~/test$ ./delDir d1 rm: cannot remove
d1': No such file or directory File: d1 was removed. rm: cannot removedelDir': No such file or directory File: delDir 被删除。rm: 无法删除delDir2': No such file or directory File: delDir2 was removed. rm: cannot removee1': 没有这样的文件或目录 File: e1 被删除。rm: 无法删除e2': No such file or directory File: e2 was removed. rm: cannot removemake_d1': 没有这样的文件或目录 文件: make_d1 已被删除。Name\t\tLinks\t\tOwner\t\tDate find: d1: 没有找到那个文件或目录: delDir: 没有那个文件或目录找到: delDir2: 没有那个文件或目录找到: e1: 没有找到那个文件或目录: e2: 没有这样的文件或目录 find: make_d1: 没有这样的文件或目录 ne1 2
ava 22 Nov 2009 ne2
2 ava 22 Nov 2009
Does anyone know what else I'm doing wrong?
有谁知道我还做错了什么?
Here's my code:if [ ! -d $f -a -f $f ] if [ ! -d $f -o -f $f ]Here are my questions:
If I execute the script with something that is NOT an ordinary file AND is NOT a directory I want it to say "Usage: Filename directory" (see line 5). I know I can do this with 2 if statements but is it possible to create an or statement for this in bash?
When I run the script I keep getting errors like this:
./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found Name
Links Owner Date find: d1: No such file or directory find: delDir: No such file or directory find: delDir2: No such file or directory e1 1
ava 22 Nov 2009 e2
1 ava 22 Nov 2009 find: make_d1: No such file or directory ne1 2
ava 22 Nov 2009 ne2
2 ava 22 Nov 2009
I believe I am getting these errors because the for loop is first looking for the file that the user typed in (the directory it changed into) and cannot find it. How can if fix this? 3. Are there any more errors you can see?
这是我的代码:files = `ls`以下是我的问题:
如果我使用不是普通文件且不是目录的内容执行脚本,我希望它说“用法:文件名目录”(参见第 5 行)。我知道我可以用 2 个 if 语句来做到这一点,但是是否可以在 bash 中为此创建一个 or 语句?
当我运行脚本时,我不断收到这样的错误:
./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found . /delDir: 39: [!: not found Name
Links Owner Date find: d1: No such file or directory find: delDir: No such file or directory find: delDir2: No such file or directory e1 1
ava 22 Nov 2009 e2
1 ava 22 Nov 2009 find: make_d1: No such file or directory ne1 2
ava 22 Nov 2009 ne2
2 ava 22 Nov 2009
我相信我收到这些错误是因为 for 循环首先查找用户输入的文件(它更改到的目录)并且找不到它。如果能解决这个问题怎么办?3. 你还能看到更多错误吗?
回答by Mikael S
You forgot to put a space after [and before ]on the line saying:
你忘了在行[前后放一个空格]说:
rm -r $f
AND tests are created using -a, -ois equal to OR:
AND 测试是使用-a,-o等于 OR创建的:
rm -r "$f"
回答by singingwolfboy
Try putting a space between the [and !.
尝试在[和之间放置一个空格!。
回答by Dave Kirby
Another issue is that at the start of the script you do
另一个问题是,在脚本开始时,您执行
##代码##but then you cd to a different directory and try to loop round $files deleting them - of course this will not work since you have changed directories.
但是然后你 cd 到一个不同的目录并尝试循环 $files 删除它们 - 当然这不会起作用,因为你已经改变了目录。
move the ls line to after you have changed directory.
更改目录后将 ls 行移至。
回答by Richard Pennington
The other answers are correct, here's why: [ is a command. If you type "[!", the shell looks for a command by that name.
其他答案是正确的,原因如下: [ 是一个命令。如果您键入“[!”,shell 将查找具有该名称的命令。
回答by Tim Post
I'm augmenting other answers here ... every time I see a bash question start with #! /bin/shI have to jump in, its a moral imperative.
我在这里增加了其他答案......每次我看到一个 bash 问题时,#! /bin/sh我都必须跳进去,这是一种道德要求。
Keep in mind that /bin/shpoints to the POSIX invocation of bash, or a completely different "posixically correct" shell like dash. /bin/sh is often a symbolic link that causes bash to alter its behavior to be more POSIX compliant. Hence, lots of goodies won't work as usual, or you may find your code being parsed by another shell.
请记住,这/bin/sh指向 bash 的 POSIX 调用,或者像破折号这样的完全不同的“位置正确”shell。/bin/sh 通常是一个符号链接,它会导致 bash 改变其行为以更符合 POSIX 标准。因此,很多好东西不会像往常一样工作,或者您可能会发现您的代码正在被另一个 shell 解析。
In other words, /bin/sh == POSIX_ME_HARDER, but .. yikes! ==is a bashism:)
换句话说, /bin/sh == POSIX_ME_HARDER,但是..哎呀!==是一种bashism:)
If you want bash, use #!/bin/bash
如果你想要 bash,请使用 #!/bin/bash
Beyond that, singingwolfboy's answer should fix your immediate problem :)
除此之外,singwolfboy 的回答应该可以解决您当前的问题:)
回答by amrox
The others got the spacing issue with the [, but I noticed a couple other things.
其他人遇到了 的间距问题[,但我注意到了其他一些事情。
You probably need a space in your rmcommand in line 23:
您可能需要rm在第 23 行的命令中添加一个空格:
Also, it's usually good practice to double quote file paths. This will allow your script to handle filenames with spaces and other special characters correctly.
此外,双引号文件路径通常是一种很好的做法。这将允许您的脚本正确处理带有空格和其他特殊字符的文件名。
##代码##
