zoukankan      html  css  js  c++  java
  • 【CentOS】又是一篇Shell

    一、Shell

    1.Shell脚本的格式

      #!/bin/bash 或者是 #!/bin/sh 开头 ,这是用于解析脚本的工具

    2.执行脚本的方法

      (1)bash filename 或者是sh filename

      (2)chmod a+x filename.sh  因为默认没有执行权限

      (3)sh -x filename.sh 这里的-x相当于是debug

    3.Shell脚本的逻辑

      if [判断语句] ; then     |      if [判断语句] ; then        |  case  变量  in

           command            |        command                  |   value1)

      fi                             |      elfi [判断语句]; then       |            command

                                     |        command                  |            ;;

                                     |      else                            |   value2)

                                     |      fi                                |             command

                                     |                                        |            ;;

                                     |                                        |   easc

    其中,shell 脚本中if还经常判断关于档案属性,比如判断是普通文件还是目录,判断文件是否有读写执行权限等。常用的也就几个选项:

    -e :判断文件或目录是否存在

    -d :判断是不是目录,并是否存在

    -f :判断是否是普通文件,并存在

    -r :判断文档是否有读权限

    -w :判断是否有写权限

    -x :判断是否可执行

    -z :如果字符串为空,返回0(true).如 if [-z $temp]

    -n :如果字符串为非空,返回0(true)

    例如 if [-d filename]  ;then 

    就可以判断这个filename是否是一个目录,并是否存在

    在判断数值大小除了可以用 (( )) 的形式外,还可以使用 [ ] 但是就不能使用>, < , = 这样的符号了,要使用 -lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)。下面阿铭就以命令行的形式简单比较一下,不再写shell脚本了

    tips:":"在判断的时候表示true , -a 表示为and ,-o表示为or

    判断拓展:

      if echo $n1|grep -q '[^0-9]'

    4.Shell脚本中的循环

      for  变量名  in  循环条件 ;do     |      while 条件 ;

            command                       |            command

      done                                    |       done

      注意,对于 for temp in 'seq 1 1 10'来说 ,中间的1为步长,如果要倒叙输出如10.9.8...的话,把命令改为for temp in '10 -1 1' 

    5.函数

     注意区分break、continue和exit0的区别,前者是退出循环,中者为退出当前循环进入下一次循环,后者是退出脚本

    二、date命令

      date +%Y  四位年份

             +%y   两位年份

             +%m  月份

             +%d   日

             +%H   时

             +%M   分

             +%S   秒

             +%w   星期几,如果是星期天显示7、

             +%W  week number of year, with Monday as first day of week 

             +%s    时间错 

             +%F   full date, the same as %Y%m%d

             +%T   time,the same as %H%M%S

      date -d 用法:参考这篇博文,http://blog.chinaunix.net/uid-9370128-id-271932.html

      date -s "2013-11-14 00:00:00" 设置时间

  • 相关阅读:
    扫面线模板
    (动态规划、栈)leetcode 84. Largest Rectangle in Histogram, 85. Maximal Rectangle
    tmux 常见命令汇总
    leetcode 221
    leetcode 319 29
    (贪心)leetcode 392. Is Subsequence, 771. Jewels and Stones, 463. Island Perimeter
    leetcode 982 668
    Python import 同文件夹下的py文件的函数,pycharm报错
    Windows里Anaconda-Navigator无法打开的解决方案
    Windows下 gpu版 Tensorflow 安装
  • 原文地址:https://www.cnblogs.com/ImJerryChan/p/6056928.html
Copyright © 2011-2022 走看看