syntax error near unexpected token in a case - bash

I am trying to compare in bash using case (in a Jenkinsfile) a given value and act upon it. However, the case fails due to:
syntax error near unexpected token `Manager'
The error happens here:
...
...
def microServicesList = microServicesToUpdate.tokenize(",")
...
...
for (String microserviceName : microServicesList) {
sh """
...
...
case ${microserviceName} in
"Instances Manager")
// do something
;;
esac
"""
}

Had to surround it with double quotes:
case "${microserviceName}" in

Related

Cannot pass parameter with near call

I'm testing the Greeting contract in "workshop--exploring-assemblyscript-contracts" and try to pass parameter to saveMyMessage function but always get error. There maybe problem with quotations marks, I tried with " or '` but nothing succeeds. I'm running it on Windows.
This is the suggestion from the code:
near call greeting..testnet saveMyMessage '{"message": "bob? you in there?"}' --account-id .testnet
When I try
near call %ID% saveMyMessage '{"message": "bob? you in there?"}' --account-id %ID%
Or replace " with " or end single quote with ` always get error like this
Unknown argument: bob? you in there?}'
For starters, it appears you have a syntax error with 2 dots instead of 1 here - greeting..testnet
Have you tried seeing what %ID% is?
I would try the following syntax, but make sure when you set your IDenv variable, that it returns the correct value when you run echo $ID
Once you have your env variables set up correctly, try the following:
near call $ID saveMyMessage '{"message": "bob? you in there?"}' --account-id $ID

Typing multiline expressions in nix repl

This PR seems to indicate that it was at least a feature in nix repl's predecessor, nix-repl.
When I try to replicate the example in the PR, I get errors.
nix-repl> rec {
error: syntax error, unexpected end of file, at (string):1:6
nix-repl> a = b.foo;
error: syntax error, unexpected ';', expecting end of file, at (string):1:7
nix-repl> b.foo.bar = 12;
error: syntax error, unexpected '=', expecting end of file, at (string):1:23
nix-repl> }
error: syntax error, unexpected '}', at (string):1:11
nix-repl>

How can I run all functions in a bash script, in the order in which they are declared without explicitly calling the function? [duplicate]

This question already has answers here:
Get a list of function names in a shell script [duplicate]
(4 answers)
Closed 6 years ago.
Lets say I have a script that has a bunch of functions that act like test cases. So for example:
#!/bin/bash
...
function testAssertEqualsWithStrings () {
assertEquals "something" "something"
}
testAssertEqualsWithStrings <--- I dont want to do this
function testAssertEqualsWithIntegers () {
assertEquals 10 10
}
testAssertEqualsWithIntegers <--- I dont want to do this
function testAssertEqualsWithIntegers2 () {
assertEquals 5 $((10 - 5))
}
testAssertEqualsWithIntegers2 <--- I dont want to do this
function testAssertEqualsWithDoubles () {
assertEquals 5.5 5.5
}
testAssertEqualsWithDoubles <--- I dont want to do this
...
Is there a way I can call of these functions in order without having to actually use that explicit function call underneath each test case? The idea is that the user shouldnt have to manage calling the functions. Ideally, the test suite library would do it for them. I just need to know if this is even possible.
Edit: The reason why I dont use just the assert methods is so I can have meaningful output. My current setup allows me to have output such as this:
...
LineNo 14: Passed - testAssertEqualsWithStrings
LineNo 19: Passed - testAssertEqualsWithIntegers
LineNo 24: Passed - testAssertEqualsWithIntegers2
LineNo 29: Passed - testAssertEqualsWithDoubles
LineNo 34: Passed - testAssertEqualsWithDoubles2
...
LineNo 103: testAssertEqualsWithStringsFailure: assertEquals() failed. Expected "something", but got "something else".
LineNo 108: testAssertEqualsWithIntegersFailure: assertEquals() failed. Expected "5", but got "10".
LineNo 115: testAssertEqualsWithArraysFailure: assertEquals() failed. Expected "1,2,3", but got "4,5,6".
LineNo 120: testAssertNotSameWithIntegersFailure: assertNotSame() failed. Expected not "5", but got "5".
LineNo 125: testAssertNotSameWithStringsFailure: assertNotSame() failed. Expected not "same", but got "same".
...
EDIT: Solution that worked for me
function runUnitTests () {
testNames=$(grep "^function" $0 | awk '{print $2}')
testNamesArray=($testNames)
beginUnitTests #prints some pretty output
for testCase in "${testNamesArray[#]}"
do
:
eval $testCase
done
endUnitTests #prints some more pretty output
}
Then I call runUnitTests at the bottom of my test suite.
If you just want to run these functions without calling them, why declare them as functions in the first place?
You either need them to be functions because you are using them multiple times and need the calls to be different each time, in which case I don't see an issue.
Otherwise you just want to run each function once without calling them, which is just running the command. Remove all the function declarations and just call each line.
For this example
#!/bin/bash
...
assertEquals "something" "something" #testAssertEqualsWithStrings
assertEquals 10 10 #testAssertEqualsWithIntegers
assertEquals 5 $((10 - 5)) #testAssertEqualsWithIntegers2
assertEquals 5.5 5.5 #testAssertEqualsWithDoubles
...

dynamic usage of attribute in recipe

I am trying to increment the value and use in another resource dynamically in recipe but still failing to do that.
Chef::Log.info("I am in #{cookbook_name}::#{recipe_name} and current disk count #{node[:oracle][:asm][:test]}")
bash "beforeTest" do
code lazy{ echo #{node[:oracle][:asm][:test]} }
end
ruby_block "test current disk count" do
block do
node.set[:oracle][:asm][:test] = "#{node[:oracle][:asm][:test]}".to_i+1
end
end
bash "test" do
code lazy{ echo #{node[:oracle][:asm][:test]} }
end
However I'm still getting the error bellow:
NoMethodError ------------- undefined method echo' for Chef::Resource::Bash
Cookbook Trace: ---------------
/var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb:3:in block (2 levels) in from_file'
Resource Declaration: ---------------------
# In /var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb
1: bash "beforeTest" do
2: code lazy{
3: echo "#{node[:oracle][:asm][:test]}"
4: }
5: end
Please can you help how lazy should be used in bash? If not lazy is there any other option?
bash "beforeTest" do
code lazy { "echo #{node[:oracle][:asm][:test]}" }
end
You should quote the command for the interpolation to work; if not, ruby would search for an echo command, which is unknown in ruby context (thus the error you get in log).
Warning: lazy has to be for the whole resource attribute; something like this WON'T work:
bash "beforeTest" do
code "echo node asm test is: #{lazy { node[:oracle][:asm][:test]} }"
end
The lazy evaluation takes a block of ruby code, as decribed here
You may have a better result with the log resource like this:
log "print before" do
message lazy { "node asm test is #{node[:oracle][:asm][:test]}" }
end
I've been drilling my head solving this problem until I came up with lambda expressions. But yet just using lambda didn't help me at all. So I thought of using both lambda and lazy evaluation. Though lambda is already lazy loading, when compiling chef recipe's, the resource where you call the lambda expression is still being evaluated. So to prevent it to being evaluated (somehow), I've put it inside a lazy evaluation string.
The lambda expression
app_version = lambda{`cat version`}
then the resource block
file 'tmp/validate.version' do
user 'user'
group 'user_group'
content lazy { app_version.call }
mode '0755'
end
Hope this can help others too :) or if you have some better solution please do let me know :)

Ruby block comment and profile questions

I have written a Ruby version of Erik Demaine's (MIT) docdist8.py. This is available on github as docdist-v3.rb. I faced two weird kind of situations:
1) In the function inner_product there is a block comment:
Inner product between two vectors, where vectors
are repeated as dictionaries of (word, freq) pairs.
Example : inner_product({"and":3, "of":2, "the":5},
{"and":4, "in":1, "of":1, "this":2}) = 14.0
If I wrap this with =begin and =end there is no problem, but if I wrap it with triple double-quotes """, I get errors as follows:
./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
Example : inner_product({"and":3, "of":2, "the":5},
^
./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
Example : inner_product({"and":3, "of":2, "the":5},
^
./docdist-v3.rb:72: syntax error, unexpected kIN, expecting kEND
... {"and":4, "in":1, "of":1, "this":2}) = 14.0
^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
... {"and":4, "in":1, "of":1, "this":2}) = 14.0
^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
..."and":4, "in":1, "of":1, "this":2}) = 14.0
^
Are there rules / allowed entries for """ that are different from =begin and =end?
2) When I run my program with the time command it executes in about 0.3 seconds. However, if I put require 'profile' the time it takes becomes very high in comparison - 30 seconds. Hence I don't get the correct output at all. This doesn't seem to be the case with the original Python version, where it takes only a marginal extra time to profile. How do I get the same profile run in Ruby?
Note: The two files I used to run the Ruby program are t2.bobsey.txt and t3.lewis.txt. They are available at http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/dd_data.htm
1) Block comments always have the form:
=begin
Comment
=end
You are actually creating a string that is never used:
"""
Not a comment
"""
# => "\nNot a comment\n"
That's why you get an error when adding another quote and that's why the syntax highlighting renders them as strings.
2) It's slower with profiler but I get identical results:
ruby docdist-v3.rb t2.bobsey.txt t3.lewis.txt
File t2.bobsey.txt:262111 lines,49785 words,3354 distinct words
File t3.lewis.txt:1015474 lines,182355 words,8530 distinct words
The distance between the documents is: 0.574160 (radians)

Resources