Bash

檢查文件是否存在的語法錯誤

  • March 27, 2019

在 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"] &gt;&gt; /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" &gt;&gt; /home/username/hosts
  done
done

此外,為了確認@steve 的懷疑,我看到在 cronjob 定義中呼叫了 python,我已將其更正如下:

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

在更正之前,python在前一行中被呼叫而不是bash。所以我將@steve 的回答標記為已接受。

您收到的錯誤表明您實際上是通過 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&gt;/dev/null而不是if/then/rm/fi方法。即無論它是否存在都嘗試刪除它。

引用自:https://unix.stackexchange.com/questions/509065