命令替换为管道
Command substitution into pipe
我有一个功能
function list_all () {
output=$(sort lastb.txt | tail +2 | head -n -2 | sort | uniq -f3 | tr -s " " | cut -d' ' -f1,3,5,6)
echo "$output"
}
我还有第二个功能
function filter_username () {
read -p "Enter filter: " filter
output=$(list_all) | grep "^$filter")
echo "$output"
}
是否可以将 list_all
的输出分配到 grep
的 output
变量?这样我就不必重复我在 list_all
?
中所做的全部工作
list_all
是一个写入标准输出的函数;因此,它可以单独用作 grep
的输入。
filter_username() {
read -p "Enter filter: " filter
output=$(list_all | grep "^$filter")
echo "$output"
}
请注意,如果您只想立即将 output
的内容写入标准输出而不对其进行任何其他操作,则在任何一种情况下都不需要命令替换。
list_all () {
sort lastb.txt | tail +2 | head -n -2 | sort | uniq -f3 | tr -s " " | cut -d' ' -f1,3,5,6
}
filter_username () {
read -p "Enter filter: " filter
list_all | grep "^$filter"
}
我有一个功能
function list_all () {
output=$(sort lastb.txt | tail +2 | head -n -2 | sort | uniq -f3 | tr -s " " | cut -d' ' -f1,3,5,6)
echo "$output"
}
我还有第二个功能
function filter_username () {
read -p "Enter filter: " filter
output=$(list_all) | grep "^$filter")
echo "$output"
}
是否可以将 list_all
的输出分配到 grep
的 output
变量?这样我就不必重复我在 list_all
?
list_all
是一个写入标准输出的函数;因此,它可以单独用作 grep
的输入。
filter_username() {
read -p "Enter filter: " filter
output=$(list_all | grep "^$filter")
echo "$output"
}
请注意,如果您只想立即将 output
的内容写入标准输出而不对其进行任何其他操作,则在任何一种情况下都不需要命令替换。
list_all () {
sort lastb.txt | tail +2 | head -n -2 | sort | uniq -f3 | tr -s " " | cut -d' ' -f1,3,5,6
}
filter_username () {
read -p "Enter filter: " filter
list_all | grep "^$filter"
}