I am trying to run the following shell script but I am getting the "/bin/bash^M: bad interpreter" error.
Script:
#!/bin/bash
rm -rf tomcat8/webapps/cpproject*
I know that this is happening because I am editing the file on windows and trying to run it on Unix. I tried all the ways of fixing it as suggested on stackoverflow. I tried the following.
- Installed Vim editor and saved it
- In Notepad++ changed the End Of Line option to Unix. Tried to save the new documents as Unix format
- Installed gedit and saved it.
- Installed doc2unix and tried it.
I tried a lot other ways but nothing worked. Can some one suggest me how to fix this issue?
Updated:
"xxd before_install1.sh | head" command is showing the followng.
00000000: 2321 2f62 696e 2f62 6173 680a 726d 202d #!/bin/bash.rm -
00000010: 7266 2074 6f6d 6361 7438 2f77 6562 6170 rf tomcat8/webap
00000020: 7073 2f63 7070 726f 6a65 6374 2a ps/cpproject*
Update: I made sure that I am not running the old file. Because I changed the file name each time and tried it. I made sure that the new file is being executed each time. Basically I am using codedeploy jenkins plugin which generates the war file and zips it with scripts and appspec.yml file. Since I change the file name each time and update the appspec.yml file with new name, I am making sure that the new file is being uploaded. But it is giving the same error. Even after it is uploaded to the S3 bucket I downloaded and the zip file and made sure that it contains the latest one and even executed the xxd command and got the following.
$ xxd before_install5.sh | head
00000000: 2321 2f62 696e 2f62 6173 680d 0a72 6d20 #!/bin/bash..rm
00000010: 2d72 6620 746f 6d63 6174 382f 7765 6261 -rf tomcat8/weba
00000020: 7070 732f 6370 7072 6f6a 6563 742a 0d0a pps/cpproject*..
Update:
@Charles Duffy, Thank you for your response. I am developing these scripts locally and pushing them to GitHub. I have a Jenkins job, which takes this code from GitHub repository and packages this into a war file. I am using "CodeDeploy" plugin, which will take this war file and zips it with these shell scripts under scripts folder and appspec.yml file and push it to S3 bucket in AWS. The following is what is happening.
xxd command in my local system file:
$ xxd before_install5.sh | head
00000000: 2321 2f62 696e 2f62 6173 680a 726d 202d #!/bin/bash.rm -
00000010: 7266 2074 6f6d 6361 7438 2f77 6562 6170 rf tomcat8/webap
00000020: 7073 2f63 7070 726f 6a65 6374 2a0a ps/cpproject*.
xxd command on the file after it is uploaded to GitHub:
$ xxd before_install5.sh | head
00000000: 2321 2f62 696e 2f62 6173 680a 726d 202d #!/bin/bash.rm -
00000010: 7266 2074 6f6d 6361 7438 2f77 6562 6170 rf tomcat8/webap
00000020: 7073 2f63 7070 726f 6a65 6374 2a0a ps/cpproject*.
xxd command on the file after it is pulled by Jenkins from GitHub to Local Workspace(in .jenkins/worspace folder)
$ xxd before_install5.sh | head
00000000: 2321 2f62 696e 2f62 6173 680d 0a72 6d20 #!/bin/bash..rm
00000010: 2d72 6620 746f 6d63 6174 382f 7765 6261 -rf tomcat8/weba
00000020: 7070 732f 6370 7072 6f6a 6563 742a 0d0a pps/cpproject*..
xxd command on the file after it is pushed to s3 bucket in AWS:
$ xxd before_install5.sh | head
00000000: 2321 2f62 696e 2f62 6173 680d 0a72 6d20 #!/bin/bash..rm
00000010: 2d72 6620 746f 6d63 6174 382f 7765 6261 -rf tomcat8/weba
00000020: 7070 732f 6370 7072 6f6a 6563 742a 0d0a pps/cpproject*..
I am seeing that the extra characters '0d0a' are added after the code is taken by Jenkins into Local Workspace. It's not a problem with CodeDeploy plugin at this momement, it seems. Is there any way we can avoid this?
Thank you.