• Managing Test Files with create_files and delete_files Bash Scripts


    Introduction: In software development and testing, we often need to create and manage various test files. To automate this process and make it more efficient, we can use bash scripts. In this blog, we will introduce two simple bash scripts, create_files and delete_files, which help to create and delete test files respectively.

    Note: “\Code” and “\Test” are sibling directories, which means they are located at the same level in the directory structure. The script or code is executed from the “\Code” directory.

    Create_files Script

    The create_files script is designed to automate the process of creating test files with specific naming conventions. For every file in your directory that follows the naming pattern test.cmm, the script creates two new files named test_1.cmm and test_2.cmm, if they don’t already exist.

    Here is the content of the create_files script:

    #!/bin/bash
    
    for file in test*.cmm; do
      if [[ -f "$file" ]]; then
        filename=$(basename -- "$file")
        extension="${filename##*.}"
        filename_without_extension="${filename%.*}"
        num="${filename_without_extension#test}"
    
        new_file1="test${num}_1.cmm"
        new_file2="test${num}_2.cmm"
    
        if [[ ! -f "$new_file1" ]]; then
          touch "$new_file1"
          echo "Created $new_file1"
        fi
    
        if [[ ! -f "$new_file2" ]]; then
          touch "$new_file2"
          echo "Created $new_file2"
        fi
      fi
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    To use the create_files script, follow these steps:

    1. Save the above script to a file named create_files in your project directory.
    2. Give execute permissions to the script:
    chmod +x create_files
    
    • 1
    1. Run the script:
    ./create_files
    
    • 1

    在这里插入图片描述

    Delete_files Script

    The delete_files script is used to delete files that follow the naming pattern test_1.cmm and test_2.cmm, while keeping the original test.cmm files intact.

    Here is the content of the delete_files script:

    #!/bin/bash
    
    for file in test*_1.cmm test*_2.cmm; do
      if [[ -f "$file" ]]; then
        rm "$file"
        echo "Deleted $file"
      fi
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    To use the delete_files script, follow these steps:

    1. Save the above script to a file named delete_files in your project directory.
    2. Give execute permissions to the script:
    chmod +x delete_files
    
    • 1
    1. Run the script:
    ./delete_files
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    Kotlin源码编译报错,提示@UnsupportedAppUsage和@SystemApi声明的Java函数无法调用
    传奇服务端:GOM GeeM2引擎更新时必须要修改哪些地方?
    【Buildroot】工具包使用
    Android键盘监听
    四大组件---ContentResolver
    Postman进阶篇(十一)-在脚本中使用pm对象访问接口请求(pm.request.*)
    用“价值”的视角来看安全:《构建新型网络形态下的网络空间安全体系》
    SQL基础练习题(mysql)
    多级编号和目录
    JavaScript基础
  • 原文地址:https://blog.csdn.net/weixin_64123373/article/details/133934518