提示用户不要执行bash命令替换 [英] Prompt user from bash command substitution
问题描述
我正在寻找一种单行代码,以提示用户进行多次输入并使用用户输入作为参数来执行命令.
I am looking for a one-liner to prompt user for multiple inputs and execute a command using the user inputs as arguments.
幼稚的方法是:
read -p shirt_size: shirt_size
read -p age: age
the_command some_complicated_arguments $shirt_size $age
不幸的是,它很冗长. 这是一种行为相同的单线纸:
Unfortunately it is verbose. Here is a one-liner that behaves the same:
the_command some_complicated_arguments \
`>&2 printf shirt_size:; head -n 1` `>&2 printf age:; head -n 1`
不幸的是,它既不紧凑也不可读. 我希望它具有如下可读性:
Unfortunately it is neither compact nor readable. I would like it to be as readable as:
the_command some_complicated_arguments \
`input shirt_size` `input age`
我希望该解决方案使用bash内置命令或程序,这些命令通常在Linux环境中可用.
I would prefer the solution to use bash built-in command or programs usually available on Linux environment.
推荐答案
听起来像是一项家庭作业问题,可让您了解功能.如果您有类似的功能:
Sounds like a homework question to get you to look at functions. If you had a function like:
input() { read -p "$1:" user_input; echo "$user_input"; }
然后您的行:
the_command some_complicated_arguments "$(input shirt_size)" "$(input age)"
将提示并打印整行.尽管这并不能检查输入是否有效.
Would prompt and print the complete line. Although this does nothing to check for valid input or no input.
这篇关于提示用户不要执行bash命令替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!