shell script 8-13

First, the text processing tool

wc command

wc (Word count) statistics for the number of characters in a text file

 

15 is the row number 78 is the number of characters 805 is the file size (bytes) byte is 8 bits [1]

2, parameters: -l (count the number of rows), - w (count the number of words), - c (the number of bytes)

 

cut (sliced) command

1 , designated delimiter, cut files

2, the common parameters: -d (designated delimiter), - f (which specifies the extraction column), - output-delimiter = '' (the output of the separator, they want to use to replace delimiter)

Example: cut -d "" -f1 / etc / fstab (taking the first column)

    cut -d "" -f1,3 / etc / fstab (1,3 take the first column)

    cut -d "" -f1-3 / etc / fstab (take the first 1-3)

    cut -d [:] -f1d / etc / fstab (designated delimiter is a colon and a space) ([] represents any character in brackets are matched)

. 3, Cut not specify a plurality of separators, awk can specify multiple delimiters

 [ Cut limitations: 1 , -d to specify delimiters can not be specified at the same time more than   2 , can not do high-level format of the output; so to grasp the awk command]

sort command

1, sort command according to ASCII code to sort, not the numerical size

2, commonly used parameters: -r (reverse), -f (ignore case characters)

         -t (specified field delimiter), -n (sorted by the numerical size)

         -u (deduplication sorting, repeated not shown)

       -k (specify which column to sort)

uniq command

1, uniq command is used to weight, but only continuous and repeated the same order to be considered a sort [it must first de-emphasis]

2, commonly used parameters: -c (Statistical some characters repeat of words) [Example: the Sort filename | uniq -c ]

                -d (display not only been repeated rows)

Exercise: Remove the / etc / passwd file first . 6 to 10 rows, and the information in the first three sort fields of numerical values, and finally only the display of each of the first field

           Head /etc/passwd | tail -5 | sort -t: -k3 -n |cut -d” “ -f1

 Second, Principles of Programming

1, the mechanical limitations of the code (hexadecimal) of: identifying the machine, is not compatible with a variety of brands

2, the machine can identify binary instructions

3, the program instructions = + Data

4, the drive can not use the default hardware device; the CPU controls the hardware resources, the CPU of the arithmetic controller composed

5, driver: when an instruction is required to communicate between the different manufacturers of hardware devices, drivers we need to "translate"

6, closer to hardware development engineers to learn the "assembly language", but is limited by the factory assembly language

7, C language and C ++ low-level assembly language

8, the operating system used to allocate hardware resources (operating system written in C language)

9、现在的编程基于高级语言及超高级语言,更好的让程序员能够实现编程功能。高级语言及超高级语言需要翻译成计算机可读的语言(二进制语言),翻译分为两种:解释型(逐行翻译,逐行执行,      例:bash -x filename(方便排错))与编译型(一次编译,全部执行)

10、Shell,Python为解释型语言,编译安装大多使用C语言、C++C#

11、编程语言的分类:根据服务的重心不同,编程分为面向对象编程(侧重于数据的语言)和面向过程编程(侧重于指令的语言)。

12、编程语言的执行方式【一行一行】:顺序执行(没有逻辑关系)、循环执行(for(遍历,常用)、While(循环条件,True的时候开始循环)、unti(与while相反,False的时候开始循环))、选择    执行(分支if语句、case(重点使用))

13、Shell编程介绍:shell语句是面向过程的,侧重于指令集;

          shell语句的基本结构组成:命令、数据-变量、逻辑关系

          shell没有内嵌函数,没有函数库调用

            没调用的接口,这一点限制了shell脚本的功能,不像Python,可以调用各种模块接口

            优点:shell的功能更好的兼容系统,可以直接调用系统命令,执行效率更高

14、shell脚本的第一句话必须写(#!/bin/bash---->定义脚本解释器

15、/etc/shells----------查看当前系统支持的shell程序 

      echo $SHELL-----查看当前会话的shell程序  

      /etc/passwd----------制定了用户默认支持的shell程序(/bin/nologin 

16、shell的执行:1bash命令来执行脚本

              参数:-n  查看shell脚本的逻辑错误

                                  -x  逐行显示执行的脚本程序(方便排错)

                            2、授权 chmod +x filename,然后使用全路径执行脚本

17、变量:环境变量

        局部变量(declare 命令--定义变量类型)

        本地变量(local在函数中使用)

18、变量类型:数值型(浮点型(float),整型(int),布尔值(bollean01    falsetrue))

                字符串:(普通字符及字符串、数组)

19、编程语言的分类:根据变量的不同,分为强类型语言(数值必须被定义,才能进行处理或运算)和弱类型语言(编程语言可以自动识别变量类型)

20、多态:一个数据有多个属性,而最后使用的属性取决于和它进行运算的数据

21、传参:$?(上一条命令的执行状态,0代表正确,1-255表示错误)

        $1、$2${10}--------命令后面传入脚本的数据,以空格为分隔符

      $#(统计传参数量,${$#}统计传参量)

      $*(表示所有传参,所有的传参以字符串形式输出)

        $@(表示所有传参,所有的传参以列表形式输出)

                 

22、定义变量格式:NAME=VALUE(中间不要有空格,一个等号是赋值,两个等号是判断)

         通过命令declare定义变量参数(例:declare -i a=10 int() str()

          declare -a(代表数组)、declare -i (代表整数)

23、变量命名:下划线的方式指定变量名称

          驼峰命名方式

24、文件测试、条件判断:在脚本语言中,我们需要进行语句分支,就是要做判断,判断就是使用test命令来实现的。

使用格式:test [option] file

                   [  条件语句 ]

【不用test做判断时不加[  ]

常见的test选项:

比较选项:-eq(等于)、-ne(不等于)、-gt(大于)、-ge(大于等于)、-lt(小于)、-le(小于等于)【用于数值的判断,字符用=!=

判断选项:-f(判断是否为普通文件)、-d(判断是否为目录件)、-L(判断是否为链接文件)、-r-w-x(判断是否具有读 写 执行的权限))

关联选项:-o(或)、-a(与)、!(非)

字符串测试:=(等于为真)、!=(不等于为假)、-z(判断字符串是否存在,存在为真,不存在为假)、-n(与-z相反)

25、逻辑运算:与 && :两边的命令都为真时结果才为真

          或 || :两边的命令,一边为真就是真,同时为假才是假

        非 !

        [ $? -eq 0 ]  && exit 0 || exit 1

前后对接的都是单独的存在的命令,前后的两个命令没有任何关系只是通过逻辑运算符联系在一起

26、算数运算:let(1+1)    例:let a=1+1  echo $a 

               expr(1*1)   例:exor 1* 10

          $[]   [$1+10]   例:a=$[2+3]   echo $a   

       $(())   (($1/$2))      例:a=$((2/4))   echo $a

 27、编程练习

输出100以内的书的和

#!/bin/bash

declare -i i=0
declare -i sum=0
while [ $i -le 100 ] ;do
        sum+=$i
        let i=i+1
done

执行结果:

 

传参

#!/bin/bash
echo "$1 $2 ...${10}"
echo $?
echo $#
echo $*
echo $@
echo ${$#} 

执行结果:

 

判断文件是否存在

#!/bin/bash
#判断文件是否存在

if [ -f $1 -a -w $1 ];then
        echo "$1 文件不存在"
else
        echo "$1 文件已存在"
fi

 

判断用户类型

#!/bin/bash
#判断用户类型

declare -i uid=`uid -u $1`

if [ $uid -eq 0 ];then
        echo "这个是管理员"
else
   

执行结果:

 

Guess you like

Origin www.cnblogs.com/cqyyyyy/p/11347844.html