epicwork
epicwork

Reputation: 59

Using Python, a command with new lines getting truncated

I have a FIO config which has numerous lines, I am using 'echo' to write the contents of the config to a new file.

config='''[global]
ioengine=libaio   ; async I/O engine for Linux
direct=1
randrepeat=0      ; use same seed for random io for better prediction
thread            ; use thread rather than process
group_reporting
; 
readwrite=randrw
percentage_random=100
iodepth=30
rwmixread=0
blocksize=3434
randrepeat=0
blockalign=3434
runtime=45454
time_based=1

[job 1]
filename=/dev/sdb
filesize=2934m
'''

I am trying the following:

cmd = '''echo "%s" > /tmp/fio.cfg''' % config

print(cmd)

But I keep getting back:

echo "[global]"

The lines after global are getting truncated. Any ideas are much appreciated!

Upvotes: 0

Views: 117

Answers (1)

9716278
9716278

Reputation: 2404

Bash and echo don't support multi-line strings. When you are formatting your echo command, the multiple lines are not going to be represented correctly when you run the command.

echo "[global]"

echo is only using the first line of the string that you constructed with %s ... % config, so config must only hold "[global]".

The solution is simple. You don't even have to use bash|shell to do this work, you can do it all form with-in python.


myconfig='''[global]
ioengine=libaio   ; async I/O engine for Linux
direct=1
randrepeat=0      ; use same seed for random io for better prediction
thread            ; use thread rather than process
group_reporting
; 
readwrite=randrw
percentage_random=100
iodepth=30
rwmixread=0
blocksize=3434
randrepeat=0
blockalign=3434
runtime=45454
time_based=1

[job 1]
filename=/dev/sdb
filesize=2934m"
'''

with open('/tmp/fio.cfg','w') as fo
    fo.write(myconfig)

Python does support multi line strings.

Alternatively in bash you could write you strings with \ chars where your line breaks are, this will tell bash to treat the next line as part of the preceding.

echo "This is a multi line string.\
 It it keeps going on and on."

You will get back:

This is a multi line string. It it keeps going on and on.

Ordinarily in bash scripts returns (new lines) denote the end of a line of insertion, so that when you progress to the next bash assumes that you wanted the behaviour that a ; would give you otherwise.

Adding \n new line and adding -e to echo will put the output on separate lines.

-e enable interpretation of backslash escapes

echo -e "This is a multi line string\
\nIt it keeps going on and on."

You then will get back:

This is a multi line string.
It it keeps going on and on.

Upvotes: 1

Related Questions