zoukankan      html  css  js  c++  java
  • shell脚本基础

    一:shell基础

    概述:shell脚本与python一样是一种解释性语言

    应用:

    Shell 脚本的优势在于处理偏操作系统底层的业务,例如,Linux 内部的很多应用(有的是应
    用的一部分)都是使用 Shell 脚本开发的,因为有 1000 多个 Linux 系统命令为它作支撑,
    特别是 Linux 正则表达式以及三剑客 grep、awk、sed 等命令。
    对于一些常见的系统脚本,使用 Shell 开发会更简单、更快速,例如,让软件一键自动化安
    装、优化,监控报警脚本,软件启动脚本,日志分析脚本等,虽然 Python 也能做到这些,但
    是考虑到掌握难度、开发效率、开发习惯等因素,它们可能就不如 Shell 脚本流行以及有优势
    了。对于一些常见的业务应用,使用 Shell 更符合 Linux 运维简单、易用、高效的三大原则

    二:shell 脚本

    #!/bin/bash         
       #   ————————》#!是一个约定的标记,它告诉系统这个脚本需要什么解释器来执#         
       #行,即使用哪一种 Shell;后面的/bin/bash就是指明了解释器的具体位置。
    echo "Hello World !"  #这是一条语句
    

    1 查看shell

     

      查看默认shell

     

    二 变量

    1 变量的声明与调用

    2 变量的引用注意点

    3 将命令结果赋值给变量

    [root@server1 ~]# log=$(cat a.txt)  #命令结构赋值
    [root@server1 ~]# echo $log  #输出变量
    server { listen port; server_name localhost ; location / { root html; index index.html index.htm; } } server { listen port; server_name localhost ; location / { root html; index index.html index.htm; } }
    [root@server1 ~]# log1=$(cat text.sh)  #命令结构赋值
    [root@server1 ~]# echo $log1    #输出变量
    #!/bin/bash url="http://www.baidu.com" web1="百度一下你就知道 ${url}" web2='百度一下你就知道 ${url}' echo $web1 echo $web2

    只读变量:

    4 变量的删除

    三 变量的作用域

    1 局部变量

    [root@server1 ~]# vim t1.sh
    [root@server1 ~]# chmod +x t1.sh 
    [root@server1 ~]# ./t1.sh 
    99
    [root@server1 ~]# cat t1.sh 
    #!/bin/bash
    #定义函数
    
    function func(){
    	a=99
    }
    
    #调用函数
    func
    #输出函数内部变量
    echo $a
    

    2 全局变量

    [root@server1 ~]# a=22    #定义一个全局变量
    [root@server1 ~]# echo $a   #输出全局变量
    22
    [root@server1 ~]# bash      # 进入shell子进程
    [root@server1 ~]# echo $a  #输出变量值
    
    [root@server1 ~]# exit #推出shell进程
    exit
    [root@server1 ~]# export a  #将a定义为环境变量
    [root@server1 ~]# bash      #进入shell环境
    [root@server1 ~]# echo $a  #输出变量值
    22
    [root@server1 ~]# exit  #推出shell环境
    exit

     三:shell命令替换

    四:shell 脚本传递参数

    2 给函数传递位置参数

    [root@server1 ~]# vim t4.sh
    [root@server1 ~]# cat t4.sh 
    #!/bin/bash
    #定义函数
    function func() {
    	echo "language :$1"
    	echo "URL:$2"
    }
    
    #调用函数
    fun c++ http://c.biancheng.net/cplus
    [root@server1 ~]# chmod +x t4.sh 
    [root@server1 ~]# ./t4.sh 
    language :c++
    URL:http://c.biancheng.net/cplus

    五: shell中特殊的变量

    Shell特殊变量:Shell $#、$*、$@、$?、$$

    变量含义
    $0 当前脚本的文件名。
    $n(n≥1) 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是 $1,第二个参数是 $2。
    $# 传递给脚本或函数的参数个数。
    $* 传递给脚本或函数的所有参数。
    $@ 传递给脚本或函数的所有参数。当被双引号" "包含时,$@ 与 $* 稍有不同,我们将在《Shell $*和$@的区别》一节中详细讲解。
    $? 上个命令的退出状态,或函数的返回值,我们将在《Shell $?》一节中详细讲解。
    $$ 当前 Shell 进程 ID。对于 Shell 脚本,就是这些脚本所在的进程 ID。

    六 shell 中字符串的操作

    1 字符串的拼接(直接拼接)

    [root@server1 ~]# name="good"
    [root@server1 ~]# name1="hello"
    [root@server1 ~]# echo name name1
    name name1
    [root@server1 ~]# echo $name $name1
    good hello
    

    2字符串的切割

    [root@server1 ~]# url="www.baidu.com"
    [root@server1 ~]# echo ${url:2:9}
    w.baidu.c
    [root@server1 ~]# echo ${url:4:9}
    baidu.com
    [root@server1 ~]# echo ${url:4:-1}
    baidu.co
    [root@server1 ~]# echo ${url:-4:-1}
    www.baidu.com

    七 shell中的数组

    1 数组的定义

    [root@server1 ~]# lst=(1 2 3 4 5 6 7)  #定义数组
    [root@server1 ~]# echo ${lst[1]}   #查看索引对应的数组
    2
    [root@server1 ~]# echo ${lst[2]}
    3
    [root@server1 ~]# echo ${lst[3]}
    4
    [root@server1 ~]# echo ${lst[*]}   #查看所有数组元素
    1 2 3 4 5 6 7 
    [root@server1 ~]# echo ${lst[@]}  #查看所有元素数组
    1 2 3 4 5 6 7
    [root@server1 ~]# lst=(1 2 3 4 5 6 7 8)
    [root@server1 ~]# echo ${#lst[*]}    #查看数组长度
    8
    

    2 数组拼接

    [root@server1 ~]# lst2=(a b c d e f)
    [root@server1 ~]# lst0=(${lst[*]} ${lst2[*]})
    [root@server1 ~]# echo ${lst0[*]}
    1 2 3 4 5 6 7 8 a b c d e f
    

    3 删除数组

    [root@server1 ~]# unset lst0[1]
    [root@server1 ~]# echo ${lst0[*]}
    1 3 4 5 6 7 8 a b c d e f
    

      

     

  • 相关阅读:
    jchdl
    jchdl
    UVa 10256 (判断两个凸包相离) The Great Divide
    UVa 11168 (凸包+点到直线距离) Airport
    LA 2572 (求可见圆盘的数量) Kanazawa
    UVa 10652 (简单凸包) Board Wrapping
    UVa 12304 (6个二维几何问题合集) 2D Geometry 110 in 1!
    UVa 10674 (求两圆公切线) Tangents
    UVa 11796 Dog Distance
    LA 3263 (平面图的欧拉定理) That Nice Euler Circuit
  • 原文地址:https://www.cnblogs.com/liucsxiaoxiaobai/p/11027805.html
Copyright © 2011-2022 走看看