递归添加特定源文件类型到svn

发布于 2024-12-09 06:29:33 字数 544 浏览 0 评论 0原文

我知道这是一个丑陋的脚本,但它完成了工作。 我现在面临的是添加更多的扩展,这会使票据更加混乱。 我怎样才能让它更加模块化? 具体来说,如何在多行上编写这么长的正则表达式(源文件扩展名)?每行说一个分机号。我想我在字符串连接方面做错了什么,但不太确定到底是什么。 这是原始文件:

#!/bin/bash
COMMAND='svn status'
XARGS='xargs'
SVN='svn add'
$COMMAND | grep -E '(\.m|\.mat|\.java|\.js|\.php|\.cpp|\.h|\.c|\.py|\.hs|\.pl|\.xml|\.html|\.sh|.\asm|\.s|\.tex|\.bib|.\Makefile|.\jpg|.\gif|.\png|.\css)'$ | awk ' { print$2 } ' | $XARGS $SVN

这大致是我的目标

...code code
'(.\m|
\.mat|
\.js|
.
.
.
.\css)'
..more code here

有人吗?

I know this is an ugly script but it does the job.
What I am facing now is adding a few more extensions what would clutter the scrip even more.
How can I make it more modular?
Specifically, how can I write this long regular expression (source file extensions) on multiple lines? say one extension on each line. I guess I am doing something wrong with string concatenation but not quite sure what exactly.
Here's the original file:

#!/bin/bash
COMMAND='svn status'
XARGS='xargs'
SVN='svn add'
$COMMAND | grep -E '(\.m|\.mat|\.java|\.js|\.php|\.cpp|\.h|\.c|\.py|\.hs|\.pl|\.xml|\.html|\.sh|.\asm|\.s|\.tex|\.bib|.\Makefile|.\jpg|.\gif|.\png|.\css)'$ | awk ' { print$2 } ' | $XARGS $SVN

and here's roughly what I am aiming at

...code code
'(.\m|
\.mat|
\.js|
.
.
.
.\css)'
..more code here

Anybody?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

死开点丶别碍眼 2024-12-16 06:29:33

我知道这并不能直接回答问题,但从可读性的角度来看,我认为大多数开发人员都会同意单行正则表达式是最常见的处理方式,因此也是最可维护的方法。此外,我不确定为什么您在每个扩展中都包含一个句点,这应该只需要使用一次。

我写了这个小脚本来自动将所有图像添加到 svn。您应该能够简单地在正则表达式中的管道之间添加扩展名来添加或删除不同的文件类型。请注意,它通过确保每行以“?”开头来确保仅添加无法识别的文件。 (^\?) 并以句点 (\.(extensions)$) 结尾。希望它有帮助!

#!/bin/bash

svn st | grep -E "^\?.*\.(png|jpg|jpeg|tiff|bmp|gif)$" > /tmp/svn-auto-add-img

while read output; do
    FILE=$(echo $output | awk '{ print $2 }')
    svn add $FILE
done < /tmp/svn-auto-add-img

exit 0

I know this doesn't answer the question directly, but from a readability perspective, I think most developers would agree that a single-line regex is the most common way to do things and therefore the most maintainable approach. Further, I'm not sure why you're including a period in each of your extensions, this should only need to be used once.

I wrote this little script to automatically add all images to svn. You should be able to simply add extensions between the pipes in the regex to add or remove different file types. Note that it makes sure to only add files that are unrecognized by making sure each line starts with a "?" (^\?) and ends with a period (\.(extensions)$). Hope it's helpful!

#!/bin/bash

svn st | grep -E "^\?.*\.(png|jpg|jpeg|tiff|bmp|gif)$" > /tmp/svn-auto-add-img

while read output; do
    FILE=$(echo $output | awk '{ print $2 }')
    svn add $FILE
done < /tmp/svn-auto-add-img

exit 0
一曲爱恨情仇 2024-12-16 06:29:33

怎么样:(

PATTERNS="
\.foo
\.bar
\.baz"

# Put them into one list separated by or ("|").
PATTERNS=`echo $PATTERNS |sed 's/\s\+/|/g'`

$COMMAND | grep -E "($PATTERNS)"

请注意,如果您在调用 echo 时在 $PATTERNS 周围加上引号,这将不起作用 - echo 正在为我们负责剥离空格并将换行符转换为空格。)

How about this:

PATTERNS="
\.foo
\.bar
\.baz"

# Put them into one list separated by or ("|").
PATTERNS=`echo $PATTERNS |sed 's/\s\+/|/g'`

$COMMAND | grep -E "($PATTERNS)"

(Note that this would not work if you put quotes around $PATTERNS in the call to echo -- echo is taking care of stripping whitespace and converting newlines to spaces for us.)

小嗷兮 2024-12-16 06:29:33
#!/bin/bash
COMMAND='svn status'
XARGS='xargs'
SVNADD='svn add'
pats=
pats+=' \.m'
pats+=' \.mat'
pats+=' \.java'
pats+=' \.js'

# add your 'or-able' sub patterns here

# build the full pattern 
pattern='(';for pat in $pats;do pattern+="$pat|";done;pattern=${pattern%\|}')


# run grep with the generated pattern
files=$($COMMAND | grep -E ${pattern} | awk ' { print $NF } ')
if [ " $files" != " " ]
then
   $COMMAND | grep -E ${pattern} | awk ' { print $NF } ' | $XARGS $SVNADD
fi
#!/bin/bash
COMMAND='svn status'
XARGS='xargs'
SVNADD='svn add'
pats=
pats+=' \.m'
pats+=' \.mat'
pats+=' \.java'
pats+=' \.js'

# add your 'or-able' sub patterns here

# build the full pattern 
pattern='(';for pat in $pats;do pattern+="$pat|";done;pattern=${pattern%\|}')


# run grep with the generated pattern
files=$($COMMAND | grep -E ${pattern} | awk ' { print $NF } ')
if [ " $files" != " " ]
then
   $COMMAND | grep -E ${pattern} | awk ' { print $NF } ' | $XARGS $SVNADD
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文