为什么我可以将 .sh 文件另存为 .txt 并且当我 运行 它仍然有效
Why can I save a .sh file as .txt and it still works when I run it
我的myshellscript.txt里有这个:
#!/bin/sh
if [ -f ]
then
cat
else
echo "Sorry, not found"
fi
为什么即使它是一个 .txt 文件我仍然可以 运行 使用 sh myshellscript.txt someotherfile.txt
因为你在第一行放了一个shebang(魔法线):
#!/bin/sh
这让您的 shell 知道,这是一个可以 运行 的脚本。至少如果您使文件可执行 (chmod +x myshellscript.txt
)。 UNIX 不像 Windows 那样关心文件扩展名,因此它不依赖于文件扩展名,脚本是否可执行。
我的myshellscript.txt里有这个:
#!/bin/sh
if [ -f ]
then
cat
else
echo "Sorry, not found"
fi
为什么即使它是一个 .txt 文件我仍然可以 运行 使用 sh myshellscript.txt someotherfile.txt
因为你在第一行放了一个shebang(魔法线):
#!/bin/sh
这让您的 shell 知道,这是一个可以 运行 的脚本。至少如果您使文件可执行 (chmod +x myshellscript.txt
)。 UNIX 不像 Windows 那样关心文件扩展名,因此它不依赖于文件扩展名,脚本是否可执行。