加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Bash Scripting“[!:not found”错误,以及如何编写或声明

发布时间:2020-12-16 01:56:12 所属栏目:安全 来源:网络整理
导读:编辑: 这是我更新的代码: #!/bin/shfiles=`ls`if [ $# -ne 1 -o -f $1 ]then echo "Usage: $0 directory" exit 1fiif [ ! -e $1 ]then echo "$1 not found" exit 1elif [ -d $1 ]thencd $1for f in $filesdo if [ ! -d "$f" ] then if [ ! -s "$f" ] then
编辑:
这是我更新的代码:

#!/bin/sh
files=`ls`
if [ $# -ne 1 -o -f $1 ]
then
        echo "Usage: $0 <directory>"
        exit 1
fi
if [ ! -e $1 ]
then
        echo "$1 not found"
        exit 1

elif [ -d $1 ]
then
cd $1

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 "NamettLinksttOwnerttDate"

for f in $files
        do
                find "$f" -type f -printf "%ftt %ntt %ut %TH %Tb %TYn"
        done
        exit 0
fi

我修复了所有的间距问题,将#!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 remove
delDir’: No such file
or directory File: delDir was removed.
rm: cannot remove delDir2': No such
file or directory File: delDir2 was
removed. rm: cannot remove
e1′: No
such file or directory File: e1 was
removed. rm: cannot remove e2': No
such file or directory File: e2 was
removed. rm: cannot remove
make_d1′:
No such file or directory File:
make_d1 was removed.
NamettLinksttOwnerttDate 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

有谁知道我做错了什么?

Here’s my code:

06001

Here are my questions:

  1. 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?

  2. 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

??
??我相信我得到了这些错误
??因为for循环是第一次看
??用户输入的文件
??(它改变的目录)和
??找不到.怎么能解决这个问题?
??3.您还可以看到更多错误吗?

解决方法

另一个问题是,在脚本开始时你会这样做

files = `ls`

但是然后你cd到另一个目录并尝试循环$文件删除它们 – 当然这不会有效,因为你已经更改了目录.

更改目录后移动ls行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读