1

A bash script running in Amazon Linux 2 is trying to test if a file exists. Then if it does exist, the script should delete the file.

The following error gets thrown every time the script runs. What needs to change in the script in order for the error to no longer be a problem?

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

Here is the entire script:

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


User Suggestions

Per @steve's suggestion, I got the following:

[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

Note: The contents of /var/spool/mail/username are not new. Just the same error shown at top above.

Per @Jesse_b's suggestion:

[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


Solution

I changed the script to the following, then I ran dos2unix on the script before calling the script. The result is a working script now:

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

Also, to confirm @steve's suspicion, I see that python was being invoked in a cronjob definition, which I have corrected as follows:

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

Before the correction, python was being called in the preceding line and not bash. So I am marking @steve's answer as accepted.

closed as off-topic by roaima, Jesse_b, terdon Mar 27 at 20:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – roaima, Jesse_b, terdon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    What is the contents of your PATH? What if you run the script with bash script.sh? – Jesse_b Mar 27 at 19:46
  • 1
    How (exactly) are you running this script? – roaima Mar 27 at 19:54
  • 1
    Your script has windows style line endings in it, you will need to run dos2unix on it or similar. This likely caused it to not understand your hashbang and your default interpreter is python. – Jesse_b Mar 27 at 19:56
  • 1
    @Jesse_b See the OP for the solution, to which you contributed with your dos2unix suggestion – CodeMed Mar 27 at 20:05
5

The error you're getting suggests you're actually running it via Python.

Has some fool overwritten your bash binary with python ? Try running bash --version.

Example

[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 ~]$

As an aside, I'm a fan of a simple rm /home/username/hosts 2>/dev/null rather than the if/then/rm/fi approach. i.e. try to delete it regardless of whether it exists.

  • 2
    I posted results of bash --version at the end of the OP, per your request. – CodeMed Mar 27 at 19:42
  • 1
    Why not use rm -f /home/username/hosts? – Freddy Mar 27 at 19:45
  • 2
    @CodeMed: It is not but you are almost certainly using python to interpret a bash script. – Jesse_b Mar 27 at 19:48
  • 2
    @steve See the OP for the solution. Given how the error was resolved, can you please explain how Python is involved? If you still think that Python is involved. Thank you. – CodeMed Mar 27 at 20:07
  • 2
    See my comment at end of OP regarding how I found python and fixed that aspect of the problem. Thank you and +1 for helping to diagnose. – CodeMed Mar 27 at 20:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.