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

相关推荐
木风小助理1 天前
`mapfile`命令详解:Bash中高效的文本至数组转换工具
开发语言·chrome·bash
liliangcsdn3 天前
bash中awk如何切分输出
开发语言·bash
ASEpochs4 天前
Vsocde中‘sh’不是内部或外部命令,也不是可运行的程序或批量处理文件--已解决
git·vscode·bash
叠叠乐4 天前
bash sh为什么终端不能tab补全
开发语言·bash
Ken_11154 天前
CentOS7升级GNU Bash
bash
我是koten7 天前
K8s启动pod失败,日志报非法的Jar包排查思路(Invalid or corrupt jarfile /app/xxxx,jar)
java·docker·容器·kubernetes·bash·jar·shell
西京刀客8 天前
Bash 脚本中的 ((i++)) || true 表达式详解( set -e 表达式陷阱)
bash·set·表达式
xuchaoxin13758 天前
Bash 重定向完全指南
bash·重定向
sz66cm11 天前
Linux基础 -- xargs 结合 `bash -lc` 参数传递映射规则笔记
linux·笔记·bash
Irene199112 天前
Bash、PowerShell 常见操作总结
bash·powershell