Stuck at 3.2.1 Passing parameters to a bash script

  1. After typing ./demo.sh
    what should I do next, in order to see the script?

  2. How do we see /home/simulations/ on Explorer? I can only see USER ?

  3. Where can we find the files: move_bb8_circle.py | move_bb8_forward_backward.py | move bb8_square.py?

Thanks.

Philip

Hi @MarkSix11,

Welcome to our community.

This error message happens because the script is expecting to have an argument.

In other words, you called ./demo.sh, but instead, you should call:

./demo.sh rotate

and then, after stopping the command above using CTRL+C, you could:

./demo.sh stop

This is basically the reason why you see this error message.

But I agree that the script has to be improved.

If you replace all $ARG1 in the code with "$ARG1", then this error also would be gone.

I agree that we should have put the commas in the script, or put an error message in case the user does not provide an argument.

I think this could also be a good exercise for you, what do you think?

You could replace the first lines with:

#!/bin/bash

ARG1=$1

if [ "$ARG1" == "" ]; then 
    echo "Please, provide an argument. E.g.: ./demo.sh rotate"
    exit 1
fi

Then the code will detect when someone calls the script without an argument.

Keep pushing your Linux learning.

Thanks Ralves.

The commands, such as ./demo.sh rotate, ./demo sh stop, and terms such as “argument” are not taught before moving on to work on the scripts in 3.2.1. What are the usage of those commands? Are there other Bash commands that I need to learn before working on the scripts?

Overall the materials are easy to follow until Section 3.2.1 where I start getting lost. I feel that I may have missed something after Exercise 3.3. but before Section 3.2.1. Can you help?

Missing|632x500

1 Like

Hi @MarkSix11 ,

the “arguments” are defined inside the demo.sh file itself. You, as the creator of a bash script, is the one who defines the arguments it accepts.

Let’s for example see the script below (let’s put the content below in a file named learning.sh):

#! /bin/bash

argument=$1

if [ "${argument}" == "marksix11" ]; then 
	echo "Congratlations, you passed the right argument."
else 
	echo "[Error]: Provided argument '${argument}' is not the expected one."
fi

If you just call it without arguments with ./learning.sh, it will print:

[Error]: Provided argument ‘’ is not the expected one.

If you pass “rotate” as argument, ./learning.sh rotate, it will print:

[Error]: Provided argument ‘rotate’ is not the expected one.

If you then max “marksix11” as argument, ./learning.sh marksix11, it will say you passed the correct argument:

Congratlations, you passed the right argument.

In this example, the script was expecting marksix11 as argument. It could be anything.

Please let me know if you still have some questions, but I aggree that we have to improve this demo.sh script.

Hi Ralves,

Thanks so much for your explanations. Very helpful :+1:

I now understand the bash syntax better, and completed Ex.3.4 with ease :tada:

With regards,
Philip

1 Like