前言
在Linux下編寫shell腳本時,每次都要使用chmod +x 文件名
的方式給文件賦予可執行權限,那有沒有一種簡單的方法,可以自動識別shell腳本並為其添加執行權限,經過網上搜索,發現可以配置vimrc來實現
步驟
[root@localhost chapter3]# vim ~/.vimrc
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endif
:wq
chmod +x ~/.vimrc
驗證
# 創建demo.sh,什么都不輸入,保存退出,這時候發現沒有可執行權限
[root@localhost chapter3]# vim demo.sh
:wq
[root@localhost chapter3]# ll
total 24
-rw-r--r--. 1 root root 0 May 31 23:00 demo.sh
-rwxr-xr-x. 1 root root 851 May 31 22:19 ex3-1.sh
-rwxr-xr-x. 1 root root 533 May 31 22:33 ex3-3.sh
-rwxr-xr-x. 1 root root 69 May 31 22:41 ex3-4.sh
-rwxr-xr-x. 1 root root 219 May 31 22:46 ex3-5.sh
-rwxr-xr-x. 1 root root 149 May 31 22:49 ex3-6.sh
-rwxr-xr-x. 1 root root 13 May 31 22:53 ex3-7.sh
# 只要第一行是以#!開頭,且包含/bin/,配置就會自動的賦予可執行權限
[root@localhost chapter3]# vim demo.sh
#! /bin/bash
:wq
[root@localhost chapter3]# ll
total 28
-rwxr-xr-x. 1 root root 13 May 31 23:01 demo.sh
-rwxr-xr-x. 1 root root 851 May 31 22:19 ex3-1.sh
-rwxr-xr-x. 1 root root 533 May 31 22:33 ex3-3.sh
-rwxr-xr-x. 1 root root 69 May 31 22:41 ex3-4.sh
-rwxr-xr-x. 1 root root 219 May 31 22:46 ex3-5.sh
-rwxr-xr-x. 1 root root 149 May 31 22:49 ex3-6.sh
-rwxr-xr-x. 1 root root 13 May 31 22:53 ex3-7.sh