Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

How to create a text file using variable #984

ChunwenWang ·
HI,
I created a variable named build_comment
Edit Step of Type: Build > Shell/Batch Command
My command is: sudo echo ${vars.get("build_comment")} > abc
I run this configuration and input build comment as "This is build comment xxx "
The log is as follow
Why did it not produce a file abc for me ?
it just show This is build comment xxx >abc in log

Another question:
Can it show in overview?
I mean the variable value, not the file.
I would like to show it in summary table.
If it can do so, I won't create a text file with this variable and publish it.
Now I can do is show it in build version. But it is too long in title.

21:52:10,781 INFO - Checking execute condition of step 'master'...
21:52:10,781 INFO - Execute condition satisfied, selecting node to execute step 'master'...
21:52:10,820 [master@chunwen-desktop:8810] INFO - Executing step 'master' on node 'chunwen-desktop:8810'...
21:52:10,834 [master@chunwen-desktop:8810] INFO - Acquiring workspace mutex...
21:52:10,834 [master@chunwen-desktop:8810] INFO - Workspace mutex acquired.
21:52:10,834 [master@chunwen-desktop:8810] INFO - Running step...
21:52:10,834 [master@chunwen-desktop:8810] INFO - Checking execute condition of step 'produce build_comment.txt'...
21:52:10,840 [master@chunwen-desktop:8810] INFO - Execute condition satisfied, selecting node to execute step 'produce build_comment.txt'...
21:52:10,851 [produce build_comment.txt@chunwen-desktop:8810] INFO - Executing step 'produce build_comment.txt' on node 'chunwen-desktop:8810'...
21:52:10,856 [produce build_comment.txt@chunwen-desktop:8810] INFO - Acquiring workspace mutex...
21:52:10,857 [produce build_comment.txt@chunwen-desktop:8810] INFO - Workspace mutex acquired.
21:52:10,857 [produce build_comment.txt@chunwen-desktop:8810] INFO - Running step...
21:52:10,948 [produce build_comment.txt@chunwen-desktop:8810] INFO - This is build comment xxx >abc
21:52:10,984 [produce build_comment.txt@chunwen-desktop:8810] INFO - Step 'produce build_comment.txt' is successful.
21:52:10,984 [produce build_comment.txt@chunwen-desktop:8810] INFO - Workspace mutex released.
21:52:11,019 [master@chunwen-desktop:8810] INFO - Step 'master' is successful.
21:52:11,019 [master@chunwen-desktop:8810] INFO - Workspace mutex released.
  • replies 14
  • views 3909
  • stars 0
robinshen ADMIN ·
Just write the command as "sudo sh -c echo ${vars.get("build_comment")} > abc"
ChunwenWang ·
It still does not work for me.
I only set a step named produce build_comment.txt
Working Directory : /works
Others are default value.
Please help.
log as follow

09:05:01,069 INFO - Checking execute condition of step 'master'...
09:05:01,070 INFO - Execute condition satisfied, selecting node to execute step 'master'...
09:05:01,100 [master@chunwen-desktop:8810] INFO - Executing step 'master' on node 'chunwen-desktop:8810'...
09:05:01,106 [master@chunwen-desktop:8810] INFO - Acquiring workspace mutex...
09:05:01,106 [master@chunwen-desktop:8810] INFO - Workspace mutex acquired.
09:05:01,107 [master@chunwen-desktop:8810] INFO - Running step...
09:05:01,107 [master@chunwen-desktop:8810] INFO - Checking execute condition of step 'produce build_comment.txt'...
09:05:01,107 [master@chunwen-desktop:8810] INFO - Execute condition satisfied, selecting node to execute step 'produce build_comment.txt'...
09:05:01,126 [produce build_comment.txt@chunwen-desktop:8810] INFO - Executing step 'produce build_comment.txt' on node 'chunwen-desktop:8810'...
09:05:01,132 [produce build_comment.txt@chunwen-desktop:8810] INFO - Acquiring workspace mutex...
09:05:01,132 [produce build_comment.txt@chunwen-desktop:8810] INFO - Workspace mutex acquired.
09:05:01,132 [produce build_comment.txt@chunwen-desktop:8810] INFO - Running step...
09:05:01,388 [produce build_comment.txt@chunwen-desktop:8810] INFO -
09:05:01,425 [produce build_comment.txt@chunwen-desktop:8810] INFO - Step 'produce build_comment.txt' is successful.
09:05:01,425 [produce build_comment.txt@chunwen-desktop:8810] INFO - Workspace mutex released.
09:05:01,451 [master@chunwen-desktop:8810] INFO - Step 'master' is successful.
09:05:01,451 [master@chunwen-desktop:8810] INFO - Workspace mutex released.
robinshen ADMIN ·
Sorry should be written as below:
sudo sh -c "echo ${vars.get("build_comment")} > abc"
ChunwenWang ·
Thanks. It works well now.
I am a little confused.
Why does it must start with sh -c ?
So should I must start with "sh -c" to my every shell command ?
But I build my code with "sudo make xxx" successfully.
I don't know when to start with "sh -c".
robinshen ADMIN ·
Since "make" is a binary, while "echo" is a sub command interpreted by "/bin/sh"
ChunwenWang ·
Thanks your remind.
I can't input single quote ' or double quote " in my variable build_comment by this way.
Is there any smarter way to fix?
My solution to input single quote in build_comment as follow
sudo sh -c "echo \"${vars.get("build_comment")}\" > abc"
But it still can't input double quote "
robinshen ADMIN ·
Please use script step (misc->execute script) to avoid redirection hassles:
groovy: new File(configuration.workspaceDir, "abc").write("\"" + vars.get("build_comment") + "\"")
ChunwenWang ·
Thanks.
It can work by this way.
quoc.vuong ·
Dear Robin,

How to write many variable to one file.
Example:
var1=abc
var2="(/?ab)" -> var2 include some special character.

I want to write var1, var2 to variable.txt
How to make it by groovy script ?

Best Regard,
Nguyen Quoc Vuong
robinshen ADMIN ·
Please use below script to see if it works:
groovy: 
def content = ""
content += "var1="
content += vars.getValue("var1")
content += "var2=";
content += vars.getValue("var2")
new File(configuration.workspaceDir, "vars.txt").write(content)
quoc.vuong ·
Thank you Robin.
I make groovy for run linux command:

groovy:
def String cmd = "echo `date +\"%T\"`"
Process p;
def String [] command = ["/bin/bash", "-c", cmd];
p = Runtime.getRuntime().exec(command);

My problem: I want to write the result of procees p to file "vars.txt"


### OR Could you show me the code groovy to write current date and time with format (YYYY/MM/DD hh:mm:ss Ex : 2013/03/07 11:12:00) to file vars.txt

Best Regard,
Nguyen Quoc Vuong
robinshen ADMIN ·
In groovy, use below code to format current date:
new Date().format("YYYY/MM/DD hh:mm:ss")

Then you can write the result to text file as usual.
quoc.vuong ·
Dear Robin,
I run command in shell
and I want to write the result to file.

Example:
write the result of command "ls -lai" to file result.txt
Could you show me the code groovy to implement that.

Thank you so much.
Best Regard,
Nguyen Quoc Vuong
robinshen ADMIN ·
Hi,
Try this please:
groovy:
new File(configuration.workspaceDir, "file.txt").text = "ls -lai".execute().text