gpt4 book ai didi

用于过滤 git status 命令的 Linux Shell 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:03 24 4
gpt4 key购买 nike

我想要的是一个脚本 shell ,它遍历 git status 命令的每一行,查找已修改的文件,并为每个文件路径运行给定的命令。

所以更具体地说,鉴于此输出:

On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

renamed: ASCourses-UI-vechi/src/application/Chat.fxml -> ASCourses-UI /src/main/resources/fxml/ChatController.fxml

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: ASCourses-UI-vechi/bin/.gitignore
modified: ASCourses-UI-vechi/bin/application/Chat.css
modified: ASCourses-UI-vechi/bin/application/Chat.fxml
modified: ASCourses-UI-vechi/bin/application/ChatController$1.class
modified: ASCourses-UI-vechi/bin/application/ChatController$2.class
modified: ASCourses-UI-vechi/bin/application/ChatController$3$1.class
modified: ASCourses-UI-vechi/bin/application/ChatController$3.class
modified: ASCourses-UI-vechi/bin/application/ChatController$4.class
modified: ASCourses-UI-vechi/bin/application/ChatController$5.class

我想为每个被修改并以 ASCourses-UI-vechi 开头的文件运行 git checkout file_path

到目前为止我得到的是:

#!/bin/bash
(IFS='
'
for x in ` git status | grep -E 'modified.*ASCourses-UI-vechi' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`;
do
echo $x;
done)

所以目前我过滤和修剪输出并得到这个:

modified:   ASCourses-UI-vechi/bin/.gitignore
modified: ASCourses-UI-vechi/bin/application/Chat.css
modified: ASCourses-UI-vechi/bin/application/Chat.fxml
modified: ASCourses-UI-vechi/bin/application/ChatController$1.class
modified: ASCourses-UI-vechi/bin/application/ChatController$2.class
modified: ASCourses-UI-vechi/bin/application/ChatController$3$1.class
modified: ASCourses-UI-vechi/bin/application/ChatController$3.class
modified: ASCourses-UI-vechi/bin/application/ChatController$4.class
modified: ASCourses-UI-vechi/bin/application/ChatController$5.class

问题是我不知道如何从第一次出现的 ASCourses 字符串到整个字符串的末尾进行子字符串

最佳答案

使用 awk:

[user@user ~]$ echo "modified:   ASCourses-UI-vechi/bin/.gitignore" | awk '{print $2}'
ASCourses-UI-vechi/bin/.gitignore

使用 sed:

[user@user ~]$ echo "modified:   ASCourses-UI-vechi/bin/.gitignore" | sed 's/modified:\s\+//'
ASCourses-UI-vechi/bin/.gitignore

关于用于过滤 git status 命令的 Linux Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40533239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com