In this article, we will learn how to create a shell script by using the following methods
1.Static Shell script
2)Dynamic shell script
Static shell script: In static scripting, you can execute any no.of times and it generates the same output i.e it is not taking the runtime input values while executing your script.
Dynamic Shell script: It always takes the runtime input values, based on the different sets of input values it will generate different outputs.
Let us create one basic monitoring script by using the static method
Ex:1 Create a script to monitor your server performance
Method: Static
#vi /myscript.sh #!/bin/bash echo "Display the Memory usage details" echo "***********************************" free -m echo "Display the Server Load average and run time details" echo "************************************" uptime echo "Display the Cpu activity and RAM Details" echo "**************************************" cat /proc/meminfo echo "***************************************" :wq!
Now Add the Execute permission
#chmod u+x /myscript.sh
Run the script as follows
#./myscript.sh or sh /myscript.sh
Once you execute the script the interpreter reads each and every line from the file and displays the output on the console.
Sample Output:👇🏿👇🏿
The main advantage of using the shell programming is automatically it executes all your larger tasks, you can combine multiple commands in a single file that are required to monitor your server performance and by adding the execute permission you can run the script file as a program.
Ex:2 Display the Total Number of Lines in a file
Method: Static
when I run a script it should display the total number of lines in a file. As all we know in Linux we have many commands to get this from the command line arguments, the same task if I put it inside the script file how it works🤗
let us create the script file as follows
#vi /demo.sh #!/bin/bash echo "Display the Total Number of Lines in a file" cat /var/log/messages |wc -l echo "End Of Script" :wq!
Sample Output:👇🏿👇🏿
As you can see from the above output the file has a total number of 68 lines.
Ex:3 Create a script that tells the username and prints the running process
#vi /test.sh #!/bin/bash echo "Hello,$USER echo "Hey, I am " $USER " and I will show you the current running process details" echo "Display the Running processes details" ps :wq!
Sample Output:👇🏿👇🏿
As you can see from the above output the ps displays the running processes details from your current terminal
Note: To display all the running processes from all the terminal you must have to use the option -aux with the ps command.
Ex:4 Create a script to get the runtime input values from the keyboard(Dynamic Method)
In shells scripting, we use the command “read” to get the inputs from the keyboard, the read command what this does is wait for the user to type something followed by the enter key it stores that given input to the variable
Let me create a simple script to get the inputs from by using the read command
#vi /dynamic.sh #!/bin/bash echo "Hi there, What is your First Name?" read var1 echo "Welcome Mr/Mrs. $var1, Would you like to tell us the last name?" read var2 echo "Thanks, Mr/Mrs. $a $b for telling us your name" echo "*********************************" echo "Mr/Mrs, $b its time to say goodbye" :wq!
Sample Output:👇🏿👇🏿
Ex:5 Get the inputs from the keyboard
#vi /script.sh #!/bin/bash echo "Please type your name" read data echo "Hi $data, Let us be friends" :wq!
As you can see from the above output, read command takes the input which is assigned from the keyboard and then it stored the value inside the variable “data”, the last line prints the value we assigned .. next time again run the same script, give another input you will get different output this is why it is called as the dynamic scripting(i.e It always takes the runtime input values)
In our next article, I will share you more programs by using the “read command”.
Your Valuable “thoughts” in comments is highly appreciated, please do Like and share us to spread.
If you found this article useful, Kindly Subscribe here 👉 Click this link to Subscribe
Never miss an article Do like my official FB page 👉 Learn Linux in an easier way
For More videos Subscribe My Youtube Channel 👉🏻https://www.youtube.com/channel/UC6bR8In-jj9-klVlZQ6YH_A
Follow me at twitter @linuxvasanth