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

相关推荐
小何好运暴富开心幸福3 天前
C++之日期类的实现
开发语言·c++·git·bash
Maple_land4 天前
Linux进程第五讲:PPID与bash的关联、fork系统调用的原理与实践操作(上)
linux·运维·服务器·centos·bash
___波子 Pro Max.4 天前
Shell脚本变量${}与命令$()替换区别
bash
九天轩辕6 天前
用一个 Bash CLI 管理多款 AI 开发工具:jt-code-cli 实战与原理解析
开发语言·人工智能·bash
许商8 天前
【stm32】bash自动配置buildenv
开发语言·bash
zhaotiannuo_19988 天前
linux centos 7 解决终端提示符出现-bash-4.2的问题
linux·centos·bash
vortex59 天前
Bash 中的 shopt -s globstar:递归 Glob 模式详解
开发语言·chrome·bash
一勺菠萝丶9 天前
macOS 终端配置全攻略:zsh、bash_profile、zprofile、zshrc 到 nvm 配置的完整科普
macos·bash·策略模式
NiKo_W10 天前
Linux 自定义shell命令解释器
linux·bash·shell
爱倒腾的老唐11 天前
02、命令行的介绍
linux·bash