How does user input get created when using the online editor?

Hello, I am brand new to this. I am trying to learn BASH scripting, and am trying the BASH excersise “two-fer”.

When I look at others’ solutions, they usually have something like:
if [ -z "$1" ]; Where is the value of $1 coming from? How can input be provided if there is no command line to run the .sh from? I would normally do: two-fer.sh Alice on the command line to provide a value to $1.

Please help!

Special Parameters from the manual is going to be where to find information about this.

It is the first argument passed to the command, used as input for your script.

Of course, you have already answered this with your “I would normally do:” example.

If you are having difficulty with an exercise, though, perhaps showing the code, answering what you expected to happen, but showing what is happening instead, can be helpful to understand what you are missing.

The unit tests do that for you. If you want to test it yourself with your own inputs, you’ll need to run it locally.

Thanks all for the replies. I have 2 questions:

  1. I did the following for the two-fer exercise. It runs perfectly locally, but fails all checks in the online editor. What am I doing wrong:?
read -p "What is the name?: " name

case $name in

("Do-yun") echo "\nOne for Do-yun, one for me" ;;
("Alice") echo "\nOne for Alice, one for me" ;;
("Bohdan") echo "\nOne for Bohdan, one for me" ;;
("Zaphod") echo "\nOne for Zaphod, one for me" ;;
("") echo "\nOne for you, one for me" ;;

esac


  1. How do you paste into the online editor? I have been unable to do it.

The tests invoke your program like this:

run bash two-fer.sh Alice

(where run is a special command for the bats test framework)

Your code is required to get the name from the positional parameters, not from stdin (which is where read gets it).

In the editor, you can read the tests in the Tests tab of the right-hand panel:

An individual test looks like this:

@test "a name given" {
  [[ $BATS_RUN_SKIPPED == "true" ]] || skip
  run bash two_fer.sh Alice
  assert_success
  assert_output "One for Alice, one for me."
}

That shows how your program gets invoked, and the assertions are that it exits with a success exit code, and that it outputs the expected output.

1 Like

I didn’t see that requirement anywhere. Where does it say that?
This is going to severely limit a beginner, since I don’t know much about using positional parameters.

Exercism uses test-driven development. The tests actually represent the requirements.

1 Like