When a Hackster.io project includes a shell script as a code attachment, the file name gets munged and it adds CR/NL to each file. The resulting file cannot be run on a raspberry pi. There is a fairly easy fix to this and it can be done on either the Mac or on your Raspberry Pi.
Another oddity I noticed while writing this story - uploading seems to strip out quotes ("). Check your uploads!
File Name MungingWhen you download a file (see myTestScript.sh in Code section here) from Hackster.io (Chrome and Safari tested), the resulting file is renamed, converting ".sh" to "_sh.txt" and the text has added CR/NL added. (eg mytestscript_sh.txt). The dos2unix tool is an effective work around.
Dos2Unix on a MacOn a Mac, retrieve the code from GitHub (https://github.com/dataf3l/dos2unix-for-mac-osx ) and copy the dos2unix file to your /usr/local/bin folder (in a terminal window execute:
sudo cp dos2unix /usr/local/bin
Then you can use the tool as
dos2unix mytestscript_sh.txt
Note that this is a simple script that does NOT rename the file. You will need to
mv mystestscript_sh.txt myTestScript.sh
you can then scp the shell script to your Pi and it will work as intended.
For example when downloaded try this on your mac:
$ bash ./mytestscript_sh.txt
This is a simple test bash shell script
: command not foundxt: line 6: date
It should demonstrate the issue with name munging
and crnl when downloading and copy to mac/raspberry pi
: command not foundxt: line 9: ls
the dos2unix tool fixes it
note that the "echo" commands work but the regular commands like "date" and "ls" fail.
the corrected output is:
$ bash myTestScript.sh
This is a simple test bash shell script
Sun Dec 3 11:07:08 HST 2017
It should demonstrate the issue with name munging
and crnl when downloading and copy to mac/raspberry pi
myTestScript.sh mytestscript_sh.txt
the dos2unix tool fixes it
Dos2Unix on a Raspberry Pi
If your files are already transferred to the Pi, you can apt-get dos2unix and convert locally...
sudo apt-get install dos2unix
Then you can use the tool as above with one file name, or (since this is a more advanced version) you can use the -n flag to create a new file from the output
dos2unix -n mytestscript_sh.txt
For example...
pi@raspberrypi:~ $ bash ./mytestscript_sh.txt
This is a simple test bash shell script
./mytestscript_sh.txt: line 6: $'date\r': command not found
It should demonstrate the issue with name munging
and crnl when downloading and copy to mac/raspberry pi
./mytestscript_sh.txt: line 9: $'ls\r': command not found
the dos2unix tool fixes it
pi@raspberrypi:~ $
Running dos2unix and then bash gives ...
pi@raspberrypi:~ $ dos2unix -n mytestscript_sh.txt myTestScript.sh
dos2unix: converting file mytestscript_sh.txt to file myTestScript.sh in Unix format ...
pi@raspberrypi:~ $ bash ./myTestScript.sh
This is a simple test bash shell script
Sun 3 Dec 21:13:11 UTC 2017
It should demonstrate the issue with name munging
and crnl when downloading and copy to mac/raspberry pi
Desktop myTestScript.sh python_games testNew.sh
Documents mytestscript_sh.txt setupPiImage.sh test.sh
Downloads Pictures Templates Videos
Music Public test1.sh wpa_supplicant.conf
the dos2unix tool fixes it
pi@raspberrypi:~ $
Comments