cancel
Showing results for 
Search instead for 
Did you mean: 
hedai710
Mission Specialist
Mission Specialist
  • 1,135 Views

RHCSA II Section 1 2.2 first bash script

Jump to solution

I have this exactly as it says on #2  but I don't get any return command from bash  firstscript.sh  //   is there a typo in the command provided or??? am I a Potatoe

Create and execute a simple Bash script.

  1. Use the vim command to create the firstscript.sh file under your home directory.

    [student@servera ~]$ vim firstscript.sh
  2. Insert the following text, and save the file. The number of hash signs (#) is arbitrary.

    #!/usr/bin/bash
    echo "This is my first bash script" > ~/output.txt
    echo "" >> ~/output.txt
    echo "#####################################################" >> ~/output.txt
  3. Use the bash command to execute the script.

    [student@servera ~]$ bash firstscript.sh
11 Replies
Chetan_Tiwary_
Community Manager
Community Manager
  • 258 Views

@hedai710 you can try writing the same bash script in two steps :

1. first write it without the > or >> redirection part and run the script to see what is happening and 

2. write it with the > / >> as depicted in the course and run it to see the difference. 

 

Bash scripting is basically the commands that you write in the terminal with some logic applied to achieve a goal ! These commands can range from simple file operations to complex logic structures, enabling automation of tasks and system management.

So practice commands more with standard redirection techniques ( stdin , stdout and stderr )  and you will get the bash scripting ! 

0 Kudos
Joe_Wulf2112
Mission Specialist
Mission Specialist
  • 160 Views

In addition to all the great feedback already provided I'd like to say that 'seemingly' this exercise was relatively simple and trivial---yet it is foundational as a key building block for you or anyone who gets into Linux and who might be looking someday to leverage this concept to go further.  I believe it is presented in this form (simple) to illustrate this concept, which it seems you have gotten (kudos!!).

To demonstrate (hopefully helpfully) an expansion on this theme:  imagine you've got a 'requirement' to generate a bash shell script to execute on demand and it is to list the contents of several directories and place the contents into that 'output.txt' file; and it must have dividers visually between each listing.  Without getting complicated, or going off on tangents IRT various methods other than bash to accomplishing this, much less exotic fine points on various bash scripting techniques; you might put together something like this (as a contrived example to 'meet' the requirements):

#!/bin/bash

echo "" > ~output.txt

ls -la /var/log/audit >> ~output.txt

echo "##########" >> ~output.txt

echo "" >> ~output.txt

ls -la /someapplication/product/data42 >> ~output.txt

echo "##########" >> ~output.txt

echo "" >> ~output.txt

ls -la /someapplication/product/files_2112 >> ~output.txt

echo "##########" >> ~output.txt

 

While remaining overly simple it is still a semi-real-world example you might be required to craft and implement.  Then, this second-generation 'script' could be a launching point for developing a bigger (err, more complex) bash shell script to address different real-world problems.

YMMV, and I hope expanding on this is helpful to you.

Thank you.
R,
-Joe Wulf
Join the discussion
You must log in to join this conversation.