How to append output to an existing file in pipeline code - jenkins-pipeline

I have a file called Sample.txt, now I want to append an Output value to this file.
I tried with the following command, but it is overwriting the file.
writeFile file: "sample.txt", text: "$projectVersion"
How can I achieve it.

This is how I achieved
def readContent = readFile "sample.txt"
writeFile file: "sample.txt", text: "$readContent Version=$projectVersion\n"
echo "${sample.txt}"

Related

shell script is not reading $HOME value properly

I have tried to fetch the value from another file with below code snippet
file=`echo "$(read_properties 'abc.config')"` // reading this input from other file
abc.config =$HOME/one/two
$HOME = /home/cbc
but when i use line number 1, Actual = $HOME/one/two
Expected : /home/cbc/one/two
where if i only print $HOME it gives me output as /home/cbc
but why it is not printing properly when i use in file variable

Writing multiline variable to file cmd

I am writing multiple lines in a variable to a file as part of my build step in groovy jenkins.
def output = GetProfiles()
String[] rev = output.toString().split("\n");
for(int i=0;i<rev.length;i++) {
String cred = rev[i]
bat "#echo off && echo $cred >> \"$tmpDir\\credentials\""
}
GetProfiles returns multiple lines of data and the above code works. However, it takes alot of time as it writes to the file line by line.
Is there a better approach to writing the entire set of lines in the variable to a file in one go? I tried encasing the echo command in () but that doesn't work.
bat "#echo off && (echo $output) > \"$tmpDir\\credentials\""
def output = GetProfiles().toString()
writeFile( file: "$tmpDir/credentials", text: output )

How to get input from a user with ARGV in Ruby to read a file?

I'm trying to read the content of a file (myfile.txt) through ARGV in Ruby. Here is my code:
filename = ARGV.first
puts "Here's your file #{filename}:"
print txt.read
What I have to do to pass the name of the file to ARGV?
Solution
With a ruby script called read_file.rb :
# read_file.rb
# example :
# ruby read_file.rb some_file.txt
filename = ARGV.first
puts "Here's your file #{filename}:"
print File.read(filename)
You can call :
ruby read_file.rb myfile.txt
Your code
print txt.read
txt isn't defined. If you meant filename, filename is a String which contains a filename. It's not a File object, so you cannot call filename.read directly.
Best use
ARGF.read
Notice the spelling with an ...F
ARGF is a stream of either all files named in the arguments, or standard input if no file has been named. This is best practice for scripts and allows your program to be used with a unix pipe.
cat filename | ruby script.rb
ruby script.rb filename
Will both work and do the same if you use ARGF.
Try the following.
file = ARGV.first
file_name_with_extn = File.basename file # => "abc.mp4"
file_extn = File.extname file # => ".mp4"
file_name = File.basename file, extn # => "abc"
file_path = File.dirname file # => "/path/"

how to write an empty line in file from a variable?

In Windows Command Line I normally write empty line in a file with
echo; >> file
However, what I have now is a variable
$param1%
If I want echo to write it in the file I have to do
echo %param1% >> file
HERE IS WHERE THE PROBLEM START :
If I'd like an empty like I'd make
set param1=;
However since the ; is not in contact with the echo word the command is
echo ; >> file
which write the ; in the file...
I need the variable to sometime contains text, and sometime nothing. How can I do it?
if "%param1%"=="" echo;>>file else echo %param1%>>file
If a param1 variable does not exist (the same as set "param1="), then %param1% results to:
In a .bat script: %param1% results to an empty string (a string of zero length);
In a CLI window: %param1% results to the %param1% string.
In a .bat script use (note no spaces surrounding %param1%)
>> file (echo;%param1%)
In a CLI window use
>>file (if not defined param1 (echo;) else echo;%param1%)
Note proper using of parentheses in if-else! For instance, check interesting result of next command:
if ""=="" echo;"THEN branch">>file else echo;"ELSE branch">>file
Output:
==>if ""=="" echo;"THEN branch">>file else echo;"ELSE branch">>file
==>type file
"THEN branch" else echo;"ELSE branch"

How to provide flags to ruby script like -v , -o and place code accordingly?

I have created one ruby script that I want to run with some flags on console say -v flag prints output on console and -o stores output in new file with file name I am taking from console using gets()
My code has following structure:
puts "Enter filename to analyze:\n\n"
filename = gets().chomp
puts "Provide filename to store result in new text file:\n\n"
output = gets().chomp
filesize = File.size(filename)
puts "File size in Bytes:\n#{filesize.to_i}\n"
pagecontent = filesize - 20
puts "\n\nData:\n#{pagecontent}\n\n"
File.open(filename,'r') do |file|
#whole process with few do..end in between that I want to do in 2 different #ways.
#If I provide -v flag on console result of this code should be displayed on console
#and with -o flag it should be stored in file with filename provided on console #stored in output variable declared above
end
end
Use stdlib OptionParser

Resources