如何引用管道输入的参数?
例如,如果您有以下代码test.sh
#!/bin/bash
echo $1
当您运行./test.sh abcd.txt
时,您将在屏幕上显示abcd.txt。
但是当我尝试ls *.txt | ./test.sh
时,我的所有回声都是空行。
答案 0 :(得分:3)
您可以尝试使用xargs
:
find . -type f -name \*.txt -printf "%p\0" | xargs -0 -I xxx ./test.sh xxx
答案 1 :(得分:1)
数据"管道输入"是STDIN,但那些不是命令行参数。以下是打印STDIN每行的示例:
#!/usr/bin/env bash
while read line; do
echo "Line: $line"
done
Bash read
命令有许多选项。阅读手册页的SHELL BUILTIN COMMANDS
部分以获取更多信息。