如何将文本/纯文本转换为text / x.shellscript [英] how to convert a text/plain to text/x.shellscript

查看:129
本文介绍了如何将文本/纯文本转换为text / x.shellscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将从Windows机器创建的.sh文件发送到linux,以便可以在那里运行它。问题是我一直在遇到一个错误的解释器错误。但是当我在Linux机器上编写shell脚本时,即使它与Windows机器发送的代码具有相同的代码,它也可以正常运行。经过我的调查,我发现Windows机器的.sh脚本是一个文本/纯文本文件(使用文件-bi),而Linux机器的另一个是text / x.shellscript。有没有一种方法可以将text / plain转换为text / x.shellscript?谢谢

I am sending a .sh file created from a windows machine to a linux so that I could run it there. The problem is that I keep on getting an error called bad interpreter.But when I program the shell script in the linux machine it runs with no problems even though it has the same code with the one sent from the windows machine. After my ivestigation, I found out that the windows machine .sh script is a text/plain file(using file -bi) and the other one from the linux machine is a text/x.shellscript. Is there a way to convert the text/plain to a text/x.shellscript? thank you

这是脚本:

#!/bin/bash

date
sudo apt-get update

I通过在仅包含

#!/bin/bash

然后Windows机器仅发送包含以下测试命令的文件:

Then the windows machine only sent a file containing test commands like :

date
hostname

然后我附加

cat windows.sh >> linux.sh

如果我运行linux.sh,它将无法正常工作。它显示以下错误:

It did not work if I run linux.sh. It says errors like:

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

但是,如果我打开Linux.sh,则无需进行任何操作即可再次保存它。

However, if I open Linux.sh then save it again without doing anything. It works

推荐答案

我在下面总结了您需要采取的步骤,以便其他用户可以轻松地了解需要做什么:

I'm summarising below the steps you need to take so other users can see easily what needs doing:

首先,您需要检查脚本中#!后的解释器路径是否正确。在第一行。这可能应该是:

Firstly, you need to check your script has the correct path to your interpreter after the "#!" in the very first line. This is should probably be:

#!/bin/bash

#!/usr/bin/bash

,您可以通过输入以下内容找到正确的答案:

and you can find which is correct by typing:

which bash 

第二,您需要确保删除Linux行末的所有Windows回车符(或 ^ M),然后才能期望Linux机器运行脚本。您可以这样操作:

Secondly, you need to make sure that any Windows carriage returns (or "^M") at the ends of the lines are removed before expecting your Linux box to run the script. You can do this with:

dos2unix yourscript

仅供参考,您可以使用以下命令轻松查看Linux中的TAB或换行符或回车符之类的奇怪字符:

Just for reference, you can easily see weird characters such as TABs or linefeeds or carriage returns in Linux by using:

cat -vet yourfile

sed -n l yourfile

第三,您需要使用chmod来确保脚本在Linux上可执行:

Thirdly, you need to make sure your script is executable on Linux, using chmod like this:

chmod +x yourscript

最后,完成所有这些操作后,您需要将脚本所在的目录添加到PATH变量中(并将其导出),或者如果您的脚本位于当前目录中,则可以这样给出脚本的完整路径:

Finally, when you have done all that, you need to either add the directory where the script is located to your PATH variable (and export it) or give the full path to your script like this if your script is in the current directory:

./yourscript

或者如果位于其他地方则类似

or like this if it is located somewhere else

/some/directory/some/where/yourscript

这篇关于如何将文本/纯文本转换为text / x.shellscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆