提示并返回输入的 shell 函数
我正在尝试编写一个在 bash 脚本中使用的辅助函数,以获取变量提示字符串并返回用户输入的值。我所拥有的只是等待用户输入值而不先显示提示,这令人费解。它还将回显存储在返回值 ($foo) 中,并且不保留使用 pid 变量读取的值。
!#/bin/bash
pid=0
promptValue() {
msg="$1"
echo -e "$msg"
read val
pid=$val
}
foo=$(promptValue "type something")
编辑: 对于将来可能想将其用于自己用途的任何人,这是完整的(功能)脚本,该脚本旨在发送电子邮件(在本例中为我的手机),让我知道长时间运行的进程何时完成。我确信必须有更好的方法来做到这一点,但是呃。 :-) (我这样写是为了在其他地方的 bash 函数库中使用。)
#!/bin/bash
promptValue() {
read -p "$1"": " val
echo $val
}
alertme() {
if [ -z "$email" ]; then
email=$(promptValue "Enter email")
fi
if [ -z "$email" ]; then
echo "ERROR: No email set!"
exit 1
fi
if [ -z "$pid" ]; then
pid=$(promptValue "Enter pid")
fi
if [ -z "$pid" ]; then
echo "ERROR: No pid set!"
exit 1
fi
ps -ef | grep $pid | grep -v grep > /dev/null 2>&1
while [ $? eq 0 ]; do
sleep 10
ps -ef | grep $pid | grep -v grep > /dev/null 2>&1
done
echo "Process Complete" | mailx -s "Process Complete" $email
}
alertme
再次感谢大家!
I'm trying to write a helper function to use in a bash script to take a variable prompt string and return the value the user inputs. What I have sits and waits for a user to input the value without displaying the prompt first, which is puzzling. It also stores the echo in the return value ($foo), and doesn't keep the value read in with the pid variable.
!#/bin/bash
pid=0
promptValue() {
msg="$1"
echo -e "$msg"
read val
pid=$val
}
foo=$(promptValue "type something")
EDIT:
For anyone who might want to use this in the future for their own use, this is the full (functional) script, which is designed to send an email (in this case to my cellphone) to let me know when a long running process completes. I'm sure there has to be a better way to do this, but meh. :-) (I wrote them like this to be used in a bash function library elsewhere.)
#!/bin/bash
promptValue() {
read -p "$1"": " val
echo $val
}
alertme() {
if [ -z "$email" ]; then
email=$(promptValue "Enter email")
fi
if [ -z "$email" ]; then
echo "ERROR: No email set!"
exit 1
fi
if [ -z "$pid" ]; then
pid=$(promptValue "Enter pid")
fi
if [ -z "$pid" ]; then
echo "ERROR: No pid set!"
exit 1
fi
ps -ef | grep $pid | grep -v grep > /dev/null 2>&1
while [ $? eq 0 ]; do
sleep 10
ps -ef | grep $pid | grep -v grep > /dev/null 2>&1
done
echo "Process Complete" | mailx -s "Process Complete" $email
}
alertme
Thanks again everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
函数的退出值与函数内执行的最后一个命令的值相同或由
return n
语句提供的值(其中n
的范围为 0-255)。如果您希望函数返回字符串或数值,请使用echo
。如果您想捕获变量中函数的输出,并且仍然能够在不捕获输出的情况下将输出发送给用户,请使用stderr
,这就是read -p
做。Functions' exit values are the same as that of the last command executed within the function or the value supplied by a
return n
statement wheren
is in the range 0-255. If you want the function to return a string or numeric value, you useecho
. If you want to capture the output of a function in a variable and still be able to send output to the user without having that captured, usestderr
which is whatread -p
does.正常的方法似乎是这样的:
$(...)
,“命令替换”,将“命令”的标准输出作为结果(所以它不会在您的终端上显示echo
等)。The normal way to do it would seem to be something like:
$(...)
, "command substitution", takes the "command"'s standard output as the result (so it doesn't show theecho
on your terminal, etc).显然,如果您直接执行而不是将其分配给 foo,这将起作用。如果您想捕获 val 的值,那么为什么不这样做:
obviously this will work if you execute directly instead assigning it to foo. If you are trying to capture the value of val then why noit just do: