0

I am running into an issue where I can run the following lines perfectly in Linux console but fail when they are contained (as shown) inside a .sh file:

aws s3 cp s3://bucket/folder/ . --recursive
python Run.py

The first line of this takes all the files out of AWS S3 and places them in the current folder. The --recursive flag grabs all files in the specified s3 bucket. The second line is a basic python script that comes from the S3 Bucket files and prints "hello world".

when I run it in the console, I am running the .sh by:

chmod +x ./aws_script.sh
./aws_script.sh

The file fails with the error:

Unknown options: --recursive
python: can't open file 'Run.py': [Errno 2] No such file or directory

I have tried using #!/bin/bash at the top of my .sh script but it failed with the following error:

-bash: ./aws_script.sh: /bin/bash^M: bad interpreter: No such file or directory

I am fairly new to Linux and never used a .sh before ( i assume it works just like a .bat file?), so I'm sure I am just missing something simple. Ideas?

WolVes
  • 1,286
  • 2
  • 19
  • 39
  • 2
    Are you editing the files in Windows and then running in a Unix based environment? The ^M indicates a Windows file. – stdunbar Dec 31 '20 at 00:56
  • Yes, I am writing the file in windows then porting it over with Filezilla. – WolVes Dec 31 '20 at 01:04
  • 1
    If you use Filezilla, select ASCII data type there (instead of binary data type). Then, it will convert the newlines on transfer. – lukasl Dec 31 '20 at 12:39

1 Answers1

2

The script seems to be in DOS format (CR LF newlines). The script must be converted to Unix format (LF newlines). Do dos2unix ./aws_script.sh, if you have the dos2unix command, or sed -i 's/\r$//' ./aws_script.sh.

lukasl
  • 436
  • 2
  • 6
  • So this definitely solves the issue. Is there a way I can port over `.sh` files from windows without having to do this prior to running it? – WolVes Dec 31 '20 at 01:15
  • 1
    Select LF newline format in your editor on Windows. If you modify an existing script, editors may keep the LF newline format automatically. Alternatively, comment out the CR of each line by starting a comment with ` #` (notice the space) on the end of each line. – lukasl Dec 31 '20 at 12:32