Groovy gives error expecting EOF, found '?' # line 9, column 25 - groovy

I'm using following code to generate random number in Groovy. I can run it in e.g. Groovy Web Console (https://groovyconsole.appspot.com/) and it works, however it fails when I try to run it in Mule. Here is the code I use:
log.info ">>run"
Random random = new Random()
def ranInt = random.nextInt()
def ran = Math.abs(​ranInt)​%20​0;
log.info ">>sleep counter:"+flowVars.counter+" ran: "+ran
sleep(ran)
And here is an exception that gets thrown:
Caused by:
org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed: Script26.groovy: 9: expecting EOF, found '?' # line 9,
column 25. def ran = Math.abs(?400)?%20?0;
^
1 error

You have some extra unicode characters in line 4. If you convert it to hex you will get:
64 65 66 20 72 61 6e 20 3d 20 4d 61 74 68 2e 61 62 73 28 e2 80 8b 72 61 6e 49 6e 74 29 e2 80 8b 25 32 30 e2 80 8b 30 3b
Now if you convert this hex back to ascii, you will get:
def ran = Math.abs(​ranInt)​%20​0;
There is a character ​ added after first (, after ) and after first 0. If you remove it, your code will compile correctly.
Here is the hex of curated line:
64 65 66 20 72 61 6e 20 3d 20 4d 61 74 68 2e 61 62 73 28 72 61 6e 49 6e 74 29 25 32 30 30 3b
And the line itself:
def ran = Math.abs(ranInt)%200;

Related

Parsing linux color control sequences

I'm trying to render the output of a linux shell command in HTML. For example, systemctl status mysql looks like this in my terminal:
As I understand from Floz'z Misc I was expecting that the underlying character stream would contain control codes. But looking at it in say hexyl (systemctl status mysql | hexyl) I can't see any codes:
Looking near the bottom on lines 080 and 090 where the text "Active: failed" is displayed, I was hoping to find some control sequences to change the color to red. While not necessarily ascii, I used some ascii tables to help me:
looking at the second lot of 8 characters on line 090 where the letters ive: fa are displayed, I find:
69 = i
76 = v
65 = e
3a = :
20 = space
66 = f
61 = a
69 = i
There are no bytes for control sequences.
I wondered if hexyl is choosing not to display them so I wrote a Java program which outputs the raw bytes after executing the process as a bash script and the results are the same - no control sequences.
The Java is roughly:
p = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "systemctl status mysql"}); // runs in the shell
p.waitFor();
byte[] bytes = p.getInputStream().readAllBytes();
for(byte b : bytes) {
System.out.println(b + "\t" + ((char)b));
}
That outputs:
...
32
32
32
32
32
65 A
99 c
116 t
105 i
118 v
101 e
58 :
32
102 f
97 a
105 i
108 l
101 e
100 d
...
So the question is: How does bash know that it has to display the word "failed" red?
systemctl detects that the output is not a terminal, and it removes colors codes from the output.
Related: Detect if stdin is a terminal or pipe? , https://unix.stackexchange.com/questions/249723/how-to-trick-a-command-into-thinking-its-output-is-going-to-a-terminal , https://superuser.com/questions/1042175/how-do-i-get-systemctl-to-print-in-color-when-being-interacted-with-from-a-non-t
Tools sometimes (sometimes not) come with options to enable color codes always, like ls --color=always, grep --color=always on in case of systemd with SYSTEMD_COLORS environment variable.
What tool can I use to see them?
You can use hexyl to see them.
how does bash know that it has to mark the word "failed" red?
Bash is the shell, it is completely unrelated.
Your terminal, the graphical window that you are viewing the output with, knows to mark it red because of ANSI escape sequences in the output. There is no interaction with Bash.
$ SYSTEMD_COLORS=1 systemctl status dbus.service | grep runn | hexdump -C
00000000 20 20 20 20 20 41 63 74 69 76 65 3a 20 1b 5b 30 | Active: .[0|
00000010 3b 31 3b 33 32 6d 61 63 74 69 76 65 20 28 72 75 |;1;32mactive (ru|
00000020 6e 6e 69 6e 67 29 1b 5b 30 6d 20 73 69 6e 63 65 |nning).[0m since|
00000030 20 53 61 74 20 32 30 32 32 2d 30 31 2d 30 38 20 | Sat 2022-01-08 |
00000040 31 39 3a 35 37 3a 32 35 20 43 45 54 3b 20 35 20 |19:57:25 CET; 5 |
00000050 64 61 79 73 20 61 67 6f 0a |days ago.|
00000059

nodejs telnet websocket response as <Buffer>

I am getting a telnet response as showed below when tried with nodejs code. Actually it is in xml format. When I did directly call the telnet response, the correct xml format is delivering. Can someone plz help, why 'Buffer' response when calling via nodejs.
"Buffer 3c 6e 66 6c 2d 65 76 65 6e 74 3e 0d 0a 20 3c 67 61 6d 65 63 6f 64 65 20
3 6f 64 65 3d 22 32 30 31 37 31 31 33 30 30 30 36 22 20 67 6c 6f 62 61 6c 2d .. "

how to extract a number from text file (or do a query on a site to get that number to bat file

I'm trying to write a bat file that would use output of https://www.feathercoin.com/netstats/
I need current network difficulty
I used curl to extract the contents of the webpage and findstr to write output of a string that contains difficulty to a file:
here's how file looks=
Current Block Number: В  1971095
Current Hashrate in Kh/s: В  2714249.165
Next retarget block number: В  1971096
Blocks to next retarget: В  1
Current difficulty: В  24.29714508
Expected next difficulty: В  26.496224436026
Time to retarget in second: В  34.366666666667
Total number of Feathercoins: В  182244280
in hex:
3C 68 31 3E 3C 2F 68 31 3E 3C 70 3E 43 75 72 72 65 6E 74 20 42 6C 6F 63 6B 20 4E 75 6D 62 65 72 3A 20 C2 A0 20 31 39 37 31 30 39 35 3C 62 72 20 2F 3E 43 75 72 72 65 6E 74 20 48 61 73 68 72 61 74 65 20 69 6E 20 4B 68 2F 73 3A 20 C2 A0 20 32 37 31 34 32 34 39 2E 31 36 35 3C 62 72 20 2F 3E 4E 65 78 74 20 72 65 74 61 72 67 65 74 20 62 6C 6F 63 6B 20 6E 75 6D 62 65 72 3A 20 C2 A0 20 31 39 37 31 30 39 36 3C 62 72 20 2F 3E 42 6C 6F 63 6B 73 20 74 6F 20 6E 65 78 74 20 72 65 74 61 72 67 65 74 3A 20 C2 A0 20 31 3C 62 72 20 2F 3E 43 75 72 72 65 6E 74 20 64 69 66 66 69 63 75 6C 74 79 3A 20 C2 A0 20 32 34 2E 32 39 37 31 34 35 30 38 3C 62 72 20 2F 3E 45 78 70 65 63 74 65 64 20 6E 65 78 74 20 64 69 66 66 69 63 75 6C 74 79 3A 20 C2 A0 20 32 36 2E 34 39 36 32 32 34 34 33 36 30 32 36 3C 62 72 20 2F 3E 54 69 6D 65 20 74 6F 20 72 65 74 61 72 67 65 74 20 69 6E 20 73 65 63 6F 6E 64 3A 20 C2 A0 20 33 34 2E 33 36 36 36 36 36 36 36 36 36 36 37 3C 62 72 20 2F 3E 54 6F 74 61 6C 20 6E 75 6D 62 65 72 20 6F 66 20 46 65 61 74 68 65 72 63 6F 69 6E 73 3A 20 C2 A0 20 31 38 32 32 34 34 32 38 30 0A
further i tried
FOR /f "usebackq tokens=1-10" %%I in ("b.txt") do (
SET I=%%I && SET J=%%J && SET K=%%K && SET L=%%L && SET M=%%M && CALL :ECHO %%I
)
That does not get me anywhere any suggestions on how to get current difficulty to a %%I would be appreciated!
If i can get feathercoin difficulty to a variable i'd be able to control which miner to use depending on the network difficulty of the feather coin thanks in advance!!!
I suggest following code for file b.txt being UTF-8 encoded and containing a non-breaking space on each line:
#echo off
for /F "tokens=1-3*" %%A in ('type "b.txt"') do (
if /I "%%A %%B" == "Current difficulty:" (
set "CurrentDifficulty=%%D"
goto HaveDifficulty
)
)
echo Upps, could not find current difficulty.
goto :EOF
:HaveDifficulty
echo Current difficulty is: %CurrentDifficulty%
The file b.txt is not processed directly by FOR line by line because FOR fails to correct process Unicode text files. Instead the command TYPE is used to output the content of this file in ANSI (or more precise in OEM) in a separate command process started by FOR in background with cmd /C and captured by FOR.
Each line of captured output not being empty or starting with a semicolon is split up by FOR into four substrings (tokens) using the default delimiters normal space and horizontal tab.
The first tab/space separated string is assigned to loop variable A which is on the line of interest the string Current. The second tab/space separated string difficulty: is assigned to next loop variable according to ASCII table which is B. The third string is the strange character being of no interest and assigned to loop variable C. And the fourth string is the rest of the line after tabs/spaces after third string which is on line of interest the string 24.29714508.
A case-insensitive IF condition is used to check if the first two substrings of current line concatenated with a space is Current difficulty:. If that condition is true, the right line is found in output of the file. For that reason the fourth string is assigned to environment variable CurrentDifficulty and the loop is exited without processing the other lines of captured output with a jump in batch file processing to a defined label.
It is expected by this batch file that the UTF-8 encoded file contains the bytes:
0000h: 43 75 72 72 65 6E 74 20 42 6C 6F 63 6B 20 4E 75 ; Current Block Nu
0010h: 6D 62 65 72 3A 20 C2 A0 20 31 39 37 31 30 39 35 ; mber:   1971095
0020h: 0D 0A 43 75 72 72 65 6E 74 20 48 61 73 68 72 61 ; ..Current Hashra
0030h: 74 65 20 69 6E 20 4B 68 2F 73 3A 20 C2 A0 20 32 ; te in Kh/s:   2
0040h: 37 31 34 32 34 39 2E 31 36 35 0D 0A 4E 65 78 74 ; 714249.165..Next
0050h: 20 72 65 74 61 72 67 65 74 20 62 6C 6F 63 6B 20 ; retarget block
0060h: 6E 75 6D 62 65 72 3A 20 C2 A0 20 31 39 37 31 30 ; number:   19710
0070h: 39 36 0D 0A 42 6C 6F 63 6B 73 20 74 6F 20 6E 65 ; 96..Blocks to ne
0080h: 78 74 20 72 65 74 61 72 67 65 74 3A 20 C2 A0 20 ; xt retarget:  
0090h: 31 0D 0A 43 75 72 72 65 6E 74 20 64 69 66 66 69 ; 1..Current diffi
00a0h: 63 75 6C 74 79 3A 20 C2 A0 20 32 34 2E 32 39 37 ; culty:   24.297
00b0h: 31 34 35 30 38 0D 0A 45 78 70 65 63 74 65 64 20 ; 14508..Expected
00c0h: 6E 65 78 74 20 64 69 66 66 69 63 75 6C 74 79 3A ; next difficulty:
00d0h: 20 C2 A0 20 32 36 2E 34 39 36 32 32 34 34 33 36 ;   26.496224436
00e0h: 30 32 36 0D 0A 54 69 6D 65 20 74 6F 20 72 65 74 ; 026..Time to ret
00f0h: 61 72 67 65 74 20 69 6E 20 73 65 63 6F 6E 64 3A ; arget in second:
0100h: 20 C2 A0 20 33 34 2E 33 36 36 36 36 36 36 36 36 ;   34.366666666
0110h: 36 36 37 0D 0A 54 6F 74 61 6C 20 6E 75 6D 62 65 ; 667..Total numbe
0120h: 72 20 6F 66 20 46 65 61 74 68 65 72 63 6F 69 6E ; r of Feathercoin
0130h: 73 3A 20 C2 A0 20 31 38 32 32 34 34 32 38 30 0D ; s:   182244280.
0140h: 0A ; .
But if the file does not even contain carriage return 0D and line-feed 0A, but is a single "line" with all the words and floating point numbers, the batch file above fails to extract the floating point number of current difficulty from this text file.
But the UTF-8 encoded b.txt contains in real the text:
Current Block Number:   1971095Current Hashrate in Kh/s:   2714249.165Next retarget block number:   1971096Blocks to next retarget:   1Current difficulty:   24.29714508Expected next difficulty:   26.496224436026Time to retarget in second:   34.366666666667Total number of Feathercoins:   182244280
The hexadecimal representation with file offset (left side) and Windows-1252 representation (right side) is:
0000h: 43 75 72 72 65 6E 74 20 42 6C 6F 63 6B 20 4E 75 ; Current Block Nu
0010h: 6D 62 65 72 3A 20 C2 A0 20 31 39 37 31 30 39 35 ; mber:   1971095
0020h: 43 75 72 72 65 6E 74 20 48 61 73 68 72 61 74 65 ; Current Hashrate
0030h: 20 69 6E 20 4B 68 2F 73 3A 20 C2 A0 20 32 37 31 ; in Kh/s:   271
0040h: 34 32 34 39 2E 31 36 35 4E 65 78 74 20 72 65 74 ; 4249.165Next ret
0050h: 61 72 67 65 74 20 62 6C 6F 63 6B 20 6E 75 6D 62 ; arget block numb
0060h: 65 72 3A 20 C2 A0 20 31 39 37 31 30 39 36 42 6C ; er:   1971096Bl
0070h: 6F 63 6B 73 20 74 6F 20 6E 65 78 74 20 72 65 74 ; ocks to next ret
0080h: 61 72 67 65 74 3A 20 C2 A0 20 31 43 75 72 72 65 ; arget:   1Curre
0090h: 6E 74 20 64 69 66 66 69 63 75 6C 74 79 3A 20 C2 ; nt difficulty: Â
00a0h: A0 20 32 34 2E 32 39 37 31 34 35 30 38 45 78 70 ;   24.29714508Exp
00b0h: 65 63 74 65 64 20 6E 65 78 74 20 64 69 66 66 69 ; ected next diffi
00c0h: 63 75 6C 74 79 3A 20 C2 A0 20 32 36 2E 34 39 36 ; culty:   26.496
00d0h: 32 32 34 34 33 36 30 32 36 54 69 6D 65 20 74 6F ; 224436026Time to
00e0h: 20 72 65 74 61 72 67 65 74 20 69 6E 20 73 65 63 ; retarget in sec
00f0h: 6F 6E 64 3A 20 C2 A0 20 33 34 2E 33 36 36 36 36 ; ond:   34.36666
0100h: 36 36 36 36 36 36 37 54 6F 74 61 6C 20 6E 75 6D ; 6666667Total num
0110h: 62 65 72 20 6F 66 20 46 65 61 74 68 65 72 63 6F ; ber of Featherco
0120h: 69 6E 73 3A 20 C2 A0 20 31 38 32 32 34 34 32 38 ; ins:   18224428
0130h: 30 ; 0
For such a file a different approach is needed to get the floating point number of Current difficulty. I suggest to use JREPL.BAT written by Dave Benham being stored in current directory on execution of the batch file posted below.
#echo off
set "CurrentDifficulty="
for /F %%I in ('jrepl.bat "^.*Current difficulty:[^0-9.]+([0-9.]+).*$" "$1" /M /F b.txt') do set "CurrentDifficulty=%%I"
if not defined CurrentDifficulty (
echo Upps, could not find current difficulty.
goto :EOF
)
echo Current difficulty is: %CurrentDifficulty%
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
if /?
set /?
type /?

how to get textual stdout from console.log(require("child_process").execSync('ls'))?

How to print the stdout from console.log(require("child_process").execSync('ls'))
I tried in ts
import { execSync } from 'child_process';
console.log(execSync('ls -la'));
which was then compiled to js:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
console.log(child_process_1.execSync('ls -la'));
but when I run it I get only like buffer how to get the stdout?
$ node app.js
$ <Buffer 74 6f 74 61 6c 20 38 38 0a 64 72 77 78 72 2d 78 72 2d 78 20 20 31 31 20 74 6f 6d 65 72 2e 62 65 6e 64 61 76 69 64 20 20 73 74 61 66 66 20 20 20 20 33 ... >
What am i missing? how to get the textual stdout?
Your last line should be:
console.log(child_process_1.execSync('ls -la').toString());
execSync returns a buffer, simply call toString on the buffer to get the contents of the buffer as a string.

Python3: UnicodeEncodeError: 'ascii' codec can't encode character '\xfc'

I'am trying to get running a very simple example on OSX with python 3.5.1 but I'm really stucked. Have read so many articles that deal with similar problems but I can not fix this by myself. Do you have any hints how to resolve this issue?
I would like to have the correct encoded latin-1 output as defined in mylist without any errors.
My code:
# coding=<latin-1>
mylist = [u'Glück', u'Spaß', u'Ähre',]
print(mylist)
The error:
Traceback (most recent call last):
File "/Users/abc/test.py", line 4, in <module>
print(mylist)
UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 4: ordinal not in range(128)
How I can fix the error but still get something wrong with stdout (print):
mylist = [u'Glück', u'Spaß', u'Ähre',]
for w in mylist:
print(w.encode("latin-1"))
What I get as output:
b'Gl\xfcck'
b'Spa\xdf'
b'\xc4hre'
What 'locale' shows me:
LANG="de_AT.UTF-8"
LC_COLLATE="de_AT.UTF-8"
LC_CTYPE="de_AT.UTF-8"
LC_MESSAGES="de_AT.UTF-8"
LC_MONETARY="de_AT.UTF-8"
LC_NUMERIC="de_AT.UTF-8"
LC_TIME="de_AT.UTF-8"
LC_ALL=
What
-> 'python3' shows me:
Python 3.5.1 (default, Jan 22 2016, 08:54:32)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
Try running your script with explicitly defined PYTHONIOENCODING environment variable:
PYTHONIOENCODING=utf-8 python3 script.py
Remove the characters < and >:
# coding=latin-1
Those character are often used in examples to indicate where the encoding name goes, but the literal characters < and > should not be included in your file.
For that to work, your file must be encoded using latin-1. If your file is actually encoded using utf-8, the encoding line should be
# coding=utf-8
For example, when I run this script (saved as a file with latin-1 encoding):
# coding=latin-1
mylist = [u'Glück', u'Spaß', u'Ähre',]
print(mylist)
for w in mylist:
print(w.encode("latin-1"))
I get this output (with no errors):
['Glück', 'Spaß', 'Ähre']
b'Gl\xfcck'
b'Spa\xdf'
b'\xc4hre'
That output looks correct. For example, the latin-1 encoding of ü is '\xfc'.
I used my editor to save the file with latin-1 encoding. The contents of the file in hexadecimal are:
$ hexdump -C codec-question.py
00000000 23 20 63 6f 64 69 6e 67 3d 6c 61 74 69 6e 2d 31 |# coding=latin-1|
00000010 0a 0a 6d 79 6c 69 73 74 20 3d 20 5b 75 27 47 6c |..mylist = [u'Gl|
00000020 fc 63 6b 27 2c 20 75 27 53 70 61 df 27 2c 20 75 |.ck', u'Spa.', u|
00000030 27 c4 68 72 65 27 2c 5d 0a 70 72 69 6e 74 28 6d |'.hre',].print(m|
00000040 79 6c 69 73 74 29 0a 0a 66 6f 72 20 77 20 69 6e |ylist)..for w in|
00000050 20 6d 79 6c 69 73 74 3a 0a 20 20 20 20 70 72 69 | mylist:. pri|
00000060 6e 74 28 77 2e 65 6e 63 6f 64 65 28 22 6c 61 74 |nt(w.encode("lat|
00000070 69 6e 2d 31 22 29 29 0a |in-1")).|
00000078
Note that the first byte (represented in hexadecimal) in the third line (i.e. the character at position 0x20) is fc. That is the latin-1 encoding of ü. If the file was encoded using utf-8, the character ü would be represented using two bytes, c3 bc.
If you are facing this problem while reading/writing a file, then try this
import codecs
# File read
with codecs.open(filename, 'r', encoding='utf8') as f:
text = f.read()
# File write
with codecs.open(filename, 'w', encoding='utf8') as f:
f.write(text)

Resources