gpt4 book ai didi

Bash 脚本 "[!: not found "错误,以及如何编写 or 语句

转载 作者:行者123 更新时间:2023-11-29 09:15:52 26 4
gpt4 key购买 nike

编辑:这是我更新的代码:

#!/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 "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

我修复了所有间距问题,将#!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. 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

还有谁知道我做错了什么吗?

Here's my code:

#!/bin/sh
files=`ls`
if [ $# -ne 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 "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

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. 你能看到更多的错误吗?

最佳答案

你忘了在 [ 之后和 ] 之前放置一个空格,表示:

if [! -d $f]

AND测试是使用-a创建的,-o等于OR:

if [ ! -d $f -a -f $f ]
if [ ! -d $f -o -f $f ]

关于Bash 脚本 "[!: not found "错误,以及如何编写 or 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1821236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com