语法错误检查文件是否存在

语法错误检查文件是否存在

在 Amazon Linux 2 中运行的 bash 脚本正在尝试测试文件是否存在。如果确实存在,脚本应该删除该文件。

每次脚本运行时都会抛出以下错误。为了使错误不再成为问题,需要在脚本中更改哪些内容?

File "/home/username/write_hosts.sh", line 3
    if [ -f /home/username/hosts ]
                                 ^
SyntaxError: invalid syntax

这是整个脚本:

#!/usr/bin/env bash

if [ -f /home/username/hosts ]
then
    rm /home/username/hosts
fi

declare -a arr_of_tags=("this-master" "this-worker")

for i in "${arr_of_tags[@]}"
do
   echo ["$i"] >> /home/username/hosts
   echo "About to test ip array for $i"
   declare -a arr_of_ips=$(aws ec2 describe-instances --region us-west-2 --query "Reservations[*].Instances[*].PrivateIpAddress" --filter "Name=tag:Name,Values=$i" --output=text)
   for j in "${arr_of_ips[@]}"
   do 
      echo "$j" >> /home/username/hosts
   done
done


用户建议

根据 @steve 的建议,我得到以下信息:

[username@ip-10-0-0-73 ~]$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-koji-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
You have new mail in /var/spool/mail/username

注意: 的内容/var/spool/mail/username并不新鲜。与上面顶部显示的错误相同。

根据@Jesse_b 的建议:

[username@ip-10-0-0-73 ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/username/.local/bin:/home/username/bin

[username@ip-10-0-0-73 ~]$ sudo bash write_hosts.sh
write_hosts.sh: line 2: $'\r': command not found
write_hosts.sh: line 7: $'\r': command not found
write_hosts.sh: line 9: $'\r': command not found
write_hosts.sh: line 11: $'\r': command not found
write_hosts.sh: line 13: syntax error near unexpected token `$'do\r''
'rite_hosts.sh: line 13: `do


解决方案

我将脚本更改为以下内容,然后dos2unix在调用脚本之前运行该脚本。现在的结果是一个工作脚本:

#!/usr/bin/env bash

rm -f /home/username/hosts

declare -a arr_of_tags=("this-master" "this-worker")

for i in "${arr_of_tags[@]}"
do
   echo ["$i"] >> /home/username/hosts
   echo "About to test ip array for $i"
   declare -a arr_of_ips=$(aws ec2 describe-instances --region us-west-2 --query "Reservations[*].Instances[*].PrivateIpAddress" --filter "Name=tag:Name,Values=$i" --output=text)
   for j in "${arr_of_ips[@]}"
   do
      echo "$j" >> /home/username/hosts
   done
done

另外,为了证实 @steve 的怀疑,我看到 python 正在 cronjob 定义中被调用,我已更正如下:

echo '* * * * * lnxcfg bash /home/username/write_hosts.sh' | sudo tee  /etc/cron.d/refresh_hosts

更正之前,python在前一行中被调用,而不是bash。所以我将 @steve 的答案标记为已接受。

答案1

您收到的错误表明您实际上是通过 Python 运行它。

有傻瓜用 python 覆盖你的 bash 二进制文件吗?尝试跑步bash --version

例子

[steve@centos ~]$ cat x1
#!/usr/bin/env bash

if [ -f /home/username/hosts ]
then
    rm /home/username/hosts
fi
[steve@centos ~]$ ./x1
[steve@centos ~]$ python x1
  File "x1", line 3
    if [ -f /home/username/hosts ]
                                 ^
SyntaxError: invalid syntax
[steve@centos ~]$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

[steve@centos ~]$

顺便说一句,我更喜欢简单的方法,rm /home/username/hosts 2>/dev/null而不是if/then/rm/fi方法。即尝试删除它,无论它是否存在。

相关内容