{"id":11984,"date":"2019-03-20T16:44:35","date_gmt":"2019-03-20T16:44:35","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11984"},"modified":"2019-03-20T16:44:35","modified_gmt":"2019-03-20T16:44:35","slug":"understand-linux-shell-and-basic-shell-scripting-language-tips-iiiiii-parts","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/20\/understand-linux-shell-and-basic-shell-scripting-language-tips-iiiiii-parts\/","title":{"rendered":"Understand Linux Shell and Basic Shell Scripting Language Tips (I,II,III parts)"},"content":{"rendered":"<p>Picture speak more than words and the below picture says all about the working of\u00a0<strong>Linux<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_3439\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/understand-linux-shell-and-basic-shell-scripting-language-tips\/understanding-linux-shell\/\" rel=\"attachment wp-att-3439\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3439\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/07\/Understanding-Linux-Shell.png\" alt=\"Understanding Linux Shell\" width=\"382\" height=\"387\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Understanding Linux Shell<\/p>\n<\/div>\n<p><strong>Read Also<\/strong><\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/basic-shell-programming-part-ii\/\" target=\"_blank\" rel=\"noopener\">5 Shell Scripts to Learn Shell Programming \u2013 Part II<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/sailing-through-the-world-of-linux-bash-scripting-part-iii\/\" target=\"_blank\" rel=\"noopener\">Sailing Through The World of Linux BASH Scripting \u2013 Part III<\/a><\/li>\n<\/ol>\n<h3>Understanding Linux Shell<\/h3>\n<ol>\n<li><strong>Shell<\/strong>: A Command-Line Interpretor that connects a user to\u00a0<strong>Operating System<\/strong>\u00a0and allows to execute the commands or by creating text script.<\/li>\n<li><strong>Process<\/strong>: Any task that a user run in the system is called a process. A process is little more complex than just a task.<\/li>\n<li><strong>File<\/strong>: It resides on hard disk (<strong>hdd<\/strong>) and contains data owned by a\u00a0<strong>user<\/strong>.<\/li>\n<li><strong>X-windows aka windows<\/strong>: A mode of Linux where screen (<strong>monitor<\/strong>) can be split in small \u201c<strong>parts<\/strong>\u201d called\u00a0<strong>windows<\/strong>, that allow a\u00a0<strong>user<\/strong>\u00a0to do several things at the same time and\/or switch from one task to another easily and view graphics in a nice way.<\/li>\n<li><strong>Text terminal<\/strong>: A monitor that has only the capability of displaying text stuff, no graphics or a very basic graphics display.<\/li>\n<li><strong>Session<\/strong>: Time between\u00a0<strong>logging<\/strong>\u00a0<strong>on<\/strong>\u00a0and\u00a0<strong>logging out<\/strong>\u00a0of the system.<\/li>\n<\/ol>\n<h3>Types of Shell on a Standard Linux Distribution<\/h3>\n<p><strong>Bourne shell<\/strong>\u00a0: The Bourne shell was one of the major shells used in early versions and became a de facto standard. It was written by\u00a0<strong>Stephen Bourne<\/strong>\u00a0at\u00a0<strong>Bell Labs<\/strong>. Every Unix-like system has at least one shell compatible with the Bourne shell. The Bourne shell program name is \u201c<strong>sh<\/strong>\u201d and it is typically located in the file system hierarchy at\u00a0<strong>\/bin\/sh<\/strong>.<\/p>\n<p><strong>C shell<\/strong>: The\u00a0<strong>C shell<\/strong>\u00a0was developed by\u00a0<strong>Bill Joy<\/strong>\u00a0for the\u00a0<strong>Berkeley Software Distribution<\/strong>. Its syntax is modelled after the\u00a0<strong>C<\/strong>\u00a0programming language. It is used primarily for interactive terminal use, but less frequently for scripting and operating system control.\u00a0<strong>C<\/strong>\u00a0shell has many interactive commands.<\/p>\n<h3>Beginning the Fun! (Linux Shell)<\/h3>\n<p>There exist thousands of commands for command-line user, how about remembering all of them? Hmmm! Simply you can not. The real power of computer is to ease the ease your work, you need to automate the process and hence you need scripts.<\/p>\n<p>Scripts are collections of commands, stored in a file. The shell can read this file and act on the commands as if they were typed at the keyboard. The shell also provides a variety of useful programming features to make scripts truly powerful.<\/p>\n<h3>Basics of Shell Programming<\/h3>\n<ol>\n<li>To get a Linux shell, you need to start a terminal.<\/li>\n<li>To see what shell you have,<strong>\u00a0run: echo $SHELL<\/strong>.<\/li>\n<li>In Linux, the dollar sign (<strong>$<\/strong>) stands for a shell variable.<\/li>\n<li>The \u2018<strong>echo<\/strong>\u2018 command just returns whatever you type in.<\/li>\n<li>The pipeline instruction (<strong>|<\/strong>) comes to rescue, when chaining several commands.<\/li>\n<li>Linux commands have their own syntax, Linux won\u2019t forgive you whatsoever is the mistakes. If you get a command wrong, you won\u2019t flunk or damage anything, but it won\u2019t work.<\/li>\n<li><strong>#!\/bin\/sh<\/strong>\u00a0\u2013 It is called shebang. It is written at the top of a shell script and it passes the instruction to the program\u00a0<strong>\/bin\/sh<\/strong>.<\/li>\n<\/ol>\n<h3>About shell Script<\/h3>\n<p>Shell script is just a simple text file with \u201c<strong>.sh<\/strong>\u201d extension, having executable permission.<\/p>\n<h4>Process of writing and executing a script<\/h4>\n<ol>\n<li>Open terminal.<\/li>\n<li>Navigate to the place where you want to create script using \u2018<strong>cd<\/strong>\u2018 command.<\/li>\n<li><strong>Cd<\/strong>\u00a0(enter) [This will bring the prompt at\u00a0<strong>Your home Directory<\/strong>].<\/li>\n<li>touch\u00a0<strong>hello.sh<\/strong>\u00a0(Here we named the script as\u00a0<strong>hello<\/strong>, remember the \u2018<strong>.sh<\/strong>\u2018 extension is compulsory).<\/li>\n<li>vi\u00a0<strong>hello.sh<\/strong>\u00a0(nano\u00a0<strong>hello.sh<\/strong>) [You can use your favourite editor, to edit the script].<\/li>\n<li><strong>chmod 744 hello.sh<\/strong>\u00a0(making the script executable).<\/li>\n<li><strong>sh hello.sh<\/strong>\u00a0or\u00a0<strong>.\/hello.sh<\/strong>\u00a0(running the script)<\/li>\n<\/ol>\n<h5>Writing your First Script<\/h5>\n<pre>#!\/bin\/bash\r\n# My first script\r\n\r\necho \"Hello World!\"<\/pre>\n<p>Save the above lines on a text file, make it executable and run it, as described above.<\/p>\n<h4>Sample Output<\/h4>\n<pre>Hello World!<\/pre>\n<p>In the above code.<\/p>\n<pre>#!\/bin\/bash (is the shebang.)\r\n# My first script (is comment, anything following '#' is a comment)\r\necho \u201cHello World!\u201d (is the main part of this script)<\/pre>\n<h5>Writing your Second Script<\/h5>\n<p>OK time to move to the next script. This script will tell you, your\u2019s \u201c<strong>username<\/strong>\u201d and list the running\u00a0<strong>processes<\/strong>.<\/p>\n<pre>#! \/bin\/bash\r\necho \"Hello $USER\"\r\necho \"Hey i am\" $USER \"and will be telling you about the current processes\"\r\necho \"Running processes List\"\r\nps<\/pre>\n<p>Create a file with above codes, save it to anything you want, but with extension \u201c<strong>.sh<\/strong>\u201c, make it executable and run it, from you terminal.<\/p>\n<h4>Sample Output<\/h4>\n<pre>Hello tecmint\r\nHey i am tecmint and will be telling you about the current processes\r\nRunning processes List\r\n  PID TTY          TIME CMD\r\n 1111 pts\/0    00:00:00 bash\r\n 1287 pts\/0    00:00:00 sh\r\n 1288 pts\/0    00:00:00 ps<\/pre>\n<p><strong>Was this cool<\/strong>? Writing script is as simple as getting an idea and writing pipelined commands. There are some restrictions, too. Shell scripts are excellent for concise filesystem operations and scripting the combination of existing functionality in filters and command line tools via pipes.<\/p>\n<p>When your needs are greater \u2013 whether in\u00a0<strong>functionality<\/strong>,\u00a0<strong>robustness<\/strong>,\u00a0<strong>performance<\/strong>,\u00a0<strong>efficiency<\/strong>\u00a0etc \u2013 then you can move to a more full-featured language.<\/p>\n<p>If you already know\u00a0<strong>C<\/strong>\/<strong>Perl<\/strong>\/<strong>Python<\/strong>\u00a0programming language or any other programming language, learning the scripting language won\u2019t be much difficult.<\/p>\n<h5>Writing your Third Script<\/h5>\n<p>Moving to, write our third and last script for this article. This script acts as an interactive script. Why don\u2019t you, yourself execute this simple yet interactive script and tell us how you felt.<\/p>\n<pre>#! \/bin\/bash\r\necho \"Hey what's Your First Name?\";\r\nread a;\r\necho \"welcome Mr.\/Mrs. $a, would you like to tell us, Your Last Name\";\r\nread b;\r\necho \"Thanks Mr.\/Mrs. $a $b for telling us your name\";\r\necho \"*******************\"\r\necho \"Mr.\/Mrs. $b, it's time to say you good bye\"<\/pre>\n<h4>Sample Output<\/h4>\n<pre>Hey what's Your First Name?\r\nAvishek\r\nwelcome Mr.\/Mrs. Avishek, would you like to tell us, Your Last Name\r\nKumar\r\nThanks Mr.\/Mrs. Avishek Kumar for telling us your name\r\n******************************************************\r\nMr.\/Mrs. Kumar, it's time to say you good bye<\/pre>\n<p>Well this is not an end. We tried to bring a taste of scripting to you. In our future article we will elaborate this scripting language topic, rather a never ending scripting language topic, to be more perfect.<\/p>\n<h1 class=\"post-title\">5 Shell Scripts for Linux Newbies to Learn Shell Programming \u2013 Part II<\/h1>\n<p>To Learn something you need to do it, without the fear of being unsuccessful. I believe in practicality and hence will be accompanying you to the practical world of Scripting Language.<\/p>\n<div id=\"attachment_3502\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/basic-shell-programming-part-ii\/shell-scripting-2\/\" rel=\"attachment wp-att-3502\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3502\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/07\/Shell-Scripting-2.png\" alt=\"Learn Basic Shell Scripting\" width=\"435\" height=\"321\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Learn Basic Shell Scripting<\/p>\n<\/div>\n<p>This article is an extension of our First article\u00a0<a href=\"https:\/\/www.tecmint.com\/understand-linux-shell-and-basic-shell-scripting-language-tips\/\" target=\"_blank\" rel=\"noopener\">Understand Linux Shell and Basic Shell Scripting \u2013 Part I<\/a>, where we gave you a taste of Scripting, continuing that we won\u2019t disappoint you in this article.<\/p>\n<h3>Script 1: Drawing a Special Pattern<\/h3>\n<pre>#!\/bin\/bash\r\nMAX_NO=0\r\necho -n \"Enter Number between (5 to 9) : \"\r\nread MAX_NO\r\nif ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then\r\n   echo \"WTF... I ask to enter number between 5 and 9, Try Again\"\r\n   exit 1\r\nfi\r\nclear\r\nfor (( i=1; i&lt;=MAX_NO; i++ )) do     for (( s=MAX_NO; s&gt;=i; s-- ))\r\n    do\r\n       echo -n \" \"\r\n    done\r\n    for (( j=1; j&lt;=i;  j++ ))     do      echo -n \" .\"      done     echo \"\" done ###### Second stage ###################### for (( i=MAX_NO; i&gt;=1; i-- ))\r\ndo\r\n    for (( s=i; s&lt;=MAX_NO; s++ ))\r\n    do\r\n       echo -n \" \"\r\n    done\r\n    for (( j=1; j&lt;=i;  j++ ))\r\n    do\r\n     echo -n \" .\"\r\n    done\r\n    echo \"\"\r\ndone\r\necho -e \"\\n\\n\\t\\t\\t Whenever you need help, Tecmint.com is always there\"<\/pre>\n<p>Most of the above \u2018<strong>key words<\/strong>\u2018 would be known to you and most of them are self explanatory. e.g.,\u00a0<strong>MAX<\/strong>\u00a0sets the maximum value of the variable, for is a loop and anything within the loop gets on executing again and again till the loop is valid for given value of input.<\/p>\n<h5>Sample Output<\/h5>\n<pre>[root@tecmint ~]# chmod 755 Special_Pattern.sh\r\n[root@tecmint ~]# .\/Special_Pattern.sh\r\nEnter Number between (5 to 9) : 6\r\n       .\r\n      . .\r\n     . . .\r\n    . . . .\r\n   . . . . .\r\n  . . . . . .\r\n  . . . . . .\r\n   . . . . .\r\n    . . . .\r\n     . . .\r\n      . .\r\n       .\r\n\r\n                         Whenever you need help, Tecmint.com is always there<\/pre>\n<p>If you are a little aware of any programming language, learning the above script is not difficult, even if you are new to computation, programming and Linux it is not going to be much difficult.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/Special_Pattern.sh\">Download Special_Pattern.sh<\/a><\/p>\n<h3>Script 2: Creating Colorful Script<\/h3>\n<p>Who says,\u00a0<strong>Linux is colorless<\/strong>\u00a0and boring, save the codes below to anything [<strong>dot<\/strong>]\u00a0<strong>sh<\/strong>, make it executable and Run it, don\u2019t forget to tell me how it was, Think what you can achieve, implementing it somewhere.<\/p>\n<pre>#!\/bin\/bash\r\nclear \r\necho -e \"33[1m Hello World\"\r\n# bold effect\r\necho -e \"33[5m Blink\"\r\n# blink effect\r\necho -e \"33[0m Hello World\"\r\n# back to normal\r\necho -e \"33[31m Hello World\"\r\n# Red color\r\necho -e \"33[32m Hello World\"\r\n# Green color\r\necho -e \"33[33m Hello World\"\r\n# See remaining on screen\r\necho -e \"33[34m Hello World\"\r\necho -e \"33[35m Hello World\"\r\necho -e \"33[36m Hello World\"\r\necho -e -n \"33[0m\"\r\n# back to normal\r\necho -e \"33[41m Hello World\"\r\necho -e \"33[42m Hello World\"\r\necho -e \"33[43m Hello World\"\r\necho -e \"33[44m Hello World\"\r\necho -e \"33[45m Hello World\"\r\necho -e \"33[46m Hello World\"\r\necho -e \"33[0m Hello World\"<\/pre>\n<p><strong>Note<\/strong>: Don\u2019t bother about the color code now, Those important to you will be at your tongue, gradually.<\/p>\n<p><strong>Warning<\/strong>: Your terminal might not have the facility of blinking.<\/p>\n<h5>Sample Output<\/h5>\n<pre>[root@tecmint ~]# chmod 755 Colorfull.sh\r\n[root@tecmint ~]# .\/Colorfull.sh\r\n\r\nHello World\r\nBlink\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World\r\nHello World<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/Colorfull.sh\">Download Colorfull.sh<\/a><\/p>\n<h3>Script 3: Encrypt a File\/Directory<\/h3>\n<p>This script will encrypt a\u00a0<strong>file<\/strong>\u00a0(remember?\u00a0<strong>directory<\/strong>\/<strong>driver<\/strong>\/\u2026. everything is treated as file, in\u00a0<strong>Linux<\/strong>). The current limitation of the above script is that it don\u2019t support auto completion of name using\u00a0<strong>TAB<\/strong>. Moreover, you need to place the script and file to be encrypted in the same folder. You may need to install \u201c<strong>pinentry-gui<\/strong>\u201d, using\u00a0<strong>yum<\/strong>\u00a0or\u00a0<strong>apt<\/strong>\u00a0the package, if required.<\/p>\n<pre>[root@midstage ~]# yum install pinentry-gui\r\n[root@midstage ~]# apt-get install pinentry-gui<\/pre>\n<p>Crete a file called \u201c<strong>Encrypt.sh<\/strong>\u201d and place the following script, make it executable and run it as shown.<\/p>\n<pre>#!\/bin\/bash\r\necho \"Welcome, I am ready to encrypt a file\/folder for you\"\r\necho \"currently I have a limitation, Place me to thh same folder, where a file to be \r\nencrypted is present\"\r\necho \"Enter the Exact File Name with extension\"\r\nread file;\r\ngpg -c $file\r\necho \"I have encrypted the file successfully...\"\r\necho \"Now I will be removing the original file\"\r\nrm -rf $file<\/pre>\n<h3>Sample Output<\/h3>\n<pre>[root@tecmint ~]# chmod 755 Encrypt.sh\r\n[root@tecmint ~]# .\/Encrypt.sh\r\n\r\nWelcome, I am ready to encrypt a file\/folder for you\r\ncurrently I have a limitation, Place me to the same folder, where a file to be\r\n\r\nencrypted is present\r\nEnter the Exact File Name with extension\r\n\r\npackage.xml\r\n\r\n                                                   \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n                                                   \u2502 Enter passphrase                                    \u2502\r\n                                                   \u2502                                                     \u2502\r\n                                                   \u2502                                                     \u2502\r\n                                                   \u2502 Passphrase *******_________________________________ \u2502\r\n                                                   \u2502                                                     \u2502\r\n                                                   \u2502       &lt;OK&gt;                             &lt;Cancel&gt;     \u2502\r\n                                                   \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n\r\nPlease re-enter this passphrase\r\n\r\n                                                   \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n                                                   \u2502 Please re-enter this passphrase                     \u2502\r\n                                                   \u2502                                                     \u2502\r\n                                                   \u2502 Passphrase ********________________________________ \u2502\r\n                                                   \u2502                                                     \u2502\r\n                                                   \u2502       &lt;OK&gt;                             &lt;Cancel&gt;     \u2502\r\n                                                   \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n\r\nI have encrypted the file successfully...\r\nNow I will be removing the original file\r\n&lt;\/pre&gt;<\/pre>\n<p><strong>gpg -c<\/strong>\u00a0: This will encrypt your file, using a passkey aka\u00a0<strong>password<\/strong>. In this process of learning you would have never thought that the actual process of learning could be that much easy. So after encrypting a file what you need? Obviously! decrypting the file. And I want you \u2013 the learner, the reader to write the decryption script yourself, don\u2019t worry I am not leaving you in the middle, I just want you to gain something out of this article.<\/p>\n<p><strong>Note<\/strong>:\u00a0<strong>gpg -d filename.gpg<\/strong>\u00a0&gt;\u00a0<strong>filename<\/strong>\u00a0is what you need to implement in your decryption script. You may post you script in comment if successful, if not you may ask me to write it for you.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/Encrypt.sh\">Download Encrypt.sh<\/a><\/p>\n<h3>Script 4: Checking Server Utilization<\/h3>\n<p>Checking the server utilization is one of the important task of an administrator, and a good administrator is one who knows how to automate his day to day task. Below is the script that will give many such information about your server. Check it yourself.<\/p>\n<pre>#!\/bin\/bash\r\n    date;\r\n    echo \"uptime:\"\r\n    uptime\r\n    echo \"Currently connected:\"\r\n    w\r\n    echo \"--------------------\"\r\n    echo \"Last logins:\"\r\n    last -a |head -3\r\n    echo \"--------------------\"\r\n    echo \"Disk and memory usage:\"\r\n    df -h | xargs | awk '{print \"Free\/total disk: \" $11 \" \/ \" $9}'\r\n    free -m | xargs | awk '{print \"Free\/total memory: \" $17 \" \/ \" $8 \" MB\"}'\r\n    echo \"--------------------\"\r\n    start_log=`head -1 \/var\/log\/messages |cut -c 1-12`\r\n    oom=`grep -ci kill \/var\/log\/messages`\r\n    echo -n \"OOM errors since $start_log :\" $oom\r\n    echo \"\"\r\n    echo \"--------------------\"\r\n    echo \"Utilization and most expensive processes:\"\r\n    top -b |head -3\r\n    echo\r\n\ttop -b |head -10 |tail -4\r\n    echo \"--------------------\"\r\n    echo \"Open TCP ports:\"\r\n    nmap -p- -T4 127.0.0.1\r\n    echo \"--------------------\"\r\n    echo \"Current connections:\"\r\n    ss -s\r\n    echo \"--------------------\"\r\n    echo \"processes:\"\r\n    ps auxf --width=200\r\n    echo \"--------------------\"\r\n    echo \"vmstat:\"\r\n    vmstat 1 5<\/pre>\n<h5>Sample Output<\/h5>\n<pre>[root@tecmint ~]# chmod 755 Server-Health.sh\r\n[root@tecmint ~]# .\/Server-Health.sh\r\n\r\nTue Jul 16 22:01:06 IST 2013\r\nuptime:\r\n 22:01:06 up 174 days,  4:42,  1 user,  load average: 0.36, 0.25, 0.18\r\nCurrently connected:\r\n 22:01:06 up 174 days,  4:42,  1 user,  load average: 0.36, 0.25, 0.18\r\nUSER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT\r\ntecmint   pts\/0    116.72.134.162   21:48    0.00s  0.03s  0.03s sshd: tecmint [priv]\r\n--------------------\r\nLast logins:\r\ntecmint   pts\/0        Tue Jul 16 21:48   still logged in    116.72.134.162\r\ntecmint   pts\/0        Tue Jul 16 21:24 - 21:43  (00:19)     116.72.134.162\r\n--------------------\r\nDisk and memory usage:\r\nFree\/total disk: 292G \/ 457G\r\nFree\/total memory: 3510 \/ 3838 MB\r\n--------------------\r\nOOM errors since Jul 14 03:37 : 0\r\n--------------------\r\nUtilization and most expensive processes:\r\ntop - 22:01:07 up 174 days,  4:42,  1 user,  load average: 0.36, 0.25, 0.18\r\nTasks: 149 total,   1 running, 148 sleeping,   0 stopped,   0 zombie\r\nCpu(s):  0.1%us,  0.0%sy,  0.0%ni, 99.3%id,  0.6%wa,  0.0%hi,  0.0%si,  0.0%st\r\n\r\n  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND\r\n    1 root      20   0  3788 1128  932 S  0.0  0.0   0:32.94 init\r\n    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd\r\n    3 root      RT   0     0    0    0 S  0.0  0.0   0:14.07 migration\/0<\/pre>\n<p><strong>Note<\/strong>: I have given you the script that gives the output in the terminal itself, how about getting the output in a file for future reference. Implement it using redirect operator.<\/p>\n<ol>\n<li>\u2018<strong>&gt;<\/strong>\u2018 : the redirection operator causes a file creation, and if it does exist, the contents are overwritten.<\/li>\n<li>\u2018<strong>&gt;&gt;<\/strong>\u2018 : when you use &gt;&gt;, you are adding information, rather than replacing it.<\/li>\n<li>\u2018<strong>&gt;&gt;<\/strong>\u2018 is safe, as compared to \u2018<strong>&gt;<\/strong>\u2018<\/li>\n<\/ol>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/Server-Health.sh\">Download Server-Health.sh<\/a><\/p>\n<h3>Script 5: Check Disk Space and Sends an Email Alert<\/h3>\n<p>How about getting an email when disk use in partition\u00a0<strong>PART<\/strong>\u00a0is bigger than Maximum allowed, it is a life saver script for web administrators with little modification.<\/p>\n<pre>MAX=95\r\nEMAIL=USER@domain.com\r\nPART=sda1\r\nUSE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`\r\nif [ $USE -gt $MAX ]; then\r\n  echo \"Percent used: $USE\" | mail -s \"Running out of disk space\" $EMAIL\r\nfi<\/pre>\n<p><strong>Note<\/strong>: Remove \u201c<strong>USER<\/strong>\u201d with your user name. You can check mail using using \u2018<strong>mail<\/strong>\u2018 command.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/Check-Disk-Space.sh\">Download Check-Disk-Space.sh<\/a><\/p>\n<p>Script writing and programming is beyond boundaries, anything and everything could be implemented as required. That\u2019s all for now, In my very next article I will be giving your some different flavors of scripting. Till then stay cool and tuned, enjoy.<\/p>\n<h1 class=\"post-title\">Sailing Through The World of Linux BASH Scripting \u2013 Part III<\/h1>\n<p>The Previous following articles of \u2018<strong>Shell Scripting<\/strong>\u2018 series were highly appreciated and hence I am writing this article to extend the never ending process of learning.<\/p>\n<div id=\"attachment_3557\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/sailing-through-the-world-of-linux-bash-scripting-part-iii\/shell-scripting-part-3\/\" rel=\"attachment wp-att-3557\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3557\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/07\/Shell-Scripting-Part-3.png\" alt=\"Basic Shell Scripting Part-3\" width=\"435\" height=\"321\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Basic Shell Scripting Part-3<\/p>\n<\/div>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/understand-linux-shell-and-basic-shell-scripting-language-tips\/\" target=\"_blank\" rel=\"noopener\">Understand Basic Linux Shell Scripting Language Tips \u2013 Part I<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/basic-shell-programming-part-ii\/\" target=\"_blank\" rel=\"noopener\">5 Shell Scripts for Linux Newbies to Learn Shell Programming \u2013 Part II<\/a><\/li>\n<\/ol>\n<h5>Bash Keywords<\/h5>\n<p>A\u00a0<strong>keyword<\/strong>\u00a0is a word or symbol that has a special meaning to a computer language. The following symbols and words have special meanings to\u00a0<strong>Bash<\/strong>\u00a0when they are unquoted and the first word of a command.<\/p>\n<pre>! \t\t\tesac \t\t\tselect \t\t} \r\ncase \t\t\tfi \t\t\tthen \t\t[[ \r\ndo \t\t\tfor \t\t\tuntil \t\t]] \r\ndone \t\t\tfunction \t\twhile \t\telif\r\nif \t\t\ttime \t\t\telse \t\tin \t\t{<\/pre>\n<p>Unlike most computer languages,\u00a0<strong>Bash<\/strong>\u00a0allows keywords to be used as\u00a0<strong>variable names<\/strong>\u00a0even though this can make scripts difficult to read. To keep scripts understandable, key-words should not be used for variable names.<\/p>\n<p>A command is implemented in shell as\u00a0<strong>$<\/strong>(command). You might have to include the full path of command. e.g.,\u00a0<strong>$(\/bin\/date)<\/strong>, for correct execution.<\/p>\n<p>You may know the path of specific program using \u2018<strong>whereis<\/strong>\u2018 command. e.g.,\u00a0<strong>whereis date<\/strong><\/p>\n<pre>[root@tecmint \/]# whereis date\r\ndate: \/bin\/date \/usr\/share\/man\/man1\/date.1.gz<\/pre>\n<p>That\u2019s enough for now. We won\u2019t be talking much about these theory now. Coming to Scripts.<\/p>\n<h3>Move Current Working Directory<\/h3>\n<p>Move from current working directory to any level up by just providing the numerical value at the end of script while executing.<\/p>\n<pre>#! \/bin\/bash \r\nLEVEL=$1 \r\nfor ((i = 1; i &lt;= LEVEL; i++)) \r\ndo \r\nCDIR=..\/$CDIR \r\ndone \r\ncd $CDIR \r\necho \"You are in: \"$PWD \r\nexec \/bin\/bash<\/pre>\n<p>Save the above codes as \u201c<strong>up.sh<\/strong>\u201c, on your desktop. Make it executable (<strong>chmod 755 up.sh<\/strong>). Run:<\/p>\n<p><strong>.\/up.sh 2<\/strong>\u00a0(will Move the current working directory to two level up).<br \/>\n<strong>.\/up.sh 4<\/strong>\u00a0(will Move the current working directory to four level up).<\/p>\n<h5>Use and Area of Application<\/h5>\n<p>In larger scripts which contains folder inside folder inside\u2026 containing\u00a0<strong>libraries<\/strong>,\u00a0<strong>binaries<\/strong>,\u00a0<strong>icons<\/strong>,\u00a0<strong>executables<\/strong>, etc at different location, You as a developer can implement this script to move to the desired location in a very automated fashion.<\/p>\n<p><strong>Note<\/strong>: For is a loop in the above script and it will continue to execute till the values are true for the loop.<\/p>\n<h5>Sample Output<\/h5>\n<pre>[root@tecmint \/]# chmod 755 up\r\n[root@tecmint \/]# .\/up.sh 2\r\nYou are in: \/\r\n\r\n[root@tecmint \/]# .\/up.sh 4 \r\nYou are in: \/ \r\n\r\n[root@tecmint \/]#<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/up.sh\" target=\"_blank\" rel=\"noopener\">Download up.sh<\/a><\/p>\n<h3>Create a Random File or Folder<\/h3>\n<p>Create a random file (folder) with no chance of duplication.<\/p>\n<pre>#! \/bin\/bash\r\n\r\necho \"Hello $USER\";\r\necho \"$(uptime)\" &gt;&gt; \"$(date)\".txt\r\necho \"Your File is being saved to $(pwd)\"<\/pre>\n<p>This is a Simple script but it\u2019s working is not that much simple.<\/p>\n<ol>\n<li>\u2018<strong>echo<\/strong>\u2018 : Prints everything written within the quotes.<\/li>\n<li>\u2018<strong>$<\/strong>\u2018 : Is a shell variable.<\/li>\n<li>\u2018<strong>&gt;&gt;<\/strong>\u2018 : The output is redirected to the output of\u00a0<strong>date<\/strong>\u00a0command followed by\u00a0<strong>txt<\/strong>\u00a0extension.<\/li>\n<\/ol>\n<p>We know the output of\u00a0<strong>date<\/strong>\u00a0command is\u00a0<strong>date<\/strong>, and\u00a0<strong>time<\/strong>\u00a0in\u00a0<strong>hour<\/strong>,\u00a0<strong>minute,<\/strong>\u00a0<strong>second<\/strong>\u00a0along with\u00a0<strong>year<\/strong>. Hence we could get output on an organised file name without the chance of filename duplication. It could be very much useful when user needs the file created with\u00a0<strong>time stamp<\/strong>\u00a0for future reference.<\/p>\n<h5>Sample Output<\/h5>\n<pre>[root@tecmint \/]# .\/randomfile.sh  \r\nHello server \r\nYour File is being saved to \/home\/server\/Desktop<\/pre>\n<p>You can view the file which is created on desktop with Today\u2019s Date and current time.<\/p>\n<pre>[root@tecmint \/]# nano Sat\\ Jul\\ 20\\ 13\\:51\\:52\\ IST\\ 2013.txt \r\n13:51:52 up  3:54,  1 user,  load average: 0.09, 0.12, 0.08<\/pre>\n<p>A more detailed implementation of the above script is given below, which works on the above principle and is very useful in gathering the network information of a\u00a0<strong>Linux<\/strong>\u00a0server.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/randomfile.sh\" target=\"_blank\" rel=\"noopener\">Download randomfile.sh<\/a><\/p>\n<h3>Script to Collect Network Information<\/h3>\n<p>Gathers network information on a\u00a0<strong>Linux<\/strong>\u00a0server. The script is too large and it\u2019s not possible to post the whole code and output of the script here. So, it\u2019s better you can download the script using below download link and test it yourself.<\/p>\n<p><strong>Note<\/strong>: You might need to install\u00a0<strong>lsb-core<\/strong>\u00a0package and other required packages and dependency.\u00a0<strong>Apt<\/strong>\u00a0or\u00a0<strong>Yum<\/strong>\u00a0the required packages. Obviously you need to be\u00a0<strong>root<\/strong>\u00a0to run the script because most of the commands used here are configured to be run as\u00a0<strong>root<\/strong>.<\/p>\n<h5>Sample Output<\/h5>\n<pre>[root@tecmint \/]# .\/collectnetworkinfo.sh  \r\n\r\nThe Network Configuration Info Written To <strong>network.20-07-13.info.txt<\/strong>. Please email this file to <strong>your_name@service_provider.com<\/strong>. ktop<\/pre>\n<p>You can change the above email address in your script to get it being mailed to you. The Automatically generated file can be viewed.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/collectnetworkinfo.sh\" target=\"_blank\" rel=\"noopener\">Download collectnetworkinfo.sh<\/a><\/p>\n<h3>Script to Converts UPPERCASE to lowercase<\/h3>\n<p>A script that converts\u00a0<strong>UPPERCASE<\/strong>\u00a0to\u00a0<strong>lowercase<\/strong>\u00a0and redirects the output to a text file \u201c<strong>small.txt<\/strong>\u201d which can be modified as required.<\/p>\n<pre>#!\/bin\/bash \r\n\r\necho -n \"Enter File Name : \" \r\nread fileName \r\n\r\nif [ ! -f $fileName ]; then \r\n  echo \"Filename $fileName does not exists\" \r\n  exit 1 \r\nfi \r\n\r\ntr '[A-Z]' '[a-z]' &lt; $fileName &gt;&gt; small.txt<\/pre>\n<p>This above script can convert the case of a file of any length with a single click from\u00a0<strong>uppercase<\/strong>\u00a0to\u00a0<strong>lowercase<\/strong>and vice-versa if required, with little modification.<\/p>\n<h5>Sample Output<\/h5>\n<pre>[root@tecmint \/]# .\/convertlowercase.sh  \r\nEnter File Name : a.txt \r\n\r\nInitial File: \r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\n...<\/pre>\n<p>New File (<strong>small.txt<\/strong>) output:<\/p>\n<pre>a\r\nb\r\nc\r\nd\r\ne\r\nf\r\ng\r\nh\r\ni\r\nj\r\nk\r\n...<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/convertlowercase.sh\" target=\"_blank\" rel=\"noopener\">Download convertlowercase.sh<\/a><\/p>\n<h3>Simple Calculator Program<\/h3>\n<pre>#! \/bin\/bash \r\nclear \r\nsum=0 \r\ni=\"y\" \r\n\r\necho \" Enter one no.\" \r\nread n1 \r\necho \"Enter second no.\" \r\nread n2 \r\nwhile [ $i = \"y\" ] \r\ndo \r\necho \"1.Addition\" \r\necho \"2.Subtraction\" \r\necho \"3.Multiplication\" \r\necho \"4.Division\" \r\necho \"Enter your choice\" \r\nread ch \r\ncase $ch in \r\n    1)sum=`expr $n1 + $n2` \r\n     echo \"Sum =\"$sum;; \r\n        2)sum=`expr $n1 - $n2` \r\n     echo \"Sub = \"$sum;; \r\n    3)sum=`expr $n1 \\* $n2` \r\n     echo \"Mul = \"$sum;; \r\n    4)sum=`expr $n1 \/ $n2` \r\n     echo \"Div = \"$sum;; \r\n    *)echo \"Invalid choice\";; \r\nesac \r\necho \"Do u want to continue (y\/n)) ?\" \r\nread i \r\nif [ $i != \"y\" ] \r\nthen \r\n    exit \r\nfi \r\ndone<\/pre>\n<h5>Sample Output<\/h5>\n<pre>[root@tecmint \/]# .\/simplecalc.sh \r\n\r\nEnter one no. \r\n12 \r\nEnter second no. \r\n14 \r\n1.Addition \r\n2.Subtraction \r\n3.Multiplication \r\n4.Division \r\nEnter your choice \r\n1 \r\nSum =26 \r\nDo u want to continue (y\/n)) ? \r\ny\r\n1.Addition \r\n2.Subtraction \r\n3.Multiplication \r\n4.Division \r\nEnter your choice \r\n3 \r\nmul = 14812\r\nDo u want to continue (y\/n)) ? \r\nn<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/scripts\/simplecalc.sh\" target=\"_blank\" rel=\"noopener\">Download simplecalc.sh<\/a><\/p>\n<p>So did you saw how easy it was to create a powerful program as calculations such a simple way. Its\u2019 not the end. We will be comping up with at least one more article of this series, covering broad perspective from administration view.<\/p>\n<p>That\u2019s all for now. Being the reader and the best critic don\u2019t forget to tell us how much and what you enjoyed in this article and what you want to see in the future article. Any question is highly welcome in comment. Till then stay\u00a0<strong>healthy<\/strong>,\u00a0<strong>safe<\/strong>\u00a0and\u00a0<strong>tuned<\/strong>.\u00a0<strong>Like<\/strong>\u00a0and\u00a0<strong>Share<\/strong>\u00a0us and help us spread.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/understand-linux-shell-and-basic-shell-scripting-language-tips\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Picture speak more than words and the below picture says all about the working of\u00a0Linux. &nbsp; Understanding Linux Shell Read Also 5 Shell Scripts to Learn Shell Programming \u2013 Part II Sailing Through The World of Linux BASH Scripting \u2013 Part III Understanding Linux Shell Shell: A Command-Line Interpretor that connects a user to\u00a0Operating System\u00a0and &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/20\/understand-linux-shell-and-basic-shell-scripting-language-tips-iiiiii-parts\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Understand Linux Shell and Basic Shell Scripting Language Tips (I,II,III parts)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11984","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11984"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11984\/revisions"}],"predecessor-version":[{"id":11985,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11984\/revisions\/11985"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}