shell脚本在linux无法运行

shell脚本在linux无法运行

在windows写的.sh脚本,直接把文件传到Linux之后运行,报错:

$ bash ./v_1.sh
: command not found
'/v_1.sh: line 4: syntax error near unexpected token `do
'/v_1.sh: line 4: `do

脚本内容是:

#shellvirus I#

for file in ./infect/*
do
    cp $0 $file
done

语法上并没有错误,报错是因为在Windows下编写的脚本通常会使用\r\n(回车+换行)作为换行符,而Linux则使用\n(换行)
在linux中使用命令:

sed -i 's/\r$//' your_script.sh

替换掉换行符就能正常运行。
批量替换就把脚本名字your_script.sh改成通配符*.sh,这样会处理当前目录下的所有.sh文件。

posted @ 2024-11-10 19:46  米小白  阅读(96)  评论(0编辑  收藏  举报