基本bash语法问题 - 意外令牌附近的语法错误

时间:2022-05-07 22:44:59

I think my script does what its meant to do, at first I just had

我认为我的剧本完成了它的意图,起初我就是这么做的

#!/bin/bash
for file in /home/parallels/Desktop/trashcan/*
    do
    echo "Would you like to delete - " $file 
done 

I then wanted to add the obvious missing functionality so I now have

然后我想添加明显缺少的功能,所以我现在有

#!/bin/bash
for file in /home/parallels/Desktop/trashcan/*
do
    echo "Would you like to delete - " $file
    read line
    if [$line == y|Y]
        sudo rm $file
fi
done 

Thats where I'm at now, I did at first try to use a case statement instead of the if as I have a working script with the case statement I'd need but simply copying it over gives me the same error - syntax error near unexpeted token, I get this for fi and done

多数民众赞成在我现在的地方,我首先尝试使用case语句而不是if,因为我有一个工作脚本我需要的case语句,但只是复制它给我同样的错误 - 语法错误附近未完成的令牌,我得到这个为fi和完成

3 个解决方案

#1


1  

[ is a command, so it must be separated by whitespace from its first argument:

[是一个命令,所以它必须用第一个参数的空格分隔:

if [ "$line" = y ] || [ "$line" = Y ]; then
    sudo rm "$file"
fi

If you are using bash, you can replace the standard usage shown above with the more concise

如果您使用的是bash,则可以更简洁地替换上面显示的标准用法

if [[ $line = [yY] ]]; then
    sudo rm "$file"
fi

As Keith Thompson pointed out, only an answer of exactly 'y' or 'Y' will allow the file to be removed. To allow 'yes' or 'Yes' as well, you can use

正如Keith Thompson指出的那样,只有'y'或'Y'的答案才能删除文件。要允许“是”或“是”,您也可以使用

shopt -s extglob
if [[ $line = [yY]?(es) ]]

(The shopt line is only required for earlier versions of bash. Starting with version 4, extended patterns are the default in a conditional expression.)

(只有早期版本的bash才需要shopt行。从版本4开始,扩展模式是条件表达式中的默认模式。)

#2


0  

The if part should be

if部分应该是

if [ "$line" = "y" ] || [ "$line" = "Y" ]
then
    sudo rm $file
fi

#3


0  

I faced similar problem. I had opened the .sh file in windows and Windows has added CRLF at the end of every line.

我遇到了类似的问题。我在Windows中打开了.sh文件,Windows在每一行的末尾添加了CRLF。

I found this out by running

我通过跑步找到了这个

cat --show-nonprinting filename.extension

E.g.

例如。

cat --show-nonprinting test.sh

Then, I converted it using

然后,我用它转换它

dos2unix filename.extension

E.g.

例如。

dos2unix myfile.txt
dos2unix myshell.sh

#1


1  

[ is a command, so it must be separated by whitespace from its first argument:

[是一个命令,所以它必须用第一个参数的空格分隔:

if [ "$line" = y ] || [ "$line" = Y ]; then
    sudo rm "$file"
fi

If you are using bash, you can replace the standard usage shown above with the more concise

如果您使用的是bash,则可以更简洁地替换上面显示的标准用法

if [[ $line = [yY] ]]; then
    sudo rm "$file"
fi

As Keith Thompson pointed out, only an answer of exactly 'y' or 'Y' will allow the file to be removed. To allow 'yes' or 'Yes' as well, you can use

正如Keith Thompson指出的那样,只有'y'或'Y'的答案才能删除文件。要允许“是”或“是”,您也可以使用

shopt -s extglob
if [[ $line = [yY]?(es) ]]

(The shopt line is only required for earlier versions of bash. Starting with version 4, extended patterns are the default in a conditional expression.)

(只有早期版本的bash才需要shopt行。从版本4开始,扩展模式是条件表达式中的默认模式。)

#2


0  

The if part should be

if部分应该是

if [ "$line" = "y" ] || [ "$line" = "Y" ]
then
    sudo rm $file
fi

#3


0  

I faced similar problem. I had opened the .sh file in windows and Windows has added CRLF at the end of every line.

我遇到了类似的问题。我在Windows中打开了.sh文件,Windows在每一行的末尾添加了CRLF。

I found this out by running

我通过跑步找到了这个

cat --show-nonprinting filename.extension

E.g.

例如。

cat --show-nonprinting test.sh

Then, I converted it using

然后,我用它转换它

dos2unix filename.extension

E.g.

例如。

dos2unix myfile.txt
dos2unix myshell.sh