【问题标题】:Using bash script with read and return statement使用带有读取和返回语句的 bash 脚本
【发布时间】:2018-09-25 16:30:00
【问题描述】:

我有以下脚本:

#!/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 时,密码的回显直到 read 执行才显示?

我试图弄清楚如何让 return 语句像 C 函数样式的 return 语句一样工作。

有人可以帮忙吗?

【问题讨论】:

  • 你在说什么return声明?

标签: bash echo return-value


【解决方案1】:

您正在将提示写入标准输出,该输出由命令替换捕获。而是将其写入标准错误(就像 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,但它实际上可能是一个选项列表。在这种情况下,您应该使用数组,但因为不可能仅从这段代码来看,我没有引用它。)

【讨论】:

  • 谢谢,这看起来很棒。当我在原始帖子中取消注释“EXAMPLE Call 1”时,带有 echo 块的花括号使脚本继续显示“请输入 SVN 密码”。我想知道是否有关于脚本和样式的事实上的书,bash 脚本非常灵活,有时很难理解它在做什么。
  • 花括号只是定义了一个命令块,所以我可以一次将所有echo 命令的输出重定向到标准错误(使用>&2),而不是分别重定向四个。
猜你喜欢
  • 2014-01-30
  • 1970-01-01
  • 2022-07-27
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
相关资源
最近更新 更多