我需要通过管道传输一个已拆分为各个参数的字符串。例如,
cd foo | cat bar.txt
已被解析并存储到数组中
char *arr[];
arr = {"cd", "foo", "|", "cat", "bar.txt"}
我最初的想法是遍历数组,直到看到管道并将命令存储到另一个数组中,然后运行它。还有其他方法可以解决这个问题吗?
最佳答案
你可以使用 awk :
str="cd foo | cat bar.txt"
command_first=echo $str | awk -F“|” '{print $1}'
command_second=echo $str | awk -F“|” '{print $2}'
评估 $command_first
评估 $command_second
关于c - 如何通过管道传输用户输入的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29176144/