初识shell脚本

1.1 脚本认识

  •  1 #!/bin/bash 

第一行,一般用来指定执行脚本的shell ,/bin/bash是CentOS 默认的shell  若是写到第二行,就是注释了html

  •  1 #!/bin/sh 

这样写也是能够的,sh是bash的软连接,和#!/bin/bash 是没有区别的,理规范的开头使用#!/bin/bashlinux

1.2 查看bash版本

  •  1 [root@oldboy-lesson scripts]# bash --version  2 GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu)  3 Copyright (C) 2009 Free Software Foundation, Inc.  4 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
     5  
     6 This is free software; you are free to change and redistribute it.  7 There is NO WARRANTY, to the extent permitted by law.  8 [root@oldboy-lesson scripts]# cat test_1.sh
     9 #!/bin/bash 10 user=`whoami` 11 echo $user 12 [root@oldboy-lesson scripts]# sh test_1.sh
    13 root 14 [root@oldboy-lesson scripts]# ./test_1.sh
    15 root 16 [root@oldboy-lesson scripts]# . test_1.sh
    17 root 18 [root@oldboy-lesson scripts]# source test_1.sh
    19 root 20 [root@oldboy-lesson scripts]# /bin/bash test_1.sh
    21 root 22 [root@oldboy-lesson scripts]# /bin/sh test_1.sh
    23 root 24 [root@oldboy-lesson scripts]# /server/scripts/test_1.sh
    25 root

     

1.3 书写脚本

 

1.3.1 执行脚本

 

1.3.1.1  小结:redis

执行脚本的方法有:sh       ./          .           source /bin/bash         /bin/sh pwd/shell

1.4 继承脚本变量

  •  1 [root@oldboy-lesson scripts]# sh test_1.sh
     2 root  3 [root@oldboy-lesson scripts]# echo $user  4  
     5 [root@oldboy-lesson scripts]# ./test_1.sh
     6 root  7 [root@oldboy-lesson scripts]# echo $user  8  
     9 [root@oldboy-lesson scripts]# bash test_1.sh
    10 root 11 [root@oldboy-lesson scripts]# echo $user 12  
    13 [root@oldboy-lesson scripts]# . test_1.sh
    14 root 15 [root@oldboy-lesson scripts]# echo $user 16 root 17 [root@oldboy-lesson scripts]# source test_1.sh
    18 root 19 [root@oldboy-lesson scripts]# echo $user 20 root

1.4.1.1  小结bash

继承:source 和 .      能继承脚本的变量,其它命令皆不能继承less

相关文章
相关标签/搜索