zoukankan      html  css  js  c++  java
  • linux的shell脚本

    linux的shell脚本


    第一行的#!是说明文件的类型的。
    第一行的/bin/bash表明该文件是一个bash程序,需要bash程序来解释执行。
    编辑内容中使用#号进行注释。

    执行shell文件:
    方法一:bash hello.sh
    方法二:sh hello.sh (sh是指向bash的一个链接)
    方法三:将文件修改为可执行的文件,然后运行它:
    chmod a+x hello.sh
    ./hello.sh


    输出重定向
    ls > 1.txt
    ls >> 1.txt
    两个>表示不清除原来的内容,以追加的形式输出。

    bash中将标准输出表示为1。
    将标准错误输出表示为2。
    find /home -name *.txt 2> err_log将错误写入到err_log中
    find /home -name *.txt log 2>& 1 首先将错误写入到log中,
    再将标准输出也写入
    简写为 find /home -name *.txt >& log

    shell中参数的传递使用$加数字。
    注意$0是文件名字,其他变量从数字1开始。
    创建一个hello.sh文件:
    #!/bin/bash
    echo "$0=" $0

    echo "$1=" $1
    echo '$2=' $2

    执行:sh hello.sh param1 param2。
    结果显示:
    hello.sh= hello.sh
    param1= param1
    $2= param2
    (双引号还是会解析成变量值。)

    示例:随着日期变化,每天备份数据库,文件名为每天的日期。
    利用date指令获取所需的文件名:

    #!/bin/bash
    
    #接收输入的文件名
    read -p "请输入文件名:" input_filename
    
    #设置文件名,如果输入的有文件名,就使用,否则使用默认 
    filename=${input_filename:-"default_name"}
    
    #获取今天的日期
    today_date=$(date +%Y%m%d)
    
    #创建文件
    touch "${filename}_${today_date}.txt"
  • 相关阅读:
    AOP在Spring Boot中如何使用
    拦截器在Spring Boot中如何使用
    跨域在Spring Boot中如何处理
    @ControllerAdvice
    文件上传之Spring Boot整合web层
    Git和GitHub
    Spring Boot 整合web层之JSON,gson,fastjson的使用
    Spring boot整合视图层
    Spring Boot中的parent是什么?
    网页自动化,验证码识别函数,深度学习训练
  • 原文地址:https://www.cnblogs.com/gyfluck/p/9761706.html
Copyright © 2011-2022 走看看