Chinaunix首页 | 论坛 | 博客
  • 博客访问: 210582
  • 博文数量: 55
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1126
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-22 15:15
个人简介

积累经验

文章分类

全部博文(55)

文章存档

2014年(55)

分类: 系统运维

2014-01-27 11:21:51

1.shell-解释器,是人和内核沟通的桥梁。
2.开源软件大都是GNU写的,Bash是shell的一种。
3.通配符
    touch file{1,2,3}
    touch file_{1,2,3}_{a,b,c}    9个文件
    mv file{,.bak}    加后缀file.bak
    *    匹配0或多个字符
    ?    匹配1个字符
    [1-4]    匹配范围内的任意1个字符
    [^1-4]    匹配除了范围的字符
    grep '^#' file   以#开头的行
    grep '/sbin/nologin$' /etc/passwd    以/sbin/nologin结尾的行
    grep 'r..' /etc/passwd    .表示占位符
    ""    弱引源
    ''    强引源    '!!'去除特殊字符的含义
    !!    执行上一次命令
    \    转义符
4.变量
1) 本地变量 (当前进程有效 ,子进程无效 bash命令开子进程)
    A=hello    定义变量A等于hello
    echo $A    $引用
    unset A    删除A变量
2) 环境变量 (当前进程有效,继承的子进程也有效)
    export A
    export A=hello
    比如:echo $PATH    
    4个文件1个目录可以设置环境变量
    # /etc/bashrc    存放本地变量及别名(全局设置)
    # ~/.bashrc    用户家目录,定义用户自己的本地变量 别名
    # ~/.bash_profile    存放全局变量
    # /etc/profile    存放全局变量
    #/etc/profile.d/    目录
    启动时4个文件1个目录读取顺序:(终端不同顺序不同)
    退出时执行~/.bash_logout
    tty终端:
    1 /etc/profile
        2 /etc/profile.d/xx.sh
    3 ~/.bash_profile
        4 ~/.bash.rc
    5 /etc/bashrc
    pts终端或su 用户(不加-)
    1 ~/.bashrc
    2 /etc/bashrc
    3 /etc/profile.d/xx.sh

练习:
建立一个copy命令,要求使用该命令复制时显示复制过程,并保留原文件的权限,且任何用户在登录后都能使用该命令。
# vi /etc/bashrc
    alias copy='cp -av'
# source /etc/bashrc    (使修改生效)

新建一个用户为harry,要求当该用户退出时,清空目录的内容 (不包括隐蔽文件)并且在家目录下创建backup隐蔽文件,文件内容为当前日期。
# useradd harry
# su - harry
# vi ~/.bash_logout
rm -rf /home/harry/*
echo $(date) > /home/harry/.backup

5.标准输入输出重定向及管道
   * 输出结果重定向,把正确或错误的内容放入指定文件,方便排错
    标准输出    stdin    键盘输入
    标准输出    stdout    屏幕输出正确结果    >或1>
    标准错误    stderr    屏幕输出错误结果    2>或>...2
    *正确和错误输出都重定向    &>

查找/etc/下面conf结尾的文件,正确输出放入find.r,错误输出放入find.e,不管正确错误都放入find.all
find /etc/ -name "*.conf" > /tmp/find.r 2> /tmp/find.e
find /etc/ -name "*.conf" &> /tmp/find.all

6.管道
    *把命令1的输出作为命令2的输入(连接多个命令),管道可以多个
    #head -n 20 /etc/passwd|tail -n 6    查看/etc/passwd 15-20行内容

基本命令:
    # rename .txt .bak /tmp    后缀txt改为bak
    # grep 'root' /etc/passwd --color    root特殊颜色显示
    # grep -C3 'root' /etc/passwd    上下3行内容
    # grep -B3 'root' /etc/passwd    上3行内容
    # grep -A3 'root' /etc/passwd    上3行内容
    # grep -v 'root' /etc/passwd     反向列出
    # grep -n 'root' /etc/passwd    标准行号
    
    # cut -d":" -f3 /etc/passwd    以:分割的第3列
    # cut -c2-5 /etc/passwd    第2-5字符列出

    # sort file -n    以数字从小到大排列
        -r    从大到小排列
        -t    分割符
    # sort -t":" -k3 -n /etc/passwd    已冒号为分割,第3列,已数字从小到大排列
    练习:cpu占有率前10
    # ps axo %mem,%cpu,pid,comm|sort -k2 -r|head -n11

    #uniq -c     计算相同的行数
    #wc -l    统计行数    -w统计单词个数    -m统计字符个数

------------------------------------------------------------------------------------------------
Bash脚本-脚本就是文件,里面都是命令(逻辑语法)再加上可执行权限
1.创建脚本    vim file.sh
    第一行    #!/bin/bash    库法字符(声明)
    第二行    # this is script user to……   注释
……    命令内容
2.创建完脚本,需要个脚本文件x权限。
3.执行文件
   . file.sh    (全局生效)
    source file.sh    (全局生效)
    bash    file.sh
    # .  变量 或 source 变量    (全局变量调用)
4.语句
1)for 格式:
    for 变量 in 值1 值2 
    do    
        内容
    done

练习:
for循环
for i in {1..100}=>for i in $(seq 1 100)
                        =>for i in $(seq 2 2 100)偶数
                        =>for i in $(seq 1 2 100)奇数

1-100不换行输出
#!/bin/bash
    for (( i=1 ; i<=100 ; i++ ))     //c语言写法
        do
            echo -n "$i “    //-n不换行
    done

vi for.sh
#!/bin/bash
#this is script user to for
1)for num in 1 2 3 
    do
        echo $num
    done
2)for num in {1..10}
    do    
        ping -c 2 192.168.0.$num &> /dev/null (ping2次,结果正确与否全部丢弃)
    done
------------------------------------------------------------------------------------------------- 
2)if格式:
    if 条件;then
        满足条件执行
    else
        不满足条件执行
    fi

练习:
#!/bin/bash
var=0
for num in {1..10}
do
   var=$(( $num+80 ))
    ping -c 2 192.168.0.$var &> /dev/null
    if [ $? -eq 0 ];then  (或  if test $? -eq 0;then)
        echo 192.168.0.$var is up
    else
    echo 192.168.0.$var is down
    fi
done

循环判断2个包是否安装
#!/bin/bash
for pkg in bind mysql
do
    if rpm -q $pkg > /dev/null
    then
        echo "$pkg install"
    else
        echo "$pkg not install"
    fi
done
-------------------------------------------------------------------------------------------------
3)case判断格式:
case 变量 in
        值1)
            执行xxxx
        ;;
        值2)
            执行xxxx
        ;;
        *)
            都不满足执行xxx
        ;;
esac

练习:
位置变量$1 (由键盘输入,传递到脚本里面)如:source case.sh 8
#!/bin/bash
case $1 in
    user)    
        echo kernal
    ;;
    kernel)
        echo user
    ;;
    *)
        echo hello
esac
-------------------------------------------------------------------------------------------------
4)while 条件(命令)
    do
        条件满足执行循环
    done

练习:
while无限循环
#!/bin/bash
while true
  do echo `date` >> /tmp/1
  sleep 4
done

循环读取/etc/hosts
#!/bin/bash
while read line
do
    echo $line
    sleep 2
done < /etc/hosts

-------------------------------------------------------------------------------------------------

5)continue    跳到下一个循环
      break    跳出循环
      exit    跳出循环 结束脚本
练习:
#!/bin/bash
for i in 1 4 0 9 0 4 0 7
do
    if [ $i -eq 0 ];then
        1.continue    (是0跳出循环,继续执行,结果非0数字+hello)
        2.break        (是0结束循环,结果 1 4+hello)
        3.exit        (是0结束循环 结束脚本,结果1 4)
    fi
    echo $i
done
echo hello
-------------------------------------------------------------------------------------------------

6)函数()    (编译函数)
    {
        内容
    }
    函数    (调用函数)

练习:
#!/bin/bash
ip=$(ifconfig eth0 |grep "inet addr:" |awk '{print $2}'|cut -c 6-)    //cut -c 6-(第6位开始取字符)
myecho()    
{
    echo $ip
}
myecho    
-------------------------------------------------------------------------------------------------

7)read用法:
read a    从键盘取值赋予变量a 
练习:
输入2个值比大小
#!/bin/bash
read -p "please int n1:" n1
read -p "please int n2:" n2
if [ $n1 -gt $n2 ];then
    echo $n1
else
    echo $n2
fi
------------------------------------------------------------------------------------------------------

5.if判断参数    test = [ ]    =>man test
if [ -d /tmp ]    判断目录已存在
if [ $a -eq $b ] 或 if test file1 -nt file2
-eq     等于
-gt     大于
-ge     大于等于
-le    小于等于
-lt    小于
-ne    不等于
-ef    文件有相同的设备和inode号
-nt    文件modification date时间 新
-ot   文件 旧

( )    引用命令执行结果 
$( )=` ` (返逗点)
$(( ))    变量运算
$(( 5+4 ))=$[ 5+4 ]
----------------------------------------------------------------------------------------------------
6.位置参数
1. $# 传递到脚本的参数个数
2. $* 以一个单字符串显示所有向脚本传递的参数变量。与位置变量不同,此选项参数可超过9个
3. $$ 脚本运行的当前进程ID号
4. $! 后台运行的最后一个进程的进程ID号
5. $@ 与$#相同,但是使用时加引号,并在引号中返回每个参数
6. $- 显示shell使用的当前选项,与set命令功能相同
7. $? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误
8. $()里面放命令,可以在脚本中执行该命令,并返回命令执行的输出.


*shift    位置参数左移
vi shfit.sh
#!/bin/bash
while [ $# -gt 0 ]
do
    echo $*
    shift    
    read        //需要键盘输入回车
done

# bash shfit.sh 11 22 33 44 55 66 回车

vi run.sh
#!/bin/bash
for i in $*
    do 
        echo $*
done


----------------------------------------------------------------------------------------------------

练习:
用户的id从1001-1030,即user1的uid是1001,以此类推
添加1-30用户(家目录/rhome/user)用户密码passwd
user1-15,shell为/bin/bash
user16-30,shell为/sbin/nologin
用户Id从1001-1030
分析:
1. mkdir /rhome
2.useradd -u 1001 -d /rhome/user1 -s /big/bash user1
3.echo passwd|passwd --stdin user1
vi adduser.sh
#!/bin/bash
if [ ! -d /rhome ];then  (或if test ! -d /rhome;then)   判断目录是否存在 !表示没有存在    man test
    mkdir /rhome
fi
for n in {1..30}
do
    n_uid=$(( $n+1000 ))
    if [ $n -le 15 ];then
        useradd -u $n_uid -d /rhome/user$n -s /bin/bash user$n
    else 
        useradd -u $n_uid -d /rhome/user$n -s /sbin/nologin user$n
    fi
    echo passwd | passwd --stdin user$n
done
-------------------------------------------------------------------------------------------------

运维脚本
vi myhttd.sh
#!/bin/bash
start()
{
  echo "start ok"
}

stop()
{
  echo "stop ok"
}

c_s()
{
  echo "creat $1 ok"
}

d_s()
{
  echo "delete $1 ok"
}

help()
{
cat<
usage:$0[option]
option
-on:"start on"
-off:"sotp off"
-c:"create server"
-d:"delete server"
EDNF
}

while [ $# -gt 0 ]
do
  case $1 in
        -on)
          start
          shift
        ;;
        -off)
          stop
          shift
        ;;
        -c)
-on:"start on"
-off:"sotp off"
-c:"create server"
-d:"delete server"
EDNF
}


while [ $# -gt 0 ]
do
  case $1 in
        -on)
          start
          shift
        ;;
        -off)
          stop
          shift
        ;;
        -c)
          c_s$2
          shift;shift;
        ;;
        -d)
          d_s$2
          shift;shift;
        ;;
        *)
          help
          exit
        ;;
        esac
done
-------------------------------------------------------------------------------------------------------------


    

    
    
阅读(1412) | 评论(0) | 转发(0) |
0

上一篇:vi常用命令

下一篇:Linux 计划任务

给主人留下些什么吧!~~