Managing Test Files with create_files and delete_files Bash Scripts

Table of contents

  • [Create_files Script](#Create_files Script)
  • [Delete_files Script](#Delete_files Script)

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<num>.cmm, the script creates two new files named test<num>_1.cmm and test<num>_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

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

  3. Run the script:

    ./create_files

Delete_files Script

The delete_files script is used to delete files that follow the naming pattern test<num>_1.cmm and test<num>_2.cmm, while keeping the original test<num>.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

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

  3. Run the script:

    ./delete_files

相关推荐
大魔王爱学习5 天前
终端bash&zsh中实现基于前缀的历史命令搜索
linux·bash
酷可达拉斯6 天前
Linux操作系统-磁盘空间使用率100%如何处理?
linux·运维·服务器·云计算·bash
Nuanyt8 天前
Linux系统的 Shell 常见指令和常见知识点(以bash为主)
linux·运维·chrome·bash
小肝一下17 天前
7. 库制作与原理
linux·c语言·操作系统·bash·shell·动静态库
德福危险18 天前
从目录枚举到bash破壳漏洞、从流量抓包到组权限提权:靶机练习之symfonos3
开发语言·bash
至乐活着24 天前
Shell脚本编写最佳实践:打造健壮、可维护的自动化利器
自动化·bash·shell·脚本·最佳实践
风向决定发型丶1 个月前
Shell中的特殊变量
linux·运维·bash
tiana_1 个月前
写了个零依赖的 Go 版本管理器,curl | bash 完事
开发语言·golang·bash
无足鸟ICT1 个月前
【RHCA+】bash命令
linux·开发语言·bash
甄同学1 个月前
第十七篇:Bash Executor命令执行器,安全运行Shell命令
开发语言·安全·bash