解决shell脚本执行报错:sudo: unable to execute ./script.sh: No such file or directory

导致这个问题的原因大部分是脚本跨平台编写和运行的兼容性有问题。
这里我们用dos2unix工具将脚本的格式转换一下。

dos2unix是将Windows格式文件转换为Unix、Linux格式的实用命令。Windows格式文件的换行符为\r\n
,而Unix&Linux文件的换行符为\n. dos2unix命令其实就是将文件中的\r\n 转换为\n。

而unix2dos则是和dos2unix互为孪生的一个命令,它是将Linux&Unix格式文件转换为Windows格式文件的命令。

命令语法:

dos2unix [options] [-c convmode] [-o file …] [-n infile outfile …]

unix2dos [options] [-c convmode] [-o file …] [-n infile outfile …]

sudo apt-get install dos2unix -y
dos2unix test.sh
sudo chmod u+x test.sh && sudo ./test.sh

转换完成后,亲测shell脚本可以正常运行了。

你可能感兴趣的:(Ubuntu,Linux,shell,脚本)