简体   繁体   English

Subversion文件的Shell脚本

[英]shell script for subversion files

The script is not working for "?" 该脚本不适用于“?” case. 案件。 When i run in command line directly ...works okay but in script is not able to run the "?" 当我直接在命令行中运行时...可以,但是在脚本中无法运行“?” condition. 条件。

  svn st svn_promote_WP
  M       svn_promote_WP\Code\Environment\DEV\properties\build.properties
  M       svn_promote_WP\Code\Environment\RT\properties\build.properties
  ?       svn_promote_WP\props.pl

this is my script: 这是我的脚本:

 #!/usr/bin/sh

 meta_data=(`svn st svn_promote_WP | cut -c1`)
 meta_files=(`svn st svn_promote_WP | awk '$1~/^[AMD?]$/{for(i=2;i<=NF;i++)print $i}'`)

 for index in ${!meta_data[*]}
 do
    if [ ${meta_data[$index]} = "?" ]; then
    echo "${meta_files[$index]} need to be added"
    elif [ ${meta_data[$index]} = "M" ]; then
    echo "${meta_files[$index]} are modified"
    fi
 done

Output: 输出:

 svn_promote_WP\Code\Environment\DEV\properties\build.properties are modified
 svn_promote_WP\Code\Environment\RT\properties\build.properties are modified

Desired output: 所需的输出:

  svn_promote_WP\Code\Environment\DEV\properties\build.properties are modified
  svn_promote_WP\Code\Environment\RT\properties\build.properties are modified
  svn_promote_WP\props.pl need to be added

Updated more details - results when run on command line: 更新了更多详细信息-在命令行上运行时的结果:

$ svn st svn_promote_WP | cut -c1
M
M
?


$ svn st svn_promote_WP | awk '$1~/^[AMD?]$/{for(i=2;i<=NF;i++)print $i}'
svn_promote_WP\Code\Environment\DEV\properties\build.properties
svn_promote_WP\Code\Environment\RT\properties\build.properties
svn_promote_WP\props.pl

It's a quoting problem, but a non-trivial one. 这是一个报价问题,但不是一个简单的问题。 First, you didn't enclose ${meta_data[$index]} in double quotes in the tests. 首先,您没有在测试${meta_data[$index]}括在双引号中。 And second, the ? 二是? was already expanded in the array assignment. 已在数组分配中扩展。 Unfortunately, this second problem cannot be fixed by adding double quotes; 不幸的是,第二个问题无法通过添加双引号解决。 you need something more tricky, namely 您需要更棘手的东西,即

set -f
meta_data=(`svn st svn_promote_WP | cut -c1`)
meta_files=(`svn st svn_promote_WP | awk '$1~/^[AMD?]$/{for(i=2;i<=NF;i++)print $i}'`)
set +f

What's the reason? 什么原因? In your original code, bash performs two operations on the output of svn st svn_promote_WP | cut -c1 在您的原始代码中,bash在svn st svn_promote_WP | cut -c1的输出上执行两项操作svn st svn_promote_WP | cut -c1 svn st svn_promote_WP | cut -c1 : It splits it into individual words whenever it encounters whitespace, and it expands globbing characters (= filename expansion). svn st svn_promote_WP | cut -c1 :每当遇到空格时,它将其拆分为单个单词,并扩展通配符(=文件名扩展)。 So, if you have a file z in the current directory, ? 因此,如果当前目录中有文件z ,则? is replaced by z . z替换。 Double quotes around `svn st svn_promote_WP | cut -c1` `svn st svn_promote_WP | cut -c1`周围的双引号`svn st svn_promote_WP | cut -c1` `svn st svn_promote_WP | cut -c1` would prevent both word splitting and filename expansion, so that the entire output of svn st svn_promote_WP | cut -c1 `svn st svn_promote_WP | cut -c1`可以防止字分割和文件名扩展,因此svn st svn_promote_WP | cut -c1的整个输出svn st svn_promote_WP | cut -c1 svn st svn_promote_WP | cut -c1 is assigned unmodified to the first array element. svn st svn_promote_WP | cut -c1未修改地分配给第一个数组元素。 But you need something different, namely "keep word splitting, but prevent filename expansion". 但是您需要一些不同的东西,即“保留单词拆分,但防止文件名扩展”。 In order to get that effect, you must omit the double quotes and use a shell option that disables filename expansion, namely -f . 为了获得这种效果,您必须省略双引号,并使用一个禁用文件名扩展的shell选项,即-f After the assignment is done, you can restore the normal shell behavior by executing set +f . 分配完成后,您可以通过执行set +f恢复正常的shell行为。

Quote the usage of your variables: 引用变量的用法:

if [ "${meta_data[$index]}" = "?" ]; then
echo "${meta_files[$index]} need to be added"
elif [ "${meta_data[$index]}" = "M" ]; then
echo "${meta_files[$index]} are modified"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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