将 bash 脚本与读取和 return 语句一起使用
Using bash script with read and return statement
我有以下脚本:
#!/bin/bash
#
# Example script for validating SVN credentials.
var_svn_user_name=
var_svn_password=
function get_svn_credentials()
{
# First, get the credentials from the user
read -r -p "Please enter SVN User Name: " var_svn_user_name
echo -n "Please enter SVN Password: "
read -r -s var_svn_password
echo ""
echo "----------------"
echo "The SVN User Name is: ${var_svn_user_name}"
echo "The SVN User Password is: ${var_svn_password}"
# Next, validate provided credentials
echo -n "Validating credentials... "
#var_ret=$(svn list --username "${var_svn_user_name}" --password \
# "${var_svn_password}" ${var_url} ${var_cfg} ${var_opt}=${var_val} \
# --no-auth-cache --non-interactive 2>&1 | grep "Authentication failed")
if [[ $var_ret == "" ]]
then
echo 'Success'
else
echo 'Failed'
fi
}
function main()
{
# EXAMPLE Call #1
#result=$(get_svn_credentials) # FAILURE
# EXAMPLE Call #2
get_svn_credentials # SUCCESS
echo "Return value is: $result"
if [[ $var_ret == "Success" ]]
then
echo "SVN User Name and Password was validated."
else
echo "SVN User Name and Password was NOT validated."
fi
}
main "$@"
为什么当我注释掉示例2和示例1时,直到执行读取才显示密码回显?
我正在尝试弄清楚如何使 return 语句像 C 函数样式 return 语句一样工作。
有人能帮忙吗?
您正在将提示写入标准输出,这是由命令替换捕获的。而是将其写入标准错误(就像 read -p
那样)。
function get_svn_credentials()
{
# First, get the credentials from the user
read -r -p "Please enter SVN User Name: " var_svn_user_name
echo -n "Please enter SVN Password: " >&2
read -r -s var_svn_password
{
echo ""
echo "----------------"
echo "The SVN User Name is: ${var_svn_user_name}"
echo "The SVN User Password is: ${var_svn_password}"
} >&2
# Next, validate provided credentials
echo -n "Validating credentials... "
#var_ret=$(svn list --username "${var_svn_user_name}" --password \
# "${var_svn_password}" ${var_url} ${var_cfg} ${var_opt}=${var_val} \
# --no-auth-cache --non-interactive 2>&1 | grep "Authentication failed")
if [[ $var_ret == "" ]]
then
echo 'Success'
else
echo 'Failed'
fi
}
就是说,不要依赖输出来确定它是否成功;只需使用退出状态。
get_svn_credentials () {
local user_name password
# First, get the credentials from the user
read -r -p "Please enter SVN User Name: " user_name
read -r -p "Please enter SVN Password: " -s password
{
echo ""
echo "----------------"
echo "The SVN User Name is: ${user_name}"
echo "The SVN User Password is: ${password}"
} >&2
# Next, validate provided credentials
# Let the exit status of grep -q be the exit status
# of the function
printf '%s\n' "Validating credentials... " >&2
svn list --username "${user_name}" \
--password "${password}" \
"${var_url}" ${var_cfg} "${var_opt}=${var_val}" \
--no-auth-cache --non-interactive 2>&1 |
grep -q "Authentication failed"
}
main () {
if get_svn_credentials
then
echo "SVN User Name and Password was validated."
else
echo "SVN User Name and Password was NOT validated."
fi
}
main
(注意:您应该 可能 引用 $var_cfg
,但它实际上可能是一个选项列表。在这种情况下,您应该使用数组相反,但由于无法单独从这段代码中分辨出来,所以我没有引用它。)
我有以下脚本:
#!/bin/bash
#
# Example script for validating SVN credentials.
var_svn_user_name=
var_svn_password=
function get_svn_credentials()
{
# First, get the credentials from the user
read -r -p "Please enter SVN User Name: " var_svn_user_name
echo -n "Please enter SVN Password: "
read -r -s var_svn_password
echo ""
echo "----------------"
echo "The SVN User Name is: ${var_svn_user_name}"
echo "The SVN User Password is: ${var_svn_password}"
# Next, validate provided credentials
echo -n "Validating credentials... "
#var_ret=$(svn list --username "${var_svn_user_name}" --password \
# "${var_svn_password}" ${var_url} ${var_cfg} ${var_opt}=${var_val} \
# --no-auth-cache --non-interactive 2>&1 | grep "Authentication failed")
if [[ $var_ret == "" ]]
then
echo 'Success'
else
echo 'Failed'
fi
}
function main()
{
# EXAMPLE Call #1
#result=$(get_svn_credentials) # FAILURE
# EXAMPLE Call #2
get_svn_credentials # SUCCESS
echo "Return value is: $result"
if [[ $var_ret == "Success" ]]
then
echo "SVN User Name and Password was validated."
else
echo "SVN User Name and Password was NOT validated."
fi
}
main "$@"
为什么当我注释掉示例2和示例1时,直到执行读取才显示密码回显?
我正在尝试弄清楚如何使 return 语句像 C 函数样式 return 语句一样工作。
有人能帮忙吗?
您正在将提示写入标准输出,这是由命令替换捕获的。而是将其写入标准错误(就像 read -p
那样)。
function get_svn_credentials()
{
# First, get the credentials from the user
read -r -p "Please enter SVN User Name: " var_svn_user_name
echo -n "Please enter SVN Password: " >&2
read -r -s var_svn_password
{
echo ""
echo "----------------"
echo "The SVN User Name is: ${var_svn_user_name}"
echo "The SVN User Password is: ${var_svn_password}"
} >&2
# Next, validate provided credentials
echo -n "Validating credentials... "
#var_ret=$(svn list --username "${var_svn_user_name}" --password \
# "${var_svn_password}" ${var_url} ${var_cfg} ${var_opt}=${var_val} \
# --no-auth-cache --non-interactive 2>&1 | grep "Authentication failed")
if [[ $var_ret == "" ]]
then
echo 'Success'
else
echo 'Failed'
fi
}
就是说,不要依赖输出来确定它是否成功;只需使用退出状态。
get_svn_credentials () {
local user_name password
# First, get the credentials from the user
read -r -p "Please enter SVN User Name: " user_name
read -r -p "Please enter SVN Password: " -s password
{
echo ""
echo "----------------"
echo "The SVN User Name is: ${user_name}"
echo "The SVN User Password is: ${password}"
} >&2
# Next, validate provided credentials
# Let the exit status of grep -q be the exit status
# of the function
printf '%s\n' "Validating credentials... " >&2
svn list --username "${user_name}" \
--password "${password}" \
"${var_url}" ${var_cfg} "${var_opt}=${var_val}" \
--no-auth-cache --non-interactive 2>&1 |
grep -q "Authentication failed"
}
main () {
if get_svn_credentials
then
echo "SVN User Name and Password was validated."
else
echo "SVN User Name and Password was NOT validated."
fi
}
main
(注意:您应该 可能 引用 $var_cfg
,但它实际上可能是一个选项列表。在这种情况下,您应该使用数组相反,但由于无法单独从这段代码中分辨出来,所以我没有引用它。)