0

I have the following script:

#!/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 "$@"

Why is it that when I comment out Example 2 and uncomment Example 1, the echoing of password does not get displayed until read executes?

I am trying to figure out how to get the return statement to work like a C function style return statement.

Would anyone be able to help with this?

1
  • What return statement are you talking about? Commented Sep 25, 2018 at 16:38

1 Answer 1

2

You are writing your prompt to standard output, which is captured by the command substitution. Write it to standard error instead (like read -p does).

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
}

That said, don't rely on the output to determine if it succeeded or not; just use the exit status.

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

(Note: you should probably be quoting $var_cfg, but it's possible it is actually a list of options. In that case, you should be using an array instead, but as it's impossible to tell from this code alone, I've left it unquoted.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you this looks great. The curly braces with echo block made the script continue to display "Please enter SVN Password" when I uncommented "EXAMPLE Call 1" in my original post. I wonder if there is is defacto book about scripting and styling, the bash script is so flexible it is hard to understand what it is doing sometimes.
The curly braces just define a command block, so I could redirect the output of all for echo commands to standard error (with >&2) at once instead of redirecting each of the four separately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.