Linux learning 41 yum advanced combat and combat shell scripts for disk management

I. Overview

  1, the desktop environment:

    Windows7, OpenSUSE 13.2, Kubuntu (KDE)

  2, yum install Review

    a, yum Package Manager

      C/S:

        yum client(yum)

        yum repository(ftp/http/https)

        

        

        base: the main package

        extras: additional bag

        updates: Upgrade Package

 

      repo definitions

        [id]

        name=

        baseurl=http://

            http://

    b, sub-command

      list,clean,makecache,grouplist,whatprovides

      install,update,remove,groupinstall,groupupdate,groupremove,groupinfo

      yum install /usr/local/src/testapp-3.3.1-1.el7.x86_64.rpm # rpm package install this manner, when the install package relies rpm for other packages, if present, his warehouse yum resolve dependencies automatically install the appropriate dependencies. However, if you install the rpm package rpm -ivh by then it will not be resolved automatically.

  3, compile and install Review

    a、C/C++:

      ./configure --> Makefile.in ==> makefile

      make + makefile ==> binary,library,configfile,manual

      make install

Two, bash scripting

  1, procedural programming language implementation process

    Order execution

    Choose Execute

    Cycle execution

  2, choose to perform:

    (1)、&&,||

    (2), if the statement

    (3), case statements

  3, if the statement: three formats -

    a, a single branch of the if statement

      if CONDITION;then

        if-true- branch

      be

    b, two-branch if statement

      if CONDITIOON;then

        if-true- branch

      else

        if-false- branch

      be

    c, multi-branch if statement

      if CONDITION1;then

        A branch condition is true

      elif CONDITION2;then

        2 branch condition is true

      elif CONDITION3;then

        Condition 3 is true branch

      ...

      elif CONDITIONn;then

        N branch condition is true

      else

        Branch when all the conditions are not satisfied

      be

      Note: Even if a number of conditions may also be able to meet, the branch will only execute one of the first test is "true."

      Example: a script parameters passed to the script file path, the file type is determined.

[root@localhost script]# cat filetype.sh 
#!/bin/bash
if [ $# -lt 1 ];then
    echo "At least one path"
    exit 1
fi

if ! [ -e "$1" ];then
    echo "No such file"
    exit 2
fi

IF [-f $ . 1 ]; the then 
    echo  " the Common File " 
elif [-d $ . 1 ]; the then 
    echo  " Directory " 
elif [-L $ . 1 ]; the then # is a symbolic link is present and
     echo  " Symbolic Link "  
elif [ $ -b 1 ]; the then 
    echo  " Block Special File " 
elif [-c $ 1 ]; the then 
    echo  " Character Special File " 
elif [$ -S1 ];then 
    echo "Socket file"
else
    echo "Unkown"

fi
[root@localhost script]# bash -n filetype.sh 
[root@localhost script]# bash -x filetype.sh /etc/
+ '[' 1 -lt 1 ']'
+ '[' -e /etc/ ']'
+ '[' -f /etc/ ']'
+ '[' -d /etc/ ']'
+ echo Directory
Directory

    d, Note: if statement nestable

    e, exercise: write a script

      (1), passing a parameter to the script, this parameter is the user name

      (2), according to its ID number to determine the type of user

        0: Administrator

        1-999: User

        1000+: login

[root@localhost script]# cat usertype.sh 
#!/bin/bash
[ $# -lt 1 ] && echo "At least on user name." && exit 1

! id $1 &> /dev/null && echo "No such user." && exit 2

userid = $ ( a $ u 1 )

if [ $userid -eq 0 ];then
    echo "root"
elif [ $userid -ge 1000 ];then
    echo "login user."
else
    echo "System user."
fi
[root@localhost script]# bash -n usertype.sh 
[root@localhost script]# bash -x usertype.sh 
+ '[' 0 -lt 1 ']'
+ echo 'At least on user name.'
At least on user name.
+ exit 1
[root@localhost script]# bash -x usertype.sh root
+ '[' 1 -lt 1 ']'
+ id root
++ id -u root
+ userid=0
+ '[' 0 -eq 0 ']'
+ echo root
root

    f, exercise: write a script

      (1), a menu to the user as listed below

          disk)show disks info

          mem) show memory info

          cpu) show cpu info

            cat / proc / cpuinfo or can be ordered lscpu

          *) quit

      (2), prompt the user for their own choice, then the system displays the corresponding information selected corresponding to its

[root@localhost script]# cat sysinfo.sh 
#!/bin/bash
cat <<EOF
disk) show disks info
mem) show memory info
cpu) show cpu info
*) QUIT
EOF
read -p "Your choice: " option
if [[ "$option" == "disk" ]];then
    fdisk -l /dev/[sh]d[a-z]
elif [[ "$option" == "mem" ]];then 
    free -m
elif [[ "$option" == "cpu" ]];then
    lscpu
else
    echo "Unkown option."
    exit3
fi
[root@localhost script]# bash sysinfo.sh 
disk) show disks info
mem) show memory info
cpu) show cpu info
*) QUIT
Your choice: mem    
              total        used        free      shared  buff/cache   available
Mem:           1991         145        1031           9         815        1606
Swap:          2047           0        2047
[root@localhost script]# 

Third, the cycle execution

  1, the piece of code or 0,1 repeatedly performed a plurality of times

    Entry conditions: when the conditions are met to enter circulation

    Exit conditions: each cycle should have exit conditions to have the opportunity to exit the loop;

  2, bash script in three ways

    for loop

    while loop

    until loop

  3, for circulation

    a, two formats

      (1), traverse the list

        Generate a list of ways:

          1) given directly

          2), a list of integers

            i、{start..end}

            ii、seq   

              seq [OPTION] ... LAST: seq 3 will represent such three outputs 1,2,3 Number

              seq [OPTION] ... FIRST LAST: seq 1 5 such as outputs 1 through says. 5
              SEQ [the OPTION] ... FIRST LAST the INCREMENT: seq 1 2 11 example it means 1 to 11 in steps of 2 outputs, that is, each plus 2 output

[root@localhost script]# seq 1 2 11
1
3
5
7
9
11

          3) returns a list of commands, such as ls / var or cat / etc / issue, etc., corresponding to the return value is a list of

          4), glob, such as ls / etc / p *

          5), variable references

            $@,$*

      (2), the control variable

    b, traverse the list

      for VARAIBLE in LIST;do

        Loop

      done

      Entry conditions: As long as the list of available elements, you can enter the circulation.

      Exit condition: the list of elements to traverse completed.

      Example 1: Add users in bulk

[root@localhost script]# cat useradd3.sh 
#!/bin/bash
for username in user21 user22 user23;do
    useradd $username
done

[root@localhost script]# bash -x useradd3.sh 
+ for username in user21 user22 user23
+ useradd user21
+ for username in user21 user22 user23
+ useradd user22
+ for username in user21 user22 user23
+ useradd user23
[root@localhost script]# 
[root@localhost script]# cat useradd3.sh 
#!/bin/bash
for username in user21 user22 user23;do
    if id $username &> /dev/null;then
        echo "$username exists."    
    else
        useradd $username && echo "Add user $username finished." 
    fi
done

      Example 2: Add 10 temporary file in / tmp

[root@localhost script]# cat addfile.sh 
#!/bin/bash
for filename in $(seq 1 10);do
    touch /tmp/f$filename
done
[root@localhost script]# bash -x addfile.sh 
++ seq 1 10
+ for filename in '$(seq 1 10)'
+ touch /tmp/f1
+ for filename in '$(seq 1 10)'
+ touch /tmp/f2
+ for filename in '$(seq 1 10)'
+ touch /tmp/f3
+ for filename in '$(seq 1 10)'
+ touch /tmp/f4
+ for filename in '$(seq 1 10)'
+ touch /tmp/f5
+ for filename in '$(seq 1 10)'
+ touch /tmp/f6
+ for filename in '$(seq 1 10)'
+ touch /tmp/f7
+ for filename in '$(seq 1 10)'
+ touch /tmp/f8
+ for filename in '$(seq 1 10)'
+ touch /tmp/f9
+ for filename in '$(seq 1 10)'
+ touch /tmp/f10

      Example 3: Find all positive integers less than 100 and

[root@localhost script]# cat sum2.sh 
#!/bin/bash
declare -i sum=0
for i in {1..100};do 
    sum=$[ $sum+$i ]
done
echo $sum
[root@localhost script]# bash sum2.sh 
5050

      A content type for each file in the judgment / var / log: Example 4

[root@localhost script]# cat filetype2.sh 
#!/bin/bash
for filename in /var/log/*;do
if [ -f $filename ];then
    echo "Common file"
elif [ -d $filename ];then
    echo "Directory"
elif [-L $ filename]; then # is a symbolic link exists and
    echo "Symbolic link" 
elif [-b $filename ];then
    echo "block special file"
elif [ -c $filename ];then
    echo "character special file"
elif [ -S $filename ];then 
    echo "Socket file"
else
    echo "Unkown"

be
done

Fourth, practice

  1, respectively, all even find the sum of less than 100, and the sum of all odd

  2, all calculated on the user id of the current system and

  3, passed through a directory script parameters to the script, and then calculate the sum of all the text files in this directory and lines, and shows the total number of such files

 

Guess you like

Origin www.cnblogs.com/Presley-lpc/p/12370025.html