5imobi 发表于 2018-5-16 12:44:15

Linux shell常用知识

  1、shell 简介

  安装一定逻辑关系记录命令的文件,在此文件有执行权限的情况下可以用文件名称发起脚本的指向,shell是一种解释性语言,文件内记录的动作需要解释器shell。

  2、脚本的建立
  2.1 vim test.sh
  一般情况下脚本的结尾为".sh"这不是系统规定的,但是是业界的一种规范
  2.2 #bin/bash
      脚本开头的写法,这是脚本的使用的解释器,也就是脚本运行时的子shell
  2.3 脚本的内容
  脚本的内容是用命令和命令执行的逻辑关系组成

  2.4 脚本的执行方式
  方法一 :sh + 脚本的名称
  方法二 :chmod +x 脚本
  脚本名称的调用

  3、 脚本的编写规范
  1》开头应该添加脚本的说明信息
# vim /etc/vimrc
map <F6> ms:callAddTile()<cr>'s
function AddTile()
call append(0,&quot;#!/bin/bash&quot;)
call append(1,&quot;############&quot;)
call append(2,&quot;#Auther:feitian&quot;)
call append(3,&quot;#Email         :feitian@westos.com&quot;)
call append(4,&quot;#Version         :&quot;)
call append(5,&quot;#Description   :test scripts&quot;)
call append(6,&quot;#Create_Date   :&quot;.strftime(&quot;%Y-%m-%D&quot;))
call append(7,&quot;#############&quot;)
Endfunction  2》脚本中尽量不要使用中文,哪怕用拼音代替
  3》脚本中出现的()|[]|{}|<>等等成对出现的要一次打出,并且内容中的字符与这些符号之间要有空格
  4》脚本中的语句要一次写完在丰富内容
  5》语句中使用的动作要缩进写入,使脚本易读
  4. 在书写脚本过程中我们用到的命令
  4.1 diff命令
  diff命令是用来比较两个文件的不同,一般我们比较文件的内容是都是使用vimdiif,以为他看起来更清楚,高亮显示不同的行。但是diff可以用来打补丁

#a是添加的意思
#c是改变的意思
#d删除的意思
#num1,num2是第一个文件的内容
#num3,num4是第二个文件的内容  使用diff命令打补丁

diff -ufile1 file2 >file.path
#生成补丁文件
patch file1file.patch
#给file1打补丁,打完补丁之后,他就和file2相同
patch -b file1file.patch
#备份原文件为file.orgi  4.2 grep命令
-n
只显示过滤出来的那一行
-E
多层过滤
-n3
显示过滤出来的上下3行(共7行)
^test
以test开头的内容
-A3
显示过滤出来的前边4行(共4行)
test$
以test结尾的内容
-B3
显示过滤出来的后边4行(共4行)
&quot;\<test&quot;
以test开头的内容-i
对原文件就行修改
&quot;test\>&quot;
以test结尾的内容-v
除了包含过滤字符的内容
^$
空行
  4.3 cut命令
cut -c 1-4|1,4 #打印文件2每行的前4个字符
cut -d 分隔符 -f 要打印的域   file  一个关于cut和grep的简单脚本 :打印出系统eth0的ip,如过由两块网卡,则下面会介绍方法。

# vimip_print.sh
#!/bin/bash
############
#Auther       :feitian
#Email       :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/22/17
#############
IP=`ifconfig | grep &quot;inet\>&quot; |grep -v 127.0.0.1|cut -d &quot; &quot; -f 10`
NAME=`ifconfig |grep mtu |grep -v lo | cut -d ' ' -f 1`
echo &quot;$NAME$IP&quot;
# sh ip_print.sh
eth0:192.168.0.100  4.4 awk 命令
TEST=`############################`
awk-F分隔符 -vTEST=$TEST'BEGIN {print TEST} {print $2} END {print TEST} /etc/passwd
#可以使用特殊  一个简单的有关awk的脚本,还是刚才的问题,打印系统的网卡,这次就是多块网卡。
# vim ip_print.sh
#!/bin/bash
############
#Auther       :feitian
#Email       :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/22/17
#############
NAME=`cat /proc/net/dev |egrep &quot;Inter|face|lo&quot; -v|awk -F :'{print $1}'`
for Device_Name in $NAME
do
      ifconfig $Device_Name | grep &quot;inet\>&quot; |awk -F &quot; &quot;-v INTERFACE=$Device_Name 'BEGIN {print INTERFACE} { print $2}'
done
# sh ip_print.sh
eth0
192.168.0.100
eth1
192.168.1.100  4.5 使用不同底色和不同颜色的字体
  有关字体的颜色编号:
字体背景颜色
所对应的编号字体颜色
所对应的编号
40黑色
30黑色
41
深红色
31红色
42
绿色
32绿色43
×××
33×××44蓝色
34
蓝色
45
紫色
35
紫色
46
深绿色
36
深绿色
47
白色
37
白色
  图
  一个简单10秒倒计时输出结果的字体为红色

#!/bin/bash
############
#Auther       :feitian
#Email       :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/22/17
#############
for iin {10..1}
do
      echo -ne &quot;\033[31m$i \033[0m &quot;
      echo -ne &quot;\r    \r&quot;
      sleep 1
done  5. 脚本中的变量
  

  5.1 变量的命名规则
  变量中只能包含字母,数字和下滑线,他的写法一般为这3种 :TEST_REDAHAT,Test_Read和testReadhat.
  关于变量的一个简单脚本,其实在前边我们已经用到变量,其实就是参数传递的作用。这里我们写一个建立用户的脚本,给定一个文件,让你把这个文件中的用户建立出来
# vimuseradd.sh
#!/bin/bash
############
#Auther       :feitian
#Email       :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/22/17
#############
Max_Line=`wc -l /scripts/username|cut -c 1`
for Line_Number in `seq 1 $Max_Line`
do
    Username=`sed -n ${Line_Number}p/scripts/username`
    useradd $Username
done
# shuseradd.sh
# shuseradd.sh
useradd: user 'username1' already exists
useradd: user 'username2' already exists
useradd: user 'usernaem3' already exists  5.2变量的设定方式
  环境级变量:在当前环境生效,当前环境关闭,变量失效
# export A=1
# echo $A
1  用户级变量:只针对设置过的用户生效,其他用户无法使用
  在用户的家目录中修改,也就是文件/root/.bash_profile或者/student/.bash_profile
5.3 系统别名的设定方式
系统别名的设定也分为2个级别,他和系统变量的级别差不多,只是少一个环境级,他有用户级和系统级别名
用户级:在文件也是在用户的家目录下的.bashrc
# vim /root/.bashrc
aliasday='date'
# source/root/.bashrc
# day
Tue Aug 2221:29:40 EDT 2017系统级别名:系统级别名在/etc/bashrc,添加方式和系统级别名添加方式一样。
5.4 另一种变量的命名方式
  $1    执行脚本时代表的第一串字符串
  $2   同理,执行脚本中时代表的第二串字符串
  $*   执行脚本时后面跟的所有字符串
  $#    执行脚本时后面跟的字符串的个数
  $?    执行脚本成功时返回0,执行脚本失败时返回非零。
# vim test.sh
#!/bin/bash
############
#Auther       :feitian
#Email       :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/22/17
#############
echo '$1is '$1' '
echo '$1is '$2' '
echo '$*is '$*' '
echo '$#is '$#' '
# sh test.shlala hehe
$1 is lala
$1 is hehe
$* is lala hehe
$# is 2  注意:在重复两个单引号时可以抵消,好像是没有一样。
# echo $A
1
# echo'$A'
$A
# echo ''$A''
1  这里穿插二个简单的脚本,第一个判断一个网段的ip是不是通的
#!/bin/bash
############
#Auther         :feitian
#Email          :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/22/17
#############
echo &quot;please inputip that netmask is(255.255.255.0) &quot;
echo &quot;please input ip thatas 172.25.254&quot;
for i in {1..254}
do
   ping-w1 -c1 $1.$i&> /dev/null && echo&quot;$1.$i is up&quot; || echo &quot;$1.$i is down&quot;
done6. Shell脚本中函数的定义和变量的比较
使用test 可以用来作为变量的比较,在下面的脚本中有使用到 &quot;$WANT&quot; = &quot;C&quot; 判断WANT 的值等不等于C。
TEST() {
echotest
}
#函数体
TEST
#对函数的调用  这里穿插一个简单的脚本,就是判断一个网段的IP是不是通的。

#!/bin/bash
############
#Auther         :feitian
#Email          :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/22/17
#############
ACTION() {
       read -p &quot;please input a IP that it will be checked: &quot;IP
       ping-w1 -c1 $IP &>/dev/null && echo &quot;$IP is up&quot; || echo &quot;$IP is down&quot;
}
CHECK() {
       read -p &quot;what will you do? please inputQUIT(Q) or CHECK(C)&quot;CHECK
       WANT=`echo $CHECK|tr a-zA-Z`
       test &quot;$WANT&quot; = &quot;C&quot; && (
ACTION
CHECK)
       test &quot;$WANT&quot; == &quot;Q&quot; && exit 0
}
MAIN#这个也可以使用case语句
#!/bin/bash
############
#Auther         :feitian
#Email          :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/22/17
#############
ACTION() {
       read -p &quot;please input a IP that it will be checked: &quot;IP
       ping-w1 -c1 $IP &>/dev/null && echo &quot;$IP is up&quot; || echo &quot;$IP is down&quot;
}
CHECK() {
       read -p &quot;what will you do? please inputQUIT(Q) or CHECK(C)&quot;CHECK
       WANT=`echo $CHECK|tr a-zA-Z`
       case $WANT in C)
                ACTION
                CHECK
       ;;
                      Q)
                exit 0
       ;;
                      *)
               echo &quot;please inputQ or C&quot;
       ;;
       esac
}
CHECK7.变量的判断
  注意:这里举例均为正确的
# echo $A
1
# [ &quot;$A&quot;= &quot;1&quot; ]&& echo yes || echo no
yes
[ “1” =“1” ]
[ “2” >“1” ]
[ “1” <“2” ]
[ “1” !=“2”]
#注意在“[”和“]”和你的比较对象之间必须要有空格
[ “$A” -eq “1” ]               #1等于1
[ “$A” -ne “2” ]               #1不等于二
[ “2” -gt “$A” ]               #2大于1
[ “1” -ge “$A” ]            #1大于等于1
[ “$A” -lt “2” ]                  #1小于2
[ “$A” -le “1” ]            #1小于等于1
[ “$A” -eq “1” -a “2” lt “3” ]   #1等于1并且2小于3
[ “2” -eq “2” -o “1” -gt “2” ]      #2等于2或者1大于2,一个为真则为真。
[ -e /mnt/file ]                #判断/mnt/file是不是存在
[ -f /mnt/file ]         #判断/mnt/file是不是普通文件,如果不存在也是他也是返回非0
[ -x /mnt/file]          #判断/mnt/file是不是有执行权限,如果文件不存在也返回非0
[ -d /mnt/file]          #判断/mnt/file是不是一个目录,如果不是也返回非0
[ -z “helle”]          #判断是否为空
#这里有很多参数,比如-b -S -p等等,都可以判断,读者可以自己理解,在前边find命令时都有讲过,这里就不过多介绍了。8. Shell 脚本中的转义和注释
  \       #转义单个字符
  ‘’       #强引用
  “”       #若引用,其转义功能不能转义“!”“$” “\” “`”
  ${A}1      #输出为11
# echo $A
1
# echo ${A}1
11
# echo $A1
#这里输出为空,因为没有A1这个变量10.Sehll脚本中的四则运算
10.1四则运算符号
和c语言中的差不多,基本一样。
  ++   i++ ==>i+1      #自加1
  --   i-- ==>i-1      #自减1
  +=   i+=j ==>i=i+j
  -=   i-=j ==>i=i-j
  +      #加
  -      #减
  *      #乘
  /      #除
  %      #取余
  **      #幂预算


10.2运算的命令
# echo $
5
# let A=3+2
# echo $A
5
# echo `expr 3&quot;+&quot; 2`
5  #注意在expr作运算的时候,在“+”旁边必须有空格
11 .sehll 运算中的一些简单的语句
11.1 shell脚本中的for语句
这里写一个简单的例子,写一个简单的for语句的倒计时
#!/bin/bash
############
#Auther         :feitian
#Email          :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/23/17
#############
TIME=10
for((TIME;TIME>0;TIME--))
do
       echo-n&quot;After ${TIME}sisend&quot;
       echo -ne &quot;\r    \r&quot;
       sleep 1
done当你输入一个字符串,他会他会反着输出
#!/bin/bash
##############
#Auther         :feitian
#Email          :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/21/17
##############
read -p &quot;please iputalpha: &quot; ALPHA
Max=`echo -n $ALPHA |wc -m`
for ((Max;Max>0;Max--))
do
LALA=`echo -ne $ALPHA|cut -c $Max`
echo-n $LALA
done11.2 while 语句
这里使用while写一个简单的倒计时为1分10s的倒计时脚本
#!/bin/bash
############
#Auther         :feitian
#Email          :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date    :2017-08-08/23/17
#############
TIME=10
MIN=1
for((TIME;TIME>=0;TIME--))
do
      while [ &quot;$TIME&quot; -eq&quot;0&quot; -a &quot;$MIN&quot; -gt &quot;0&quot; ]
      do
                echo-n &quot;After $MIN:${TIME}isend &quot;
                echo-ne &quot;\r\r&quot;
                sleep 1
                ((MIN--))
                TIME=59
      done
      while [ &quot;$TIME&quot; -eq&quot;0&quot; -a &quot;$MIN&quot; -eq &quot;0&quot; ]
      do
                echo-ne &quot;\r\r&quot;
                clear
                echo &quot;Time Out!!&quot;
                exit 0
      done
      clear
      echo -n&quot; After $MIN:${TIME} isend &quot;
      echo -ne &quot;\r   \r&quot;
      sleep 1
done11.3 if 语句
这里也是用一个脚本来更好的体现
#!/bin/bash
############
#Auther         :feitian
#Email         :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date   :2017-08-08/23/17
#############
if
      [ -z $* ]
then
      echo &quot;please giveme a filename&quot;
elif
      [ -f $* ]
then
      echo &quot;$* is acommon file&quot;
elif
      [ -L $*]
then
      echo &quot;$* is alink&quot;
else
      echo $* is not exist
fi#这里就不在多写了,就是变量的判断和if语句的运用
# sh panduan.sh /mnt/file
/mnt/file is a common file
这里写一个进一步的脚本,给定两个文件,一个文件中是用户的名字,另一个文件是用户的密码,必须是一一对应。#!/bin/bash
############
#Auther         :feitian
#Email         :feitian@westos.com
#Version      :
#Description    :test scripts
#Create_Date   :2017-08-08/23/17
#############
CHECK() {
if   
      [ &quot;$#&quot; -ne&quot;2&quot; ]
then   
      echo &quot;echo pleasegive me usernemfile and userpasswdfile&quot;
elif   
      [&quot;$#&quot; -eq&quot;2&quot; ]
then   
      Max_Line1=`wc -l$1|awk -F &quot; &quot; '{print $1}'`
      Max_Line2=`wc -l$1|awk -F &quot; &quot; '{print $1}'`
      if
                [&quot;$Max_Line&quot; -ne &quot;$Max_Line2&quot; ]
      then
                echo &quot;Thefile usernemfile don't muth userpasswdfile,please check it&quot;
      else
                echo&quot;lala&quot;
      fi
fi
}
CHECK
页: [1]
查看完整版本: Linux shell常用知识