gpt4 book ai didi

bash - bash 中的参数错误太多

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

我正在编写一个脚本来删除目录中的所有文件以进行练习。我在我的变量周围使用引号,但我仍然收到以下错误:

/usr/local/bin/deleteall: line 6: [: too many arguments
/usr/local/bin/deleteall: line 11: [: too many arguments

这是我的代码:

#!/bin/bash
#Deletes all files in the current directory

read -p "You are about to delete all files in $(pwd). Are you sure you want to do this? y/n" yn
echo $yn
if [ [ "$yn" = "y" ] -o [ "$yn" = "Y" ] ] ; then
for i in `ls`; do
rm $i
done
exit;
elif [ [ "$yn" = "n" ] -o [ "$yn" = "N" ] ] ; then
exit;
else
read -p "Please enter y (yes) or n (no)"
exit;
fi

这是整个输出:

You are about to delete all files in <my dir>. Are you sure you want to do this? y/nn
n
/usr/local/bin/deleteall: line 6: [: too many arguments
/usr/local/bin/deleteall: line 11: [: too many arguments
Please enter y (yes) or n (no)n

我做错了什么?

最佳答案

这一行似乎有问题:

if [ [ "$yn" = "y" ] -o [ "$yn" = "Y" ] ] ; then

您可以将其替换为:

if [[ "$yn" == [yY] ]]; then

PS:对检查 n 或 N 的行执行相同的操作。

关于bash - bash 中的参数错误太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21253969/

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