Define a multiline variable using catenate and dots without spaces

Hi,

i’m looking for a way to use variable and catenate it because my variable is a “sed” linux command which is quite long and i want to have it user-friendly readable.
My “sed” command does a modification in a file by finding a string and then adding 4 new lines.

As of now everything is working BUT i face a trailing “space” in my file because of the […] of catenate which add this space.

So my command is this:

${command}=    Catenate
...    sed -i '/Environment="XDG_RUNTIME_DIR=\\/run\\/user\\/1302"/a\Environment="QT_QPA_EGLFS_PHYSICAL_WIDTH=500"\\n
...    Environment="QT_QPA_EGLFS_PHYSICAL_HEIGHT=400"\\nEnvironment="QT_QPA_EGLFS_KMS_CONFIG=\/home\/ui\/display.json"\\n
...    Environment="QT_QPA_EGLFS_HIDECURSOR=0"' /lib/systemd/system/myfile.service

Now i use “Execute Command” from SSHLibrary to run this on my remote device (where i’m connected to using open connection and login with ssh key from SSHLibrary.

Now if i look on my “myfile.service” i see this:

Environment="XDG_RUNTIME_DIR=/run/user/1302"
Environment="QT_QPA_EGLFS_PHYSICAL_WIDTH=500"
 Environment="QT_QPA_EGLFS_PHYSICAL_HEIGHT=400"
Environment="QT_QPA_EGLFS_KMS_CONFIG=/home/ui/display.json"
 Environment="QT_QPA_EGLFS_HIDECURSOR=0"

So the sed worked fine. It found the line and using “/a” (for append) it appended the line.
Now my sed do a \n to create a new line to add it which works - BUT because of the […] dots on catenate there is a trailing “space” at the beginning of line 3 and line 5 which are present in the file.

So the problem is that after this line

/a\Environment="QT_QPA_EGLFS_PHYSICAL_WIDTH=500"\\n

we see the […] from catenate and this will add the space in next line that’s why the next line starts with a space in the file which you can see

And the same happens after this line:
Environment=“QT_QPA_EGLFS_KMS_CONFIG=/home/ui/display.json”\n

here we have again the […] which results in a trailing space in the file

What i can also do is to put the whole sed command as a one liner directly to Execute Command like this:

Execute Command    sed -i '/Environment="XDG_RUNTIME_DIR=\\/run\\/user\\/1302"/a\Environment="QT_QPA_EGLFS_PHYSICAL_WIDTH=500"\\nEnvironment="QT_QPA_EGLFS_PHYSICAL_HEIGHT=400"\\nEnvironment="QT_QPA_EGLFS_KMS_CONFIG=\/home\/ui\/display.json"\\nEnvironment="QT_QPA_EGLFS_HIDECURSOR=0"' /lib/systemd/system/myfile.service

This is also working fine WITHOUT spaces - but in my vscode this line is very long and not handy to read for others.

So any ideas?

For now i plan to just define 3 string variables and just use “Evaluate”

Br,
Camil

Not sure if it answers your question, but you could try using the SEPARATOR=[sep] feature to define how to join a string defined on multiple lines.

If you are using RF 7 this is compatible with the new VAR syntax (see here), but I would assume it works on older frameworks Set Test Variable & co.

Actually Catenate keyword also has a SEPARATOR argument.

Hi,

i tried with the SEPERATOR=\n - but this doesn’t helped me.

Maybe what i should mention is that i have defined my ${command} variable inside my Keyword - NOT in the Variables section.

So i moved my command to the Variables section and created it like this:

${sed_command}= 
...    sed -i '/Environment="XDG_RUNTIME_DIR=\\/run\\/user\\/1302"/a\Environment="QT_QPA_EGLFS_PHYSICAL_WIDTH=500"\\n
...    Environment="QT_QPA_EGLFS_PHYSICAL_HEIGHT=400"\\nEnvironment="QT_QPA_EGLFS_KMS_CONFIG=\/home\/ui\/display.json"\\n
...    Environment="QT_QPA_EGLFS_HIDECURSOR=0"' /lib/systemd/system/myfile.service

Then i called this in my keyword

Execute Command    ${sed_command}

But the result was the same. For the […] a space will be added as trailing space to the line in my file.

If you look at the documentation you can see that passing an empty separator does what you need.

Here is a screenshot of my favorite IDE :wink:

The same is applicable to multi-line variables in the Variables section.
And now the new VAR command marker also supports SEPARATOR.

Hi,

i tried it with SEPERATOR=
but no luck. Also if i define it in the *** Variables *** section.

From the log i see this:

INFO 	Executing command 'SEPERATOR= sed -i '/Environment="XDG_RUNTIME_DIR=\/run\/user\/1302"/aEnvironment="QT_QPA_EGLFS_PHYSICAL_WIDTH=500"\n Environment="QT_QPA_EGLFS_PHYSICAL_HEIGHT=400"\nEnvironment="QT_QPA_EGLFS_KMS_CONFIG=/home/ui/display.json"\n Environment="QT_QPA_EGLFS_HIDECURSOR=0"' /lib/systemd/system/myfile.service'.

If you look closer you see a space after “…500”\n Environment…"
And the second space you see after “…display.json”\n Environment"

This space is added TO the sed command and therefore sed will put it to my file
It looks like this

image

And now you see that 2 lines starts with a beginning space.

So for some reason the dots […] are turned into space and will be added to the “sed” command which ofc is not what i want.

Here is my complete example, for Robot Framework version 7.0.1:

*** Variables ***
${table_command}    sed -i '/Environment="XDG_RUNTIME_DIR=\\/run\\/user\\/1302"/a\Environment="QT_QPA_EGLFS_PHYSICAL_WIDTH=500"\\n
...               Environment="QT_QPA_EGLFS_PHYSICAL_HEIGHT=400"\\nEnvironment="QT_QPA_EGLFS_KMS_CONFIG=\/home\/ui\/display.json"\\n
...               Environment="QT_QPA_EGLFS_HIDECURSOR=0"' /lib/systemd/system/myfile.service
...               separator=

*** Test Cases ***
Using Step Variable
    ${command}=    Catenate    SEPARATOR=
    ...    sed -i '/Environment="XDG_RUNTIME_DIR=\\/run\\/user\\/1302"/a\Environment="QT_QPA_EGLFS_PHYSICAL_WIDTH=500"\\n
    ...    Environment="QT_QPA_EGLFS_PHYSICAL_HEIGHT=400"\\nEnvironment="QT_QPA_EGLFS_KMS_CONFIG=\/home\/ui\/display.json"\\n
    ...    Environment="QT_QPA_EGLFS_HIDECURSOR=0"' /lib/systemd/system/myfile.service
    Log    Variable is ${command}    html=True

Using Table Variable
    Log    Variable is ${table_command}    html=True

What is your html log showing here

and here

Do you see a space between \n and Environment?

because for me i see a space - see the red mark

im using
robot --version

Robot Framework 7.0 (Python 3.10.12 on linux)

Hi,

In your variable you wrote “SEPERATOR” with an e, so it’s not recognized and added to the variable.
You have to use SEPARATOR=

Regards
Charlie

2 Likes

The command you are using is for SSHLibrary, not variable definition.
Your command is defining a Linux variable, SEPARATOR and then running sed.
Did you tried my code?
See the user guide for the Variables section for Robot Framework versions older than 7.0.1.

1 Like

OMG !!!

you are right - wrong spelling.
Now using SEPARATOR=
it is working and the file was modified with sed as expected.

image

1 Like

Nice :wink:

Also even better, since v7.0 version and VAR implementation, I barely use Catenate keyword with SEPARATOR when no separator is needed.
You can define your variables, then create your final command one by adding them:

VAR    ${VAR1}   CONTENT1
VAR    ${VAR2}   CONTENT2
VAR    ${VAR3}   CONTENT3
VAR    ${CMD}    ${VAR1}${VAR2}${VAR3}

Regards
Charlie

Thx for the hint.
Yes it’s something i can use as well.

Br,
Camil