①编辑.sh文件时自动生成关于脚本文件说明的注释
[root@RHCE11 ~]# vim /root/.vimrc
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#########################")
call setline(3,"#File name:".expand("%"))
call setline(4,"#Version:v1.0")
call setline(5,"#Email:[email protected]")
call setline(6,"#Created time:".strftime("%F %T"))
call setline(7,"#Description:")
call setline(8,"#########################")
call setline(9,"")
endif
endfunc
②创建文件并编辑(vim中的+表示跳到文件的最后一行)
[root@RHCE11 ~]# mkdir /chap01
[root@RHCE11 ~]# cd /chap01
[root@RHCE11 chap01]# vim + ./test0.sh
#!/bin/bash
#########################
#File name:./test0.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-12-03 16:19:44
#Description:
#########################
read -p "please input a number:" n
echo `seq -s "+" $n`=$[`seq -s "+" $n`]
③修改所属者的权限(注意:./filename.sh这样的文件会产生子进程再运行,使用脚本里面指定的shell去运行,因此,使用该种方式执行需要x权限)
[root@RHCE11 chap01]# chmod u+x ./test0.sh
④测试
[root@RHCE11 chap01]# ./test0.sh
please input a number:10
1+2+3+4+5+6+7+8+9+10=55
①编辑文件
[root@RHCE11 chap01]# vim + ./test1.sh
#!/bin/bash
#########################
#File name:./test1.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-12-03 16:35:26
#Description:
#########################
read -p 'input file:' file
echo ${file%/*}
echo .${file##*.}
②修改权限
[root@RHCE11 chap01]# chmod u+x ./test1.sh
③测试
[root@RHCE11 chap01]# ./test1.sh
input file:/etc/shell/test1.th
/etc/shell
.th
①编辑文件
[root@RHCE11 chap01]# vim + ./test2.sh
#!/bin/bash
#########################
#File name:./test2.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-12-03 16:39:27
#Description:
#########################
read -p 'input number:' num
echo ${#num}
②修改权限
[root@RHCE11 chap01]# chmod u+x ./test2.sh
③测试
[root@RHCE11 chap01]# ./test2.sh
input number:1234
4
①编辑文件
[root@RHCE11 chap01]# vim + ./test3.sh
#!/bin/bash
#########################
#File name:./test3.sh
#Version:v1.0
#Email:[email protected]
#Created time:2023-12-03 16:41:58
#Description:
#########################
read -p 'input a dir:' dir
ls $dir | wc -l
②修改权限
[root@RHCE11 chap01]# chmod u+x ./test3.sh
③测试
[root@RHCE11 chap01]# ./test3.sh
input a dir:/root
1