argv vs bash扩展/命令替换/ python

问题描述

如果我的术语有些偏离,我会事先道歉,但我想弄清楚 为什么一件事起作用,而另一件事却没有。 我有这个程序,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char* argv[],char* envp[]){
    printf("argc: %d\n",argc);
    if(argc != 10) {
        printf("argc incorrect\n");
        return 0;
    }
    if(strcmp(argv[4],"\x00")) 
    {
        printf("argv[4] incorrect\n");
        return 0;
    }
    if(strcmp(argv[5],"\x20\x0a\x0d")) 
    {
        printf("argv[5] incorrect\n");
        return 0;
    };
    printf("yes\n");    
}

我已经尝试了一段时间,通过混合使用来传递strcmp部分 命令和/或进程替换和python的关系,但我不知道 如何进行这项工作,例如

./compartx $(python -c 'print("A " * 4)'; python -c 'print("\x00")'; python -c 'print("B " * 5)';)

[我正在尝试通过argv [4]的第一次检查,请不要理会argv [5] 暂时]

我确实看到警告“输入中的命令替换忽略了空字节”。有 网络上有很多帖子暗示不可能传递nullbyte 到argv,但是今天早上我浏览了bash手册, 使用此参数传递strcmp部分

./compartx {0..2} $'\0' $' \n\r' {0..3}

有什么方法可以使用python来实现相同的目的吗?

干杯, 斯文

解决方法

尝试在shell命令行中进行插值甚至没有任何意义。而且很可能反而不能一次插值!

如果需要使用Python生成参数,则也可以使用Python运行命令:

frames,channels,rows,columns = 5,3,224,224

video = Input(shape=(frames,columns,channels))

cnn_base = VGG16(input_shape=(rows,channels),weights="imagenet",include_top=True)
cnn_base.trainable = False

cnn = Model(cnn_base.input,cnn_base.layers[-3].output,name="VGG_fm")
encoded_frames = TimeDistributed(cnn,name = "encoded_frames")(video)
encoded_sequence = LSTM(256,name = "encoded_seqeunce")(encoded_frames)
hidden_layer = Dense(1024,activation="relu",name = "hidden_layer")(encoded_sequence)

encoding_input = Input(shape=(784,),name="Encoding",dtype='float') 
sentence_features = Dense(units = 60,name = 'sentence_features')(encoding_input)
x = concatenate([sentence_features,hidden_layer])
outputs = Dense(10,activation="softmax")(x)

model = Model([video,encoding_input],outputs) #<=== double input
model.summary()

运行它,您会得到

import subprocess

subprocess.run([
    './compartx','1','2','3',# strcmp will stop at the first null byte,so empty string will do here
    '','\x20\x0a\x0d','6','7','8','9'
])
,

无需发送NUL;只需传递空字符串。 strcmp始终以NUL个字节停止,C中的空字符串只是一个NUL;明确地传递额外的NUL不会改变行为(它会碰到第一个并停止)。