Shell

一,什么是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,因此能够输出当前变量的值。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值