简体   繁体   中英

Linux Shell Script to filter git status command

What I want is a script shell that iterates through each line of git status command, finds files that were modified and for each file path run a given command.

So to be more specific, Given this output:

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

I want for each file that was modified and starts with ASCourses-UI-vechi to run git checkout file_path

What I got so far is this:

#!/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)

So currently I filter and trim output and get this:

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

Problem is I don't know how to substring from first occurrence of ASCourses string until the end of the whole string

Using awk:

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

Using sed:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM