Home  >  Article  >  Operation and Maintenance  >  An explanation of bash programming in Linux

An explanation of bash programming in Linux

巴扎黑
巴扎黑Original
2017-07-19 11:14:451792browse

为何叫做shell?
在介绍 shell 是什么东西之前,不妨让我们重新审视使用者与电脑的关系。我们知道电脑的运作不能离开硬件,但使用者却无法直接对硬件作驱动,硬件的驱动只能透过一个称为“操作系统(Operating System)”的软件来控管,事实上,我们每天所谈的linux,严格来说只是一个操作系统,我们称之为“核心(kernel)”。然而,从使用者的角度来说,使用者也没办法直接操作kernel,而是透过kernel的“外壳”程序,也就是所谓的shell,来与kernel沟通。
这也正是kernel跟shell的形像命名关系。

从技术角度来说,shell是一个使用者与系统的互动界面(interface),主要是让使用者透过命令行(command line)来使用系统以完成工作。因此,shell的最简单的定义就是---命令解释器(Command Interpreter):

将使用者的命令翻译给核心处理,

同时,将核心处理结果翻译给使用者。

每次当我们完成系统登入(log in),我们就取得一个互动模式的shell,也称为login shell或primary shell。若从行程(process)角度来说,我们在shell所下达的命令,均是shell所产生的子行程。这现像,我们暂可称之为fork。如果是执行脚本(shell script)的话,脚本中的命令则是由另外一个非互动模式的子shell(sub shell)来执行的。也就是primary shell产生sub shell的行程,sub shell再产生script中所有命令的行程。(关于行程,我们日后有机会再补充。)

这里,我们必须知道:kernel与shell是不同的两套软件,而且都是可以被替换的:

不同的操作系统使用不同的kernel,

而在同一个kernel之上,也可使用不同的shell。

bash编程也叫shell编程

预定义变量
        $? 最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确的执行;如果这个变量返回的值非0(具体是那个数,有命令自己来决定),则证明上一个命令执行不正确。
        $$当前进程的进程号(PID)
        $!后台运行的最后一个进程的进程号(PID)
  运算符
        declare 声名变量的类型  
        declare [+/-][选项] 变量名
        - 给变量设定类型属性
        + 取消变量的类型属性
        -i 将变量声名为整数型
        -x将变量什申明为环境变量
        -p 显示指定变量的被声名的类型

举例:

<p>a<span style="color: rgb(128, 128, 128)">=</span><span style="color: rgb(128, 0, 0); font-weight: bold">1</span><span style="color: rgb(0, 0, 0)">b</span><span style="color: rgb(128, 128, 128)">=</span><span style="color: rgb(128, 0, 0); font-weight: bold">2</span><span style="color: rgb(0, 0, 255)">declare</span> <span style="color: rgb(128, 128, 128)">-</span>i c <span style="color: rgb(128, 128, 128)">=</span> $a<span style="color: rgb(128, 128, 128)">+</span><span style="color: rgb(0, 0, 0)">$b
c</span><span style="color: rgb(128, 128, 128)">=</span>$(($a<span style="color: rgb(128, 128, 128)">+</span>$b))</p><p>注意用双小括号,因为Linux中认为一切皆为字符。</p><p>如果不加()当成一个命令,shell脚本中Linux命令在 $() 中运行,当然可以直接写命令,如果则一些引用命令时 就要 $()</p><p><br></p>

单分支if  条件语句

if [ 条件判断式 ];then程序
fior  if [ 条件判断式 ]then程序
fi

判断登录用户是否为root用户:

#!/bin/bash

#grep 获取行通配符
#cut 获取列统配符,以 = 分割 2 获取第二列
login_name=$(env | grep LOGNAME | cut -d "=" -f 2)
#echo $login_nameif [ "$login_name" != "root" ]thenecho 'isnot root'fi

判断根分区的占用率

#!/bin/bash
#df -h 任性话显示磁盘的利用率
#awk 获取第五列
#cut 对字符用 % 进行分割 获取第一列值
result=$( df -h | grep sda1 | awk '{print  $5}'  | cut -d % -f 1)if [ "$result" -lt '90' ]thenecho 'the root dir  is not full'fi

双分支if条件语句

if [ 条件判断式 ]then ...else...
fi

判断nginx 是否在运行 (最好不用包含nginx作为文件名)

#!/bin/bash
result=$(ps aux | grep nginx | grep -v grep)if [ -n "$result" ]thenecho "$(date) nginx is ok !"elseecho "$(date) nginx is not ok !"

                sudo /etc/init.d/nginx start &>/dev/nullecho "$(date) restart nginx !!"
fi

多分支if语句

if []then elif []then...else 
   ...if

The above is the detailed content of An explanation of bash programming in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn