Trouble with quotes in ant's <exec> - linux

This is the command that runs just fine on terminal
egrep Version ./path/fileName.java | cut -d"\"" -f4
I've used the following in my code
<exec command="egrep Version ./path/fileName.java | cut -d"\" -f4)" outputproperty="VER"/>
But getting errors
the command attribute is deprecated.
[exec] Please use the executable attribute and nested arg elements.
[exec] Result: 1
[echo] "Version: egrep: invalid argument `\\' for `--directories'
[echo] Valid arguments are:
[echo] - `read'
[echo] - `recurse'
[echo] - `skip'
[echo] Usage: egrep [OPTION]... PATTERN [FILE]...
[echo] Try `egrep --help' for more information."
there is one less quot; in the command because if I write 2 quots, it will give me number of quots unbalanced error.

Ant's <exec> uses Java's rules for execution, in particular it is not a shell and does not understand pipes and redirections on its own. Probably your best bet will be to invoke a shell. You will also need to capture the output in a property if you want to make use of it later in your build:
<exec executable="sh" outputproperty="version.number">
<arg value="-c" />
<arg value="egrep Version ./path/fileName.java | cut -d'"' -f4" />
</exec>
Alternatively, you could forget the exec and implement the required logic directly in Ant using loadfile with a filterchain rather than calling an external process:
<loadfile srcFile="path/fileName.java" property="version.number"
encoding="UTF-8">
<filterchain>
<tokenfilter>
<!-- equivalent of egrep Version -->
<containsregex pattern="Version" />
<!-- equivalent of the cut - extract the bit between the third and
fourth double quote marks -->
<containsregex pattern='^[^"]*"[^"]*"[^"]*"([^"]*)".*$$'
replace="\1" />
</tokenfilter>
<!-- I'm guessing you don't want a trailing newline on your version num -->
<striplinebreaks />
</filterchain>
</loadfile>

Try using ' in your xml
<exec command='egrep Version ./path/fileName.java | cut -d"\"" -f4)' outputproperty="VER"/>

Related

shell script sed replace

I have this file config.xml
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev#callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>PASSWORD</enter>
<content src="index.html" />
<access origin="*" />
I tried to do it with sed without success.
I need to do this:
$./script.sh config.xml NEWPASSWORD
to get:
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev#callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>NEWPASSWORD</enter>
<content src="index.html" />
<access origin="*" />
Using backreference:
sed "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
^\( *<enter>\): search for lines starting with any number of spaces followed by <enter>. Matching characters are captured with escaped parentheses.
\([^>]*\)<: following characters up top next < are captured in a second group.
\1$2<: in the substitution string, characters from first group are output(\1) followed by the second parameter value passed to the script, ($2, the new password value)
The command is applied to $1, the file passed as first parameter to the script (the file name).
To edit the file in place, use the -i flag:
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
The good result is:
$cat script.sh
#!/bin/sh
file=$1
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
Then:
$./script.sh config.xml NEWPASSWORD
Many thanks to everyone, especially to Kenavoz.

Can not run ' Ant exec executable="zip" '

Below is my ant script.
<exec executable="zip" dir="/usr/local/clo/ven/image/manual_bundle/testzip/">
<arg value="-y"/>
<arg value="-r"/>
<arg value="${file.path}"/>
<arg value="*"/>
</exec>
But below error occur.
zip-image_binary:
[exec] zip warning: name not matched: *
[exec]
[exec] zip error: Nothing to do! (try: zip -y -r /usr/local/clo/ven/image/a.zip . -i *)
[exec] Result: 12
My purpose is to zip all the files and directories under /usr/local/clo/ven/image/manual_bundle/testzip/
When you run the command using your shell then the shell expands the * glob pattern. The zip executable doesn't expect any pattern at all but a list of files (usually provided by your shell). If you don't want to use the built-in zip task you can emulate that behaviour by using apply rather than exec. Something like this
<apply executable="zip" parallel="true" relative="true"
dir="/usr/local/clo/ven/image/manual_bundle/testzip/">
<fileset dir="/usr/local/clo/ven/image/manual_bundle/testzip/"/>
<mergemapper to="${file.path}"/>
<arg value="-y"/>
<arg value="-r"/>
<targetfile/>
</apply>
The equivalent zip task is a lot simpler
<zip destfile="${file.path}">
<fileset dir="/usr/local/clo/ven/image/manual_bundle/testzip/"/>
</zip>

Linux command to add a word after a specific line

we need to add the line <property name="assumeHungTime" value="600000" />
after the line <bean id="DataUnpacker" class="com-packer" singleton="false">
in one xml file . Please let me know the command to add it - using sed?
Thanks,
Ravikanth
The follwing sed command will do what you want.
sed -e '/<bean id="DataUnpacker" class="com-packer" singleton="false">/a<property name="assumeHungTime" value="600000" />'

grep does not take pattern from file

I have a pattern file, say t.txt, containing the following:
vali
and the file I want to run grep against, say a, containing the following:
validate
validate:
bw_validate:
[echo] Validating project: CrossService_v1_59_5_1
[echo] Found 62 errors and 28 warnings
[echo] -----------------------------------------------------------------
[echo] Validating project: CRM-UDB_59_4_2
[echo] Found 25 errors and 28 warnings
[echo] -----------------------------------------------------------------
[echo] Validation Failed: At least one project contains errors.
notify:
BUILD FAILED
bw.xml:311: Validation Failed: At least one project contains errors.
if I execute:
grep -iE vali a
I get the expected output, i.e.:
validate
validate:
bw_validate:
[echo] Validating project: CrossService_v1_59_5_1
[echo] Validating project: CRM-UDB_59_4_2
[echo] Validation Failed: At least one project contains errors.
bw.xml:311: Validation Failed: At least one project contains errors.
but if I execute:
grep -iE -f t.txt a
I don't get any match.
files are readable and both in the same directory (from which I execute the command).
I tried both with -f and --file=t.txt, --file='t.txt', --file="t.txt"
I'm on linux Fedora 16 64bit. Strangely enough, the same command works properly in windows with the grep/egrep porting.
Am I missing something?
Any help is appreciated as this is giving me an headache :(
thanks!
It should work right (it works for me). The -f switch is specified by POSIX.
The fact that you say it's working on Windows gave me an idea: Could it be the t.txt file ends with a DOS newline? I tried it with a clean file (no newline), and it worked. Then I tried it with a DOS file, and I was able to reproduce your results.
Try dos2unix to "fix" DOS files.
If you don't have the dos2unix utility then you can do any of the following to convert DOS or Windows newlines to Unix newlines.
This one-liner assumes that all lines end with CR+LF (carriage return + line feed)
sed -i 's/.$//' filename
You can usually enter the ^M control char literally on the command line by first pressing Ctrl-v (control key + v key) and then Ctrl-m.

sed -i 's/^M$//' filename
This following one-liner assumes that we are using GNU sed. The hex value for CR is 0x0D.
sed -i 's/\x0D$//' filename

While using sed command i receive the following error : Function cannot be parsed. Any Solutions or reason for this?

I need to replace a particular character in a text file with another character. For example, replacing "E" with "A":
Apple ice → ApplA Ica
While executing sed 's/E/A' < apple.txt > app.txt I receive the error
function cannot be parsed
Please help! I need to automate this using Antscript.
You should terminate your sed command with a slash (/) and I guess you want to exchange all occurences of E with A? Then you have to add a g for a global substitution:
sed 's/E/A/g' app.txt
sed 's/E/A/g' app.txt. You missed the trailing / (g means all occurrences),
Since you are in Ant environment, you probably don't need to execute sed at all, but rather use Copy task with filter, or ReplaceRegExp task.
<?xml version="1.0" encoding="UTF-8"?>
<project>
<replaceregexp file="apple.txt" flags="g" match="e" replace="A"/>
</project>
This alters the file in place:
$ cat apple.txt
Apple ice
$
$ ant
Buildfile: build.xml
BUILD SUCCESSFUL
Total time: 0 seconds
$
$ cat apple.txt
ApplA icA
Your example is strange with case (a|A, e|E). I'll assume that's typo.
Follow up: To declare encoding...
<?xml version="1.0" encoding="utf-8"?>
<project>
<replaceregexp file="apple.txt" encoding="utf-8" flags="g" match="Á" replace=" "/>
</project>
I tested this successfully. Before:
ApplA icA
ApplÁs icÁs
After:
ApplA icA
Appl s ic s

Resources