Linux下腳本的運(yùn)行方式有四種,以腳本test.sh為例:
第一種:bash test.sh
第二種:./test.sh
第三種:. test.sh
第四種:source test.sh
第一種和第二種都是開啟一個(gè)子進(jìn)程運(yùn)行腳本,但是第二種方式運(yùn)行腳本的前提是腳本有可執(zhí)行權(quán)限,需執(zhí)行命令chmod +x test.sh;
第三種和第四種都是在當(dāng)前進(jìn)程運(yùn)行腳本。
[root@docker1 lianxi]# cat test.sh? ? # 查看腳本的內(nèi)容
i=1# 輸出變量i
echo $i# 輸出當(dāng)前的進(jìn)程號
echo $$
[root@docker1 lianxi]# ll test.sh? ? # 查看腳本的權(quán)限,此時(shí)沒有可執(zhí)行權(quán)限
-rw-r--r-- 1 root root 20 11月 16 15:23 test.sh
[root@docker1 lianxi]# bash test.sh?
1
7237
[root@docker1 lianxi]# ./test.sh?
-bash: ./test.sh: 權(quán)限不夠
[root@docker1 lianxi]# . test.sh?
1
7185
[root@docker1 lianxi]# source test.sh?
1
7185
[root@docker1 lianxi]# chmod +x test.sh? # 添加可執(zhí)行權(quán)限[root@docker1 lianxi]# ll test.sh?
-rwxr-xr-x 1 root root 20 11月 16 15:23 test.sh文章來源:http://www.zghlxwxcb.cn/news/detail-505011.html[root@docker1 lianxi]# ./test.sh?
1
7250文章來源地址http://www.zghlxwxcb.cn/news/detail-505011.html
到了這里,關(guān)于Linux下腳本運(yùn)行的四種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!