Linux中脚本script.sh的运行讲解,linuxscript.sh


1.认识脚本:下面是个简单的脚本

[root@centos06 tmp]# vi script.sh

#!/bin/bash #这行是shebang,shebang是个文本行,其中#!位于解释器路径之前,/bin/bash 是解释器命令路径

echo 'helle this is the first script!' #我的脚本命令:向控制台输出一句话:helle this is the first script!:

2.脚本的执行:脚本的执行共有两种方式:

方式一、将脚本作为bash的命令行来运行

bash script.sh (在script.sh所在的当前目录中)

或者 bash /home/hadoop/script.sh (在任意目录下适用)

这种方式可以不写shebang

方式二、利用shebang实现脚本的独立运行

vi script.sh

#!/bin/bash

echo 'helle this is the first script!'

除了必须声明shebang,还必须给脚本赋予运行权限:

在脚本所在目录下: chmod a+x script.sh

有了权限后,可以通过如下命令运行脚本:

./scrip.sh或者 /home/hadoop/script.sh

相关内容