0

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
iaav
  • 484
  • 2
  • 9
  • 26

2 Answers2

1

It's a quoting problem, but a non-trivial one. First, you didn't enclose ${meta_data[$index]} in double quotes in the tests. 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: It splits it into individual words whenever it encounters whitespace, and it expands globbing characters (= filename expansion). So, if you have a file z in the current directory, ? is replaced by z. Double quotes around `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 is assigned unmodified to the first array element. 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. After the assignment is done, you can restore the normal shell behavior by executing set +f.

Uwe
  • 718
  • 4
  • 9
0

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
Alfe
  • 56,346
  • 20
  • 107
  • 159
  • Did not work. I get same result for only modified files but not test condition. – iaav May 29 '13 at 15:08
  • Yeah, actually, I cannot reproduce any error behavior. So maybe you'd like to tell sth more about this. The quotes, however, should never be omitted. – Alfe May 29 '13 at 15:10
  • What I find weird is that you shebang a shell called `/usr/bin/sh` but then propose code which is `bash` syntax (and also tag the question with `bash`). – Alfe May 29 '13 at 15:12
  • What does `echo "${meta_data[$index]}"` in the loop give as output? And what about `"${meta_files[$index]}"`? – Alfe May 29 '13 at 15:14
  • Did you mean output when run from command line? Updated the question part. – iaav May 29 '13 at 15:17
  • 1
    @Alfe The filename expansion of globbing characters happens already in the array assignment. Try the following: `A=($(echo 'x' '*' 'y')); printf '%s\n' "${A[@]}"`. The problem is that one cannot fix this using double quotes as in `A=("$(echo 'x' '*' 'y')"); printf '%s\n' "${A[@]}"`; so one has to switch off filename expansion explicitly using `set -f`. (Of course, `?` will only be expanded, if there is at least one file whose name consists of only one character in the current directory.) – Uwe May 29 '13 at 15:38
  • (… which doesn't start with `.` …) Very good reason, Uwe! :) – Alfe May 30 '13 at 08:14