Linux – How to convert text/plain text to text/x.shellscript

How to convert text/plain text to text/x.shellscript… here is a solution to the problem.

How to convert text/plain text to text/x.shellscript

I’m sending a .sh file created from a Windows machine to Linux so I can run it there. The problem is that I keep getting an error called bad interpreter. But when I write a shell script on a linux machine, it runs without problems, even though it’s the same code sent from a windows machine. After investigating, I found that the .sh script for Windows machines is a text/plain file (using file -bi), while the other script from Linux machines is text/x.shellscript. Is there a way to convert text/plain to text/x.shellscript? Thanks

Here is the script:

#!/bin/bash

date
sudo apt-get update

I tried to do this by including only

#!/bin/bash

The Windows machine then sends only a file containing the test command, such as:

date
hostname

Then I appended the file from the windows box into Linux

cat windows.sh >> linux.sh

If I run linux.sh, it doesn’t work. It says the error is as follows:

./linuxh.sh: line 2 $'date\r':command not found
./linuxh.sh: line 2 $'hostname\r':command not found

However, if I open Linux.sh and then save it again without doing anything. Effective

Solution

I’ve summarized the steps you need to take below so that other users can easily see what needs to be done:

First, you need to check your script in the “#!” Is there a correct interpreter path after that. In the first line. This could probably be:

#!/bin/bash

or

#!/usr/bin/bash

You can find the right one by typing:

which bash 

On your Linux machine.

Second, you need to make sure that any Windows carriage returns (or “^M”) at the end of the line are removed before expecting your Linux machine to run the script. You can do this:

dos2unix yourscript

FYI, you can easily see strange characters in Linux such as TAB or newline or carriage return by:

cat -vet yourfile

or

sed -n l yourfile

Third, you need to make sure your script is executable on Linux, using chmod as follows:

chmod +x yourscript

Finally, when you’ve done all of this, you’ll need to add the directory where the script is located to your PATH variable (and export it) or if your script is in the current directory:

./yourscript

If it’s somewhere else, like this

/some/directory/some/where/yourscript

Related Problems and Solutions