Hello Everyone!
This is my first time using the Community and I'm hoping I can get aquainted well and get some help with my Red Hat course. I'm new to Red Hat and all things System Administration. I might have some very basic questions but at this stage there's a lot I do not understand. I've attached a picture of a guided exercise I am trying to complete and I am stuck on Instruction Step # 2. If you're familiar with the Red Hat Academy 124/134 courses or simply the content please chime in!
My problem: In step 2, I don't understand if I am supposed to create the "for" loop on the command line or create it in a new file. The exercise shows the step at the cmd line but when I did this I received an error. I'll attach that as well. (Also note, step 3 goes on to actually create a .sh script) and I understand that step in the exercise. I do not understand step 2 and the proper way to complete that.
If I can get past step # 2 I can complete the exercise. The other steps make sense to me.
Thank you!
Errors I received in the guided exercise when trying to complete step 2.
I will confess being confused by the advice offered so far, so I'll take a stab at helping.
"The problem"- a syntax error in the for statement as-entered. To help find the problem in the syntax, I'll recommend pulling up the man page on bash, and searching for the phrase "for name" (the command man bash will open the man page for bash, pressing / will open search, and typing in "for name" (without the quotes) will find the section of the bash man page that addresses for loops using named elements). In short, you're missing a key word in the syntax of the for loop when naming a variable (HOST) and a list of elements to be processed in that variable space (servera serverb).
Read on for more about what (I suspect) is happening in the examples shown.
The textbook struggles with depicting 2 related concepts that came into play:
Thus, to follow the text exactly, type in the command shown, and PRESS ENTER each time the command is shown on a newline (not depicted, because efforts to depcit pressing the enter key, as done in past editions, confuses students who think they need to type "enter"). Then type in the next line (at the secondary prompt - also not depicted in the text, for the same reason) and press ENTER. The last line shown (done) will close out the requirements for the command sequence, and the command will actually execute when ENTER is pressed.
We do not need to use the escape character in this example to achieve the newline secondary prompt behaviour depicted elsewhere in the textbooks because it automatically happens when bash sees the partial "for-do-done" sequence.
I hope this is helpful to you @TechBAB
Hello TechBAB,
Welcome to the community!!!
You've come to the right place to get assistance. I say that because there's a wealth of knowledge in the community, and the best thing about all that wealth is that everyone is so willing to share!!!
You referred to your question as "basic", and I'm sure you'll agree that the term is relative. I can assure you that once upon a time, this question that you refer to as "basic", was advanced-level to me. I say that to say, please don't ever feel that any query you may have is too elementary to bring to the community!!! I know in my case, the more queries you bring, the wider the smile on my face
Okay, now, let's get on to your query. The problem that you're having is with the syntax of the command within your loop. The command that you're having your loop to execute is:
ssh student@${HOST} hostname
That one word that I bolded - hostname - is where your issue lies!!!
The proper syntax for the command should be: ssh student@${HOST}
The proper, generic syntax is: ssh username@remote_hostname
where
- username is the name of the account on the remote host that you're logging into
- remote_hostname is the name of the remote Linux system that you're connecting to
Just for completeness, that '@' character is used to separate the username from
the name of the remote host.
By appending hostname to the end of the command, what you're attempting to
do is include a piece of information that the ssh command does not know what to
do with.
I want to ask you a question, and please don't feel embarrassed or ashamed if you
don't know - I will NEVER ask a question to do either!!!!! My only purpose for being
out here is to elevate knowledge where I can!!! There's NEVER a reason to feel
ashamed about not knowling something. There's so much that I don't know about
Linux, I don't even know how much that is
I do in the community - so that I can continue to advance the needle!
My question is, are you aware that "hostname" is a Linux command?
I'll stop here for now, and just summarize the issue: The word "hostname"
should NOT be included in that ssh command!
After you've had a chance to digest this, let's talk some more if you feel the need.
Remember, we (the community and myself) want your questions!!!
We are MUCH stronger together, than we are alone!!!!
I will confess being confused by the advice offered so far, so I'll take a stab at helping.
"The problem"- a syntax error in the for statement as-entered. To help find the problem in the syntax, I'll recommend pulling up the man page on bash, and searching for the phrase "for name" (the command man bash will open the man page for bash, pressing / will open search, and typing in "for name" (without the quotes) will find the section of the bash man page that addresses for loops using named elements). In short, you're missing a key word in the syntax of the for loop when naming a variable (HOST) and a list of elements to be processed in that variable space (servera serverb).
Read on for more about what (I suspect) is happening in the examples shown.
The textbook struggles with depicting 2 related concepts that came into play:
Thus, to follow the text exactly, type in the command shown, and PRESS ENTER each time the command is shown on a newline (not depicted, because efforts to depcit pressing the enter key, as done in past editions, confuses students who think they need to type "enter"). Then type in the next line (at the secondary prompt - also not depicted in the text, for the same reason) and press ENTER. The last line shown (done) will close out the requirements for the command sequence, and the command will actually execute when ENTER is pressed.
We do not need to use the escape character in this example to achieve the newline secondary prompt behaviour depicted elsewhere in the textbooks because it automatically happens when bash sees the partial "for-do-done" sequence.
I hope this is helpful to you @TechBAB
One more thing I wanted to clarify about the guided exercise: when you create a "for" loop do you have to first create it at the CLI (as in Step 2), before you can go on to use it in a .sh script file? Or, is this more fluid and dependent on what you want the "for" loop to do? (ie in the guided exercise its connecting to a remote host). From the beginning, I didn't understand why Step 2 was needed since in Step 3.2 you go on to create a script file anyway.
This question is why I'm the student that needs guided instruction and why I struggle to be a self taught learner.. I'm good at studying but there are nuances that I need more instruction on in order to connect concepts and thus application.
More of the Exercise Steps
@TechBAB Please take a note of the Guided exercise objectives and expected outcomes mentioned at the top of the exercise.
Hence it instructs you to first do the for loop task from the command line at step 2 and then it instructs you to do the same task via a simple shell script.
Also notice how in step 1, the guided exercise tells you to test that you can run the hostname command from workstation using ssh successfully. Suppose if you get an error at this step - you cannot do the for loop task of the later steps. Guided exercise is a kind of self paced learning which has to include all logical steps in order to let someone new to this technology read, learn and apply the concepts.
So, in a nutshell - The guided exercise guides you to do certain tasks inorder to learn an objective - here it wants you to learn how to run for loop from the CLI terminal and then how to do the same using a shell script as well. In a real world scenario or say in an exam - you can use this learning to do the task either way - hence the learning.
To add to the other excellent responses, addressing this specifically:
"...there are nuances that I need more instruction on in order to connect concepts and thus application."
In addition to hitting the outcome metrics, and working through important concepts, this technique (doing the work 2x - once at the prompt, then again in the script) is prevelant in the real world - if you're working up a script, and need to test the steps, it's easy peasy to try them out right there in bash. That's how I view this - first, we test at the prompt to be sure we're getting the syntax right. Once we see that it works, we put that step into our script. This particular script is pretty easy, straightforward, and simple, but shell scripts can get very large, long, and complex. Breaking them down into smaller steps and building up the complexity in chunks is a great way to keep everything moving forward. As we mature, we need to do this less and less for concepts we use frequently, but the technique remains a solid method fo working through the intricacies or anything that's new to us or infrequently employed.
"...it reminded me of the <at> job cmd.. I think that's another instance where the prompt is a (>) arrow."
Students are typcially less confused by at because the prompt for it includes the at in the prompt, so it appears as "at>" in both the text and in the shell. It seems including those 2 extra tiny characters makes a huge difference for learners, and the confusion is less when depicted in the text than when attempting to convey "press enter", and "work at the secondary prompt" (>), and "escape pressing enter to put some of this really long command on another line" (\).
Welcome to the community. May your LInux journey be eternal and always improving.
Hello again Everyone
First, thank you so much for the responses. I was quite pleasantly surprised by the quick replies when I woke up Friday morning (and very eager to check them and apply the solutions). My college instructor introduced the Community to me/my class during a Linux course I just completed. He said very good things about it and stressed the importance of using it and how useful it is and unique to Linux. I was nervous about posting.. because it's not something I do much at all but I wanted to give it a try and use it as he suggested, instead of the usual route of reaching out to him for help. So I greatly appreciate it!
Side note: I have always heard people say "Google" is your best friend in IT and yes those things can be helpful but as someone newer in IT I always find myself down a dreaded rabbit hole of blog/discussion forums that don't give me the answer I need, or as easy as I need it! LOL.. so I'm growing in my use of how to use the internet as a tool in this career.
Finally, yes @Trevor I am familiar with the <hostname> cmd and I appreciate your welcome and thoroughness. That was awesome and so kind. I have "studied" Linux for about 1 year but have less hands on experience, so I'm familiar with a good breadth of cmds but not as proficient as I want to be at using them and when to apply them or when I get stumped.
@Andrew that was the solution that I needed and clearly a break in me knowing what to do next (simply press the Enter key) on my own; but I had a good idead of what you meant with your reply.. and it reminded me of the <at> job cmd.. I think that's another instance where the prompt is a (>) arrow.
I was able to complete the guided exercise!
Hooray for that. RH 134 removes the video instruction and that was soo helpful in RH 124 with the written step by step instructions (really good content for beginners). RH 134 also removes the ability to "grade" the guided exercises as a check while you work through it (smh lol). I guess its moving leaners toward applying the material independently and toward the real exam.
The end of chapter labs however omit steps and this makes it difficult if you've missed a key clue about using the CLI. I'm still working on the chapter lab. It combines multiple scripting techniques but I'm going to spend more time working on it this weekend and next few days. I may bring another post but I hope to be able to get through each chapter better and better by what I learn and pick up. I'm a huge note taker!
Thank you Community.
@TechBAB So now you must have got the answer from @Andrew who did a great job explaining the intricacies and the error. Let me try to simplify it for any learner trying to learn for loop.
The for loop iterates over a list of items and performs the given set of commands.
The Bash for loop takes the following form:
for item in [LIST] do [COMMANDS] done
in your first screenshot, you missed the keyword "in" - for loop requires the in keyword without it, Bash doesn’t know your host list, hence the error.
Now, read what @Andrew mentioned in the first point about secondary prompt : you can type the for loop syntax part by part as mentioned above :
or you can give the entire command in a single step/line also :
but here you must use ";" to
1. separate the commands/tasks to do from iteration list ( before beginning of keyword "do" ) and
2. to indicate the end of tasks and loop closing.
";" is nothing here but works same as new line with flash " >" cursor just as we see in the previous example.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.