一,什么是shell?
1)shell也是操作系统中的一个软件
2)它包在 linux 内核的外面,为用户和内核之间的交互提供了一个接口
3)系统中的命令用shell去解释,shell接收系统回应的输出并显示其到屏幕中
二,Shell的使用:
1,设置指定用户在创建Shell脚本的时候可以自动显示标示作者的信息:
[kiosk@foundation92 ~]$ pwd
/home/kiosk #在kiosk用户的家目录下
[kiosk@foundation92 ~]$ vim .vimrc
autocmd BufNewFile *.sh exec ":call SetComment_sh()"
func SetComment_sh()
call setline(1,"#!/bin/bash")
call setline(2,"")
call setline(3,"#==================================================================")
call setline(4,"# Copyright (C) ".strftime("%Y")."Westos All rights reserved.")
call setline(5,"# ")
call setline(6,"# 文件名称:".expand("%:t"))
call setline(7,"# 创建者:AmbiaSf")
call setline(8,"# 创建日期:".strftime("%Y年%m月%d日"))
call setline(9,"# 描 述:")
call setline(10,"#")
call setline(11,"#=================================================================")
call setline(12,"")
call setline(13,"")
endfunc
创建完成后,在使用kiosk用户的身份去创建.sh文件的时会自动显示标示作者的信息。
2,Shell脚本的执行方式:
[root@foundation92 shell]# vim script.sh
name='test';
echo 'hello';
[root@foundation92 shell]# ll script.sh
-rw-r--r-- 1 root root 27 Mar 29 15:01 script.sh
1)chmod +x script.sh
./script.sh
当不加x权限的时候,无法执行
[root@foundation92 shell]# ./script.sh
bash: ./script.sh: Permission denied
[root@foundation92 shell]# chmod +x script.sh
[root@foundation92 shell]# ./script.sh
hello
[root@foundation92 shell]# echo $name #此时无法显示name变量的值(test),原因是该种方式产生了一个子进程Shell,因此echo $name仍然找不到该变量。
2)sh script.sh或者bash script.sh
当脚本文件本身没有可执行权限(即文件权限属性x位为-号)时常使用的方法,或者脚本文件开头没有指定解释器时需要使用的方法。推荐使用这种方法.
[root@foundation92 shell]# bash script.sh
hello
[root@foundation92 shell]# echo $name
name的值是空的。不难理解,bash script-name是产生了一个子进程shell,而我们当前的操作还在父shel中因此得不到该变量值。
3)source script.sh 或者. script.sh
[root@foundation92 shell]# source script.sh
hello
[root@foundation92 shell]# echo $name
test
可以成功的输出变量的值。这是因为source script-name和. script-name是将script-name中的内容直接加载到当前的shell,因此能够输出当前变量的值。