zoukankan      html  css  js  c++  java
  • Shell

    ----------------------------Shell相关知识---------------------------------------

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

    Shell

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

    ----'文件'

    1.'全局(所有用户)环境'
    /etc/profile
    /etc/bashrc

    2.'为当前用户环境‘
    ~/.bash_profile
    ~/.bashrc


    ----'chsh':改变登录shell后,需要重新登录

    1.chsh -s /bin/ksh //改变当前用户的shell,/bin/下的脚本必须在shells中列出,才可作为登录脚本
    //usermod -s /bin/ksh Jon {只能由root执行}

    2.chsh -l //列出/etc/shells中的脚本,该文件中的脚本都存在/bin/下

    -----'test': man test //真:0;假:!0

    //---验证文件file

    1.test -r | -w | -x file //验证file是否可 r/w/x时
    --w-r--r-x. 1 student root file //如果此时effective user 为普通用户[student] && student拥有file则,此时验证的是 u=?r/w/x;与g=?r/w/x o=?r/w/x 无关
    //如果此时------------------harry,-----------------------------=, 此时验证的是 o=?r/w/x;
    //如果此时-------------------root----,---------- u/g/o =? r/w/x;即u/g/o至少一个拥有r/w/x即为真:0
    //---验证String

    1.str="abc" //变量赋值:【=两边没有空格】&& 【推荐用双引号->括注字符串】。
    2.test "$str" = "abc" //真0:引用字符串变量:$str.【----必须空格】
    3.test '$str' = 'abc' //假1:『单引号中运算符失效』

    //---验证数字

    1.str=100 //shell是若类型脚本语言;变量赋值时,不去别类型。str=100/str="100" 既可以当String,也可以当number使用
    2.test $str -eq 100 //真0:『推荐数字比较,不用引号』
    3.test "$str" = "100" //真0:体现若类型

    -----'misc'

    1执行一个文件: ./if.sh . if.sh source if.sh bash if.sh //三种方式

    ------'将命令添加到$PATH'-->即可直接运行 if.sh

    a>或将if.sh所在路径添加到 $PATH中,

    b>或将if.sh复制到/bin/ 或 /usr/bin/等已有的命令搜素路径下,即可直接运行 if.sh

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

    '2'echo $SHELL //查看当前shell类型

    '3'[ -x if.sh ] && ls //第一个命令返回真,才执行第二个 『A类写法:逻辑判断中的 && ||』
    [ -d /etc/ ] || ls //第一个返回假,才。。。。。。。

    [ -x if.sh -a -d /etc/ ] //-a:and 『B类写法:test命令 选项 -a -o』
    [ -x if.sh -o -d /etc/ ] //-o:or
    『A/B功能等价』

    [jacky@localhost MyShell]$ [ ! -x if.sh ] //!:非
    [jacky@localhost MyShell]$ echo $? //显示前驱命令返回值
    1

    '4'-- command > /dev/null 2>&1 //将stdout && stderr 重定向到 /dev/null,即将产生的所有信息丢弃。
    -- command &> /dev/null

    ****************************************************
    in UNIX //01,2被称作文件描述符(file descriptor)
    0 = stdin
    1 = stdout
    2 = stderr

    command [1]> /dev/null //将command的stdout重定向到 /dev/nullnull device)
    command 2> /dev/null //将command的stderr从定向到 /dev/null

    Everything in Linux is a file, including I/O.
    There are three standard file descriptors,
    Standard In (STDIN, file descriptor 0),
    Standard Output (STDOUT, file descriptor 1)
    Standard Error (STDERR, file descriptor 2).

    *****************************************************


    为什么要用 /dev/null 2>&1 这样的写法.这条命令的意思是将标准输出和错误输出全部重定向到/dev/null中,也就是.
    下面我就为大家来说一下, command > file 2>file 与command > file 2>&1 有什么不同的地方.

    1.'command > file 2>file' 的意思是将命令所产生的标准输出信息,和错误的输出信息送到file 中.
    command > file 2>file 这样的写法,stdout和stderr都直接送到file中, 'file会被打开两次,'
    这样stdout和stderr会互相覆盖,这样写相当使用了FD1和FD2两个同时去抢占file 的管道.

    2.'command >file 2>&1' 这条命令就将stdout直接送向file, stderr 继承了FD1管道后,再被送往file,
    此时,file 只被打开了一次,'也只使用了一个管道FD1',它包括了stdout和stderr的内容.

    '注':/dev/null是 Unix里的【无底洞】
    你不想看到 output 或者output 太多太大了,有可能把硬碟给挤爆了的时候,程序的设计就会考虑把 output 送到 /dev/null
    cash.sh
    #!/bin/bash
    #test
    echo "do u like linux?"
    read ans
    case $ans in
    y|Y|yes|YES)
    echo "Yes,I do"
    ;;
    n|N|no|NO)
    echo "No,I don't"
    ;;
    *)
    echo "ur ans is " $ans
    ;;
    esac
    while.sh
    #!/bin/bash
    #while.sh
    loop=0
    while [ $loop -ne 5 ]
    do
    let loop=loop+1
    echo "loop = $loop"
    done
    for.sh
    #!/bin/bash
    #for test
    function for_first()
    {
    for os in Linux Window Mac
    do
    echo "Operating System is :$os"
    done
    }

    function for_second()
    {
    for name
    do
    echo "I'm $name"
    done
    }

    function main()
    {
    for_first
    for_second $1 $2 $3
    }

    main $@
    if.sh
    #!/bin/bash
    #judge the file type
    function file_type()
    {
    if [ -d $path_name ]
    then
    {
    echo "it's a dir :)"
    permission
    }
    elif [ -f $path_name ]
    then
    {
    echo "it's a file :("
    permission
    }
    else
    echo "it's not either a file or a dir!!"
    fi
    }
    #permission judgement
    function permission()
    {
    if [ -r $path_name ]
    then
    echo "it's readable"
    fi

    if [ -w $path_name ]
    then
    echo "it's writable"
    fi

    if [ -x $path_name ]
    then
    echo "it's executable"
    fi
    }

    function main()
    {
    echo "enter a path or file name"
    read path_name
    file_type

    }
    echo "start-----------"
    main
    echo "end------------"
    until.sh
    #!/bin/bash
    #until.sh
    loop=0
    until [ $loop -eq 5 ]
    do
    let loop=$loop+1
    echo "loop= $loop"
    done
    ping.sh
    #!/bin/bash
    #ping 指定的主机
    for n in {139..148}
    do
    #变量赋值不能有空格
    host=192.168.0.$n

    #将ping的stdout/stderr定向到黑洞
    ping -c2 $host &> /dev/null

    #如果ping通,返回0;否则,返回1
    if [ $? -eq 0 ]
    then
    echo "$host is up"
    else
    echo "$host is down"
    fi
    done
    function.sh
    #!/bin/bash
    #function.sh
    function func()
    {
    echo "Your command is :$0 $*"
    echo "Number of Param(\$#) is :$#"
    echo "Script file name(\$0) is :$0"
    echo "Params(\$*) are :$*"
    echo "Params(\$@) are :$@"
    count=1
    for param in $@
    do
    echo "Params(\$$count) is:$param"
    let count=$count+1
    done
    }
    echo "START-----"
    func $@
    echo "END------"
    example.sh
    #!/bin/bash
    #script of backup files:backup files or dirs to BACKUP_DIR

    #LOG_START_TIME=`date`
    BACKUP_DIR=~/backup
    BACKUP_LOG="$BACKUP_DIR/backup.log"

    function create_log_file()
    {
    if [ ! -e $BACKUP_DIR ]
    then
    mkdir $BACKUP_DIR
    fi
    if [ -e $BACKUP_LOG ]
    then
    rm -f $BACKUP_LOG
    fi
    touch $BACKUP_LOG
    }

    function backup_file()
    {
    cp -fr $1 $BACKUP_DIR>/dev/null 2>&1
    write_log $? $1
    }

    function write_log()
    {
    # log_time=`date`
    backup_file_name=$2
    err_msg="log_time ERROR in backup file/directory($backup_file_name)"
    suc_msg="log_time SUCCESS in backup file/directory($backup_file_name)"

    if [ $1 -eq 0 ]
    then
    echo $suc_msg
    echo $suc_msg >> $BACKUP_LOG
    else
    echo $err_msg
    echo $err_msg >> $BACKUP_LOG
    fi
    }

    echo "START-----------"
    create_log_file
    for file in $@
    do
    backup_file $file
    done
    echo "END------------"










    我在IBM工作,可以为大家内部推荐IBM各种职位 IBM全球职位尽在以下链接(请在浏览器中打开,QQ/微信 会阻止): http://ibmreferrals.com/ 很乐意为感兴趣的小伙伴分享:我的面试经验^_^ 如需咨询,请邮件发送以下邮箱,有问必回 1026096425@qq.com
  • 相关阅读:
    BZOJ 2034 【2009国家集训队】 最大收益
    vijos P1780 【NOIP2012】 开车旅行
    BZOJ 2115 【WC2011】 Xor
    BZOJ 3631 【JLOI2014】 松鼠的新家
    BZOJ 4717 改装
    BZOJ 2957 楼房重建
    BZOJ 4034 【HAOI2015】 T2
    BZOJ 1834 【ZJOI2010】 network 网络扩容
    BZOJ 2440 【中山市选2011】 完全平方数
    BZOJ 2733 【HNOI2012】 永无乡
  • 原文地址:https://www.cnblogs.com/jackydalong/p/2408848.html
Copyright © 2011-2022 走看看