写在开头:shell 菜鸟的第一次自己编写shell时遇到的问题,大神请绕行,多谢!
1.shell 脚本的简单内容如下:
#!/bin/sh
#Date:2018/09/17
main()
{
echo "---------This is my shell start---------------"
ls
echo "---------This is my shell end-----------------"
}
main ..
exit 0
2.将文件保存为test_001.sh后从windows系统移到linux服务器后执行,发现出现上述错误,网上查资料后解决了此问题,具体步骤如下:
(1)出错的原因是因为:在windows上编辑的.sh文件的格式为dos格式,而linux只能执行格式为unix格式的脚本。所以如果是在windows上编写的.sh文件,需要先经过转换为linux上支持的unix格式的脚本。
(2)我们可以通过vim编辑器,查看文件的格式(使用set ff命令):
#!/bin/sh
#Date:2018/09/17
main()
{
echo "---------This is my shell start---------------"
ls
echo "---------This is my shell end-----------------"
}
main ..
exit 0
~
~
~
~
~
~
~
~
~
~
~
~
fileformat=dos
(3)然后有两种方式可以实现dos到unix的文件格式的转换。
第一种使用dos2unix命令修改,简单快捷 ,如:
$ dos2unix test_001.sh
dos2unix: converting file test_001.sh to UNIX format ...
第二种,使用set ff=unix命令,需要使用vim先打开文件,然后设置set ff=unix ,接着保存退出即可。
(4)修改完以后,我们可以再次使用set ff命令查看文件的格式是否改变。