当前位置: 首页 > news >正文

git,bash - 例子整理

文章目录

    • git,bash - 例子整理
    • 概述
    • 笔记
    • 遍历目录,找到目标文件后干活
    • 备份一个文件
    • END

git,bash - 例子整理

概述

在git bash中的脚本和linux bash中好像差不多。
整理一些例子,为以后做参考

笔记

遍历目录,找到目标文件后干活

#!/bin/bash
# git bash 脚本 - 遍历修改当前目录下得所有 .gitmodules , 替换https 库 url 到 ssh 库 url

# 前置操作 : 写好my_task.sh后,给执行权限
# chmod 777 ./my_task.sh

# 前置操作 : 检查脚本的语法是否正确?
# bash -n my_task.sh
# 如果没看到输出,就说明脚本语法是正确的

# 运行脚本
# 进入到脚本的目录
# ./my_task.sh

clear
echo "---------- my_task.sh running ----------"

# echo "current user:(" $(whoami) ")"

# log begin time
echo "begin : ($(date '+%Y-%m-%d %H:%M:%S'))"

# =号两边不能有空格
file_cnt=0

find . -type f -name ".gitmodules" -print0 | while IFS= read -r -d '' file; do
    dir=$(dirname "$file")
    (
        cd "$dir" || { echo "⚠️ dir switch err: $dir"; exit 1; }
        
        # echo "finding $(pwd)/.gitmodules"
        if [ -f "$(pwd)/.gitmodules" ]; then 
            echo "!!! ($((file_cnt + 1))) was found $(pwd)/.gitmodules, ready to process"
            sed -i 's|https://github.com/|git@github.com:|g' .gitmodules
            git submodule sync --recursive
        fi    
    )

    file_cnt=$((file_cnt + 1))
done

# log end time
echo "end   : ($(date '+%Y-%m-%d %H:%M:%S'))"

备份一个文件

#!/bin/bash

# file - bk_file.sh

# function - 从命令行给定一个文件名 e.g. a.html, 备份为 a.html.bk

# run env = git bash

# sh begin
clear
echo "task begin, please wait a moment"

# 命令行参数检查

# echo "param count = $#"
# param count = 0
# 如果不给命令行参数,那么参数个数 $# 就为 0

if [ $# -ne 1 ]; then
    echo "usage : $0 <some_file_path_name>"
    echo "must give a file path name to operation"
    exit 1
fi


# 文件存在性检查
if [ ! -f "$1" ]; then 
    echo "Error: File [$1] does not exist." 
    exit 1 
else 
    echo "ok - File [$1] exists." 
fi

# 执行备份操作
cp "$1" "$1.bk"

# 判断上一个命令是否执行成功
if [ $? -ne 0 ]; then
    echo "failed - cp "$1" "$1.bk""
    exit 1
fi

# 检查备份文件是否存在
if [ ! -f "$1.bk" ]; then 
    echo "error - back up file [$1.bk]" 
    exit 1 
else 
    echo "ok - back up File [$1.bk]" 
fi


# sh end
echo "END"

END

相关文章:

  • Calico网络组件本地部署支持IPv6(Kubernetes)
  • deepseek和chatgpt对比
  • 移远通信边缘计算模组成功运行DeepSeek模型,以领先的工程能力加速端侧AI落地
  • DeepSeek教unity------MessagePack-01
  • vue纯静态实现 视频转GIF 功能(附源码)
  • HARCT 2025 分论坛10:Intelligent Medical Robotics智能医疗机器人
  • 详解df -h命令
  • BERT文本分类(PyTorch和Transformers)畅用七个模型架构
  • win11 MBR 启动 如何把我的硬盘改 GPT win11 的 UEFI 启动
  • Springboot3与openApi
  • Golang 语言的内存管理
  • android 安装第三方apk自动赋予运行时权限
  • 二次封装axios解决异步通信痛点
  • Electron 全面解析:跨平台桌面应用开发指南
  • Web前端开发--HTML
  • css: 针对属性left/right/top/bottom为啥设置transition动画不起作用
  • Mysql中使用sql语句生成雪花算法Id
  • Linux内核模块参数与性能优化:__read_mostly属性的深度剖析
  • 前端开发所需参考文档—重中之中
  • postman登录cookie设置
  • 诠释微末处的丰盈:“上海制造佳品汇”首届海外专场即将亮相日本大阪
  • 国家林草局原党组成员、副局长李春良接受审查调查
  • 广东省人大教科文卫委原主任委员梁万里被开除党籍:退休后受贿仍不知止
  • 江西省司法厅厅长张强已任江西省委政法委分管日常工作副书记
  • 广西百色通报:极端强对流天气致墙体倒塌,3人遇难7人受伤
  • “苏河超级管”调研:桥下公园“留白”很好,指引差点