Recent Posts

ads

Bash Script




A Bash script is a plain text file which contains a series of commands. These commands are a mixture of commands we would normally type ouselves on the command line (such as ls or cp for example) and commands we could type on the command line but generally wouldn't (you'll discover these over the next few pages). An important point to remember though is:
Anything you can run normally on the command line can be put into a script and it will do exactly the same thing. Similarly, anything you can put into a script can also be run normally on the command line 



Step 1: We will go to nano, our text editor

Step 2: We will type this :
 #!/bin/bash
echo "Hello Everybody"

Step 3: we will save it with the command CRTL+ O with the file name hello.sh and to exit we will use Ctrl + X

 and them we will get this


Step 4: After typing crtl + x  to exit   we will exit the  nano text editor. As for security reasons enforced by Linux distributions, files and scripts are not executable by default. 

However we can change that for our script using the chmod command in Linux CLI. Close the nano application and open a terminal.
 Now type the following command: chmod +x hello.sh


The line above sets the executable permission to the
hello.sh file. This procedure has to be done only once before running the script for the first time.


Step 5: To run the script, type the following command at the terminal:

./hello.sh

This will be the result




Let's have an extra  example. This time, we will incorporate displaying some system information by using the whoami and date commands to our hello script.
Open the hello.sh in our nano text editor ( go to nano and type ctrl + R) and we will edit type this:
#!/bin/bash
echo "Hello $(whoami) !"
echo "The date today is $(date)"
Save the changes we made in the script and run the script (Step 5 in the previous example) by typing:  ./hello.sh

After  saved it will looks like this:

We will exit using ctrl + x and them we will type ./hello.sh



In the previous example, the commands whoami and date were used inside the echo command. That means  that all utilities and commands available in the command line can also be used in shell scripts. 

Using "printf"

We have used echo to print strings and data from commands in our previous example. Echo is used to display a line of text. Another command that can be used to display data is the printf command. 
The printf controls and prints data like the printf function in C.
Below is the summary of the common printf controls:
Control Usage
\" Double quote
\\ Backslash
\b Backspace
\c Produce no further output
\e Escape
\n New Line
\r Carriage Return
\t Horizontal tab
\v Vertical Tab
Example3: We will open the previous hello.sh and change all echo to printf and run the script again. Notice what changes occur in our output.
#!/bin/bash
printf "Hello $(whoami) !"
printf "The date today is $(date)"

The difference is that echo sends a newline at the end of its output
Now, you have learned the basics of shell scripting and were able to create and run shell scripts. Next project of Linux Bash script you will learn about Linux Variable in a shell script.
 If you like this tutorial please click "like".
Thanks in advance! 😉














Bash Script Bash Script Reviewed by ohhhvictor on November 21, 2018 Rating: 5

No comments:

Facebook

ads
Powered by Blogger.