from command line, how to pass a variable to coffeescript, so it can replace a corresponding placeholder, something like this:
$ echo "module.exports = {version: '$VERSION'}" | coffee -p -s VERSION=0.0.0
Expected JS:
(function() {
module.exports = {
version: '0.0.0'
};
}).call(this);
Thank you
Two things:
You need to define VERSION in the echo, not in the coffeescript compiler; by the time the coffeescript compiler sees it it's already translated $VERSION into ''.
echo is a shell builtin, and therefore the standard VERSION=0.0.0 echo "$VERSION" construct doesn't work.
So you want to create a new subshell so that the setting of VERSION doesn't propagate into your main shell, then perform the echo and coffee, like so:
$ (VERSION=0.0.0; echo "module.exports = {version: '$VERSION'}" | coffee -ps)
(function() {
module.exports = {
version: '0.0.0'
};
}).call(this);
The parentheses around the expression stop VERSION from being set:
$ echo $VERSION
$
Related
I want to validate a string which should only be Alphabets (Capital/Small). I can do it in Linux easily using Bash or Shell, but not able to validate in Busybox (OpenWRT). My piece of code is
...
#!/bin/sh
. /usr/share/libubox/jshn.sh
Info=$(cat /root/Info.json)
json_load "$Info"
json_get_var value plmn_description
echo "$value"
if [[ "$value" == [a-zA-Z] ]] ;then
echo "Valid"
else
echo "Invalid information"
fi
...
You can use case conditional construct like this:
case "$value" in
*[!a-zA-Z]*) echo invalid information ;;
*) echo valid
esac
Using Busybox awk:
$ busybox awk '{ # using busybox awk
for(i=1;i<NF;i++) # iterate all json record fields (not the last, thou)
if($i=="\"plmn_description\":" && $(i+1)~/^\"[a-zA-Z]+\",?$/) {
ret="Valid" # if "plmn_description": is followed by "alphabets"
exit # exit for performance
}
}
END {
print (ret?ret:"Invalid") # output Valid or Invalid
}' Info.json # process the json file
Output:
Valid
Background
I am running a Jenkns Job, called Job A, that feeds its build parameters into a perl script, called ScriptA.pl, in the following format below:
#Check the input params
my $PARAM1 = $ENV{ "PARAM1" };
my $PARAM2 = $ENV{ "PARAM2" };
.....more params fed in the same way
if ( $PARAM1 eq "" ) {
print "PARAM1 is a required parameter.\n";
exit 1;
}
if ( $PARAM2 eq "" ) {
print "PARAM2 is a required parameter.\n";
exit 1;
}
.....more param checks done in the same way
###Script then runs a bunch of execution statements####
Problem
I am trying to run this script from Linux command line in cshell in the following way to test that the execution component works:
/% ScriptA.pl jetFuel steelBeams
And it thinks that no parameters have been entered, since this error is returned
PARAM1 is a required parameter.
Question
How do I properly input Jenkins parameters into a Jenkins script from command line?
If you can't modify the Perl script so it reads command-line parameters (which would improve its general usability to boot), maybe create a simple wrapper script:
#/bin/sh
env PARAM1="$1" PARAM2="$2" perl ScriptA.pl
(Yes, sh. Nobody should use csh for anything in 2019.)
Need help with groovy script.
We have a perl script in github. We need to download and run the perl-script inside groovy script (jenkins) and also pass parameters to the script and get the output.
Example, Perl script in github accepts parameter "item1" "item2", using curl we get the raw format of the script.
def command = "curl -s https://github.com/raw/script.pl?token=%3D"
def proc = command.execute() | "perl /dev/stdin $item1 $item2".execute()
proc.waitFor()
def roles = []
roles = "${proc.in.text}" .eachLine { line ->
roles << line
}
return roles
Above script does not return expected result. Please help.
Thanks in advance..
Thanks Guys. Found it. Actually following does work..
def command = "curl -s https://github.com/raw/script.pl?token=%3D"
def proc = command.execute() | "perl /dev/stdin $item1 $item2".execute()
proc.waitFor()
def roles = []
roles = "${proc.in.text}" .eachLine { line ->
roles << line
}
return roles
In the process of writing a Perl-script to submit PBS-jobs, I noticed that the output-files are only usable by the owner (rw-------). After some research, I found that you can put #PBS -W umask=002 in the job-script to make it accessible for others.
Perl:
my $client = PBS::Client->new();
my $wd = $dir_temp;
my $name = "demultiplex";
my $queue = "default";
my $wallt = "72:00:00";
my $job_demultiplex = PBS::Client::Job -> new(
wd => $wd,
queue => $queue,
name => $name,
wallt => $wallt,
cmd => "perl ".$script_directory."demultiplex.pl ".$dir_in." 2>"."demultiplex_error.log 1>"."demultiplex_output.log"
);
This Perlscript creates the following job:
#!/bin/sh
#PBS -N demultiplex
#PBS -d /store/www/labresults_QC/small_rna_sequence_analyser/data/data_temp/BGI_pilot
#PBS -q default
#PBS -l nodes=1
#PBS -l walltime=72:00:00
#PBS -W umask=002 <---
perl /store/www/labresults_QC/small_rna_sequence_analyser/scripts/demultiplex.pl /store/www/labresults_QC/small_rna_sequence_analyser/data/data_input/BGI_pilot 2>demultiplex_error.log 1>demultiplex_output.log
Is there a possibility to pass the indicated line (<---) to the job from the Perlscript?
Given the current codebase, no. You'd have to open a bug report to the PBS::Client module and request the feature. Accompany it with a patch - that might help.
When you run a PHP script through a browser it looks something like
http://somewebsite.com/yourscript?param1=val1¶m2=val2.
I am trying to achieve the same thing via command line without having to rewrite the script to accept argv instead of $_REQUEST. Is there a way to do something like this:
php yourscript.php?param1=val1¶m2=val2
such that the parameters you send show up in the $_REQUEST variable?
In case you don't want to modify running script, you can specify parameters using in -B parameter to specify code to run before the input file. But in this case you must also add -F tag to specify your input file:
php -B "\$_REQUEST = array('param1' => 'val1', 'param2' => 'val2');" -F yourscript.php
I can't take credit for this but I adopted this in my bootstrap file:
// Concatenate and parse string into $_REQUEST
if (php_sapi_name() === 'cli') {
parse_str(implode('&', array_slice($argv, 1)), $_REQUEST);
}
Upon executing a PHP file from the command line:
php yourscript.php param1=val1 param2=val2
The above will insert the keys and values into $_REQUEST for later retrieval.
No, there is no easy way to achieve that. The web server will split up the request string and pass it into the PHP interpreter, who will then store it in the $_REQUEST array.
If you run from the command line and you want to accept similar parameters, you'll have to parse them yourself. The command line has completely different syntax for passing parameters than HTTP has. You might want to look into getopt.
For a brute force approach that doesn't take user error into account, you can try this snippet:
<?php
foreach( $argv as $argument ) {
if( $argument == $argv[ 0 ] ) continue;
$pair = explode( "=", $argument );
$variableName = substr( $pair[ 0 ], 2 );
$variableValue = $pair[ 1 ];
echo $variableName . " = " . $variableValue . "\n";
// Optionally store the variable in $_REQUEST
$_REQUEST[ $variableName ] = $variableValue;
}
Use it like this:
$ php test.php --param1=val1 --param2=val2
param1 = val1
param2 = val2
I wrote a short function to handle this situation -- if command line arguments are present and the $_REQUEST array is empty (ie, when you're running a script from the command line instead of though a web interface), it looks for command line arguments in key=value pairs,
Argv2Request($argv);
print_r($_REQUEST);
function Argv2Request($argv) {
/*
When $_REQUEST is empty and $argv is defined,
interpret $argv[1]...$argv[n] as key => value pairs
and load them into the $_REQUEST array
This allows the php command line to subsitute for GET/POST values, e.g.
php script.php animal=fish color=red number=1 has_car=true has_star=false
*/
if ($argv !== NULL && sizeof($_REQUEST) == 0) {
$argv0 = array_shift($argv); // first arg is different and is not needed
foreach ($argv as $pair) {
list ($k, $v) = split("=", $pair);
$_REQUEST[$k] = $v;
}
}
}
The sample input suggested in the function's comment is:
php script.php animal=fish color=red number=1 has_car=true has_star=false
which yields the output:
Array
(
[animal] => fish
[color] => red
[number] => 1
[has_car] => true
[has_star] => false
)