我想使用python3脚本生成一个“自解压” bash脚本,该脚本具有一个嵌入式.tar.gz存档作为有效载荷。
在bash中,我只会做这样的事情:
printf "#!/bin/bash
PAYLOAD_LINE=\`awk '/^__PAYLOAD_BELOW__/ {print NR + 1; exit 0; }' \$0\`
tail -n+\$PAYLOAD_LINE \$0 | tar -xvz
#script that does something with the unpacked payload
exit 0
__PAYLOAD_BELOW__\n" > "tmpfile"
cat "tmpfile" "payload.tar.gz" > "myscript.sh"
我在python中尝试过的是这样的:
with open('myscript.sh','wb') as script:
for line in open(payload.tar.gz, 'rb'):
script.write(line)
我可以使用cat myscript.sh | tar -xvz
手动解压缩生成的文件
要添加bash脚本部分(PAYLOAD_LINE= .... __PAYLOAD_BELOW__\n"
),是否有比第二次打开myscript.sh
而不是二进制('wb'
)模式更好的方式?>
答案 0 :(得分:0)
想通了。
我只需要在将字符串写入文件之前将字符串转换为字节,就不必将其打开两次。
CREATE TABLE test (
sensor_id uuid,
username text,
value double
)