在 bash 中,如何处理命令行上的所有用户输入
In bash, how to process all user input on command line
如何将命令行上的所有用户输入作为程序的标准输入?
就我而言,我想替换用户输入的某些单词。例如,每次用户使用 animal1
这个词时,我希望它被接收为 goldfish
。所以它看起来像这样:
$ animal1
goldfish: command not found
我尝试了以下 bash 命令
while read input
do
sed "s/animal2/zebra/g;s/animal1/goldfish/g" <<< "$input"
done
但是它提示用户输入并且不会return到bash。我希望它在使用 bash 命令行时 运行。
此外,这让我只能捕获输出。
bash | sed 's/animal2/zebra/g;s/animal1/goldfish/g'
但不是用户输入。
如果我没理解错的话,听起来你只需要设置一些别名:
$ alias animal1=goldfish
$ animal1
bash: goldfish: command not found
这允许 shell 像往常一样以交互方式使用,但会进行您想要的替换。
您可以将此别名定义添加到您的启动文件之一,通常是 ~/.bashrc
或 ~/.profile
,以使它们对您打开的任何新 shell 生效。
Tom Fenech 提供的解决方案很好,但是,如果您打算向命令添加更多功能,您可以使用如下函数:
animal1() {
echo "Welcome to the new user interface!"
goldfish
# other commands
}
并放入用户~/.bashrc
或~/.bash_profile
输出将是:
$>animal1
Welcome to the new user interface!
-bash: goldfish: command not found
例如,通过使用这种方法,您可以创建自定义输出消息。在下面的代码片段中,我从命令中取出 return 值并逐字处理它。然后我删除输出的 -bash:
部分并重建消息并输出它。
animal1() {
echo "Welcome to the new user interface!"
retval=$(goldfish 2>&1)
# Now retval stores the output of the command glodfish (both stdout and stderr)
# we can give it directly to the user
echo "Default return value"
echo "$retval"
echo
# or test the return value to do something
# here I build a custom message by removing the bash part
message=""
read -ra flds <<< "$retval"
for word in "${flds[@]}" #extract substring from the line
do
# remove bash
msg="$(echo "$word" | grep -v bash)"
# append each word to message
[[ msg ]] && message="$message $msg"
done
echo "Custom message"
echo "$message"
echo
}
现在输出为:
Welcome to the new user interface!
Default return value
-bash: goldfish: command not found
Custom message
goldfish: command not found
如果您评论与默认 return 值相呼应的行,那么您将得到您要求的输出。
如何将命令行上的所有用户输入作为程序的标准输入?
就我而言,我想替换用户输入的某些单词。例如,每次用户使用 animal1
这个词时,我希望它被接收为 goldfish
。所以它看起来像这样:
$ animal1
goldfish: command not found
我尝试了以下 bash 命令
while read input
do
sed "s/animal2/zebra/g;s/animal1/goldfish/g" <<< "$input"
done
但是它提示用户输入并且不会return到bash。我希望它在使用 bash 命令行时 运行。
此外,这让我只能捕获输出。
bash | sed 's/animal2/zebra/g;s/animal1/goldfish/g'
但不是用户输入。
如果我没理解错的话,听起来你只需要设置一些别名:
$ alias animal1=goldfish
$ animal1
bash: goldfish: command not found
这允许 shell 像往常一样以交互方式使用,但会进行您想要的替换。
您可以将此别名定义添加到您的启动文件之一,通常是 ~/.bashrc
或 ~/.profile
,以使它们对您打开的任何新 shell 生效。
Tom Fenech 提供的解决方案很好,但是,如果您打算向命令添加更多功能,您可以使用如下函数:
animal1() {
echo "Welcome to the new user interface!"
goldfish
# other commands
}
并放入用户~/.bashrc
或~/.bash_profile
输出将是:
$>animal1
Welcome to the new user interface!
-bash: goldfish: command not found
例如,通过使用这种方法,您可以创建自定义输出消息。在下面的代码片段中,我从命令中取出 return 值并逐字处理它。然后我删除输出的 -bash:
部分并重建消息并输出它。
animal1() {
echo "Welcome to the new user interface!"
retval=$(goldfish 2>&1)
# Now retval stores the output of the command glodfish (both stdout and stderr)
# we can give it directly to the user
echo "Default return value"
echo "$retval"
echo
# or test the return value to do something
# here I build a custom message by removing the bash part
message=""
read -ra flds <<< "$retval"
for word in "${flds[@]}" #extract substring from the line
do
# remove bash
msg="$(echo "$word" | grep -v bash)"
# append each word to message
[[ msg ]] && message="$message $msg"
done
echo "Custom message"
echo "$message"
echo
}
现在输出为:
Welcome to the new user interface!
Default return value
-bash: goldfish: command not found
Custom message
goldfish: command not found
如果您评论与默认 return 值相呼应的行,那么您将得到您要求的输出。