2016-11-10 117 views
0

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

所以更具体, 鉴于这种输出:

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字符串的第一次出现,直到串整串

回答

0

年底用awk:

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

使用SED:

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