Understanding Linux Shell
- Shell: A Command-Line Interpretor that connects a user to Operating System and allows to execute the commands or by creating text script.
- Process: Any task that a user run in the system is called a process. A process is little more complex than just a task.
- File: It resides on hard disk (hdd) and contains data owned by a user.
- X-windows aka windows: A mode of Linux where screen (monitor) can be split in small “parts” called windows, that allow a user to do several things at the same time and/or switch from one task to another easily and view graphics in a nice way.
- Text terminal: A monitor that has only the capability of displaying text stuff, no graphics or a very basic graphics display.
- Session: Time between logging on and logging out of the system.
Types of Shell on a Standard Linux Distribution
Bourne shell : The Bourne shell was one of the major shells used in early versions and became a de facto standard. It was written by Stephen Bourne at Bell Labs. Every Unix-like system has at least one shell compatible with the Bourne shell. The Bourne shell program name is “sh” and it is typically located in the file system hierarchy at /bin/sh.
C shell: The C shell was developed by Bill Joy for the Berkeley Software Distribution. Its syntax is modelled after the C programming language. It is used primarily for interactive terminal use, but less frequently for scripting and operating system control. C shell has many interactive commands.
Beginning the Fun! (Linux Shell)
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.
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.
Basics of Shell Programming
- To get a Linux shell, you need to start a terminal.
- To see what shell you have, run: echo $SHELL.
- In Linux, the dollar sign ($) stands for a shell variable.
- The ‘echo‘ command just returns whatever you type in.
- The pipeline instruction (|) comes to rescue, when chaining several commands.
- Linux commands have their own syntax, Linux won’t forgive you whatsoever is the mistakes. If you get a command wrong, you won’t flunk or damage anything, but it won’t work.
- #!/bin/sh – It is called shebang. It is written at the top of a shell script and it passes the instruction to the program /bin/sh.
About shell Script
Shell script is just a simple text file with “.sh” extension, having executable permission.
Process of writing and executing a script
- Open terminal.
- Navigate to the place where you want to create script using ‘cd‘ command.
- Cd (enter) [This will bring the prompt at Your home Directory].
- touch hello.sh (Here we named the script as hello, remember the ‘.sh‘ extension is compulsory).
- vi hello.sh (nano hello.sh) [You can use your favourite editor, to edit the script].
- chmod 744 hello.sh (making the script executable).
- sh hello.sh or ./hello.sh (running the script)
Writing your First Script
#!/bin/bash# My first scriptecho "Hello World!"
Save the above lines on a text file, make it executable and run it, as described above.
Sample Output
Hello World!
In the above code.
#!/bin/bash (is the shebang.)# My first script (is comment, anything following '#' is a comment)echo “Hello World!” (is the main part of this script)
Writing your Second Script
OK time to move to the next script. This script will tell you, your’s “username” and list the running processes.
#! /bin/bashecho "Hello $USER"echo "Hey i am" $USER "and will be telling you about the current processes"echo "Running processes List"ps
Create a file with above codes, save it to anything you want, but with extension “.sh“, make it executable and run it, from you terminal.
Sample Output
Hello tecmintHey i am tecmint and will be telling you about the current processesRunning processes ListPID TTY TIME CMD1111 pts/0 00:00:00 bash1287 pts/0 00:00:00 sh1288 pts/0 00:00:00 ps
Was this cool? 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.
When your needs are greater – whether in functionality, robustness, performance, efficiency etc – then you can move to a more full-featured language.
If you already know C/Perl/Python programming language or any other programming language, learning the scripting language won’t be much difficult.
Writing your Third Script
Moving to, write our third and last script for this article. This script acts as an interactive script. Why don’t you, yourself execute this simple yet interactive script and tell us how you felt.
#! /bin/bashecho "Hey what's Your First Name?";read a;echo "welcome Mr./Mrs. $a, would you like to tell us, Your Last Name";read b;echo "Thanks Mr./Mrs. $a $b for telling us your name";echo "*******************"echo "Mr./Mrs. $b, it's time to say you good bye"
Sample Output
Hey what's Your First Name?Avishekwelcome Mr./Mrs. Avishek, would you like to tell us, Your Last NameKumarThanks Mr./Mrs. Avishek Kumar for telling us your name******************************************************Mr./Mrs. Kumar, it's time to say you good bye
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. Your valuable thoughts in comments is highly appreciated, Like and share us and help us to spread. Till then just chill, keep connected, stay tuned.
Read Also :
This article is an extension of our First article , where we gave you a taste of Scripting, continuing that we won’t disappoint you in this article.
Script 1: Drawing a Special Pattern
#!/bin/bashMAX_NO=0echo -n "Enter Number between (5 to 9) : "read MAX_NOif ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; thenecho "WTF... I ask to enter number between 5 and 9, Try Again"exit 1ficlearfor (( i=1; i<=MAX_NO; i++ )) do for (( s=MAX_NO; s>=i; s-- ))doecho -n " "donefor (( j=1; j<=i; j++ )) do echo -n " ." done echo "" done ###### Second stage ###################### for (( i=MAX_NO; i>=1; i-- ))dofor (( s=i; s<=MAX_NO; s++ ))doecho -n " "donefor (( j=1; j<=i; j++ ))doecho -n " ."doneecho ""doneecho -e "\n\n\t\t\t Whenever you need help, Tecmint.com is always there"
Most of the above ‘key words‘ would be known to you and most of them are self explanatory. e.g., MAX sets 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.
Sample Output
[root@tecmint ~]# chmod 755 Special_Pattern.sh[root@tecmint ~]# ./Special_Pattern.shEnter Number between (5 to 9) : 6.. .. . .. . . .. . . . .. . . . . .. . . . . .. . . . .. . . .. . .. ..Whenever you need help, Tecmint.com is always there
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.
Script 2: Creating Colorful Script
Who says, Linux is colorless and boring, save the codes below to anything [dot] sh, make it executable and Run it, don’t forget to tell me how it was, Think what you can achieve, implementing it somewhere.
#!/bin/bashclear echo -e "33[1m Hello World"# bold effectecho -e "33[5m Blink"# blink effectecho -e "33[0m Hello World"# back to normalecho -e "33[31m Hello World"# Red colorecho -e "33[32m Hello World"# Green colorecho -e "33[33m Hello World"# See remaining on screenecho -e "33[34m Hello World"echo -e "33[35m Hello World"echo -e "33[36m Hello World"echo -e -n "33[0m"# back to normalecho -e "33[41m Hello World"echo -e "33[42m Hello World"echo -e "33[43m Hello World"echo -e "33[44m Hello World"echo -e "33[45m Hello World"echo -e "33[46m Hello World"echo -e "33[0m Hello World"
Note: Don’t bother about the color code now, Those important to you will be at your tongue, gradually.
Warning: Your terminal might not have the facility of blinking.
Sample Output
[root@tecmint ~]# chmod 755 Colorfull.sh[root@tecmint ~]# ./Colorfull.shHello WorldBlinkHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World
Script 3: Encrypt a File/Directory
This script will encrypt a file (remember? directory/driver/…. everything is treated as file, in Linux). The current limitation of the above script is that it don’t support auto completion of name using TAB. Moreover, you need to place the script and file to be encrypted in the same folder. You may need to install “pinentry-gui”, using yum or apt the package, if required.
[root@midstage ~]# yum install pinentry-gui[root@midstage ~]# apt-get install pinentry-gui
Crete a file called “Encrypt.sh” and place the following script, make it executable and run it as shown.
#!/bin/bashecho "Welcome, I am ready to encrypt a file/folder for you"echo "currently I have a limitation, Place me to thh same folder, where a file to be encrypted is present"echo "Enter the Exact File Name with extension"read file;gpg -c $fileecho "I have encrypted the file successfully..."echo "Now I will be removing the original file"rm -rf $file
Sample Output
[root@tecmint ~]# chmod 755 Encrypt.sh[root@tecmint ~]# ./Encrypt.shWelcome, I am ready to encrypt a file/folder for youcurrently I have a limitation, Place me to the same folder, where a file to beencrypted is presentEnter the Exact File Name with extensionpackage.xml┌─────────────────────────────────────────────────────┐│ Enter passphrase ││ ││ ││ Passphrase *******_________________________________ ││ │││└─────────────────────────────────────────────────────┘Please re-enter this passphrase┌─────────────────────────────────────────────────────┐│ Please re-enter this passphrase ││ ││ Passphrase ********________________________________ ││ ││ │└─────────────────────────────────────────────────────┘I have encrypted the file successfully...Now I will be removing the original file
gpg -c : This will encrypt your file, using a passkey aka password. 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 – the learner, the reader to write the decryption script yourself, don’t worry I am not leaving you in the middle, I just want you to gain something out of this article.
Note: gpg -d filename.gpg > filename is 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.
Script 4: Checking Server Utilization
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.
#!/bin/bashdate;echo "uptime:"uptimeecho "Currently connected:"wecho "--------------------"echo "Last logins:"last -a |head -3echo "--------------------"echo "Disk and memory usage:"df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}'free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}'echo "--------------------"start_log=`head -1 /var/log/messages |cut -c 1-12`oom=`grep -ci kill /var/log/messages`echo -n "OOM errors since $start_log :" $oomecho ""echo "--------------------"echo "Utilization and most expensive processes:"top -b |head -3echotop -b |head -10 |tail -4echo "--------------------"echo "Open TCP ports:"nmap -p- -T4 127.0.0.1echo "--------------------"echo "Current connections:"ss -secho "--------------------"echo "processes:"ps auxf --width=200echo "--------------------"echo "vmstat:"vmstat 1 5
Sample Output
[root@tecmint ~]# chmod 755 Server-Health.sh[root@tecmint ~]# ./Server-Health.shTue Jul 16 22:01:06 IST 2013uptime:22:01:06 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18Currently connected:22:01:06 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATtecmint pts/0 116.72.134.162 21:48 0.00s 0.03s 0.03s sshd: tecmint [priv]--------------------Last logins:tecmint pts/0 Tue Jul 16 21:48 still logged in 116.72.134.162tecmint pts/0 Tue Jul 16 21:24 - 21:43 (00:19) 116.72.134.162--------------------Disk and memory usage:Free/total disk: 292G / 457GFree/total memory: 3510 / 3838 MB--------------------OOM errors since Jul 14 03:37 : 0--------------------Utilization and most expensive processes:top - 22:01:07 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18Tasks: 149 total, 1 running, 148 sleeping, 0 stopped, 0 zombieCpu(s): 0.1%us, 0.0%sy, 0.0%ni, 99.3%id, 0.6%wa, 0.0%hi, 0.0%si, 0.0%stPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND1 root 20 0 3788 1128 932 S 0.0 0.0 0:32.94 init2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd3 root RT 0 0 0 0 S 0.0 0.0 0:14.07 migration/0
Note: 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.
- ‘>‘ : the redirection operator causes a file creation, and if it does exist, the contents are overwritten.
- ‘>>‘ : when you use >>, you are adding information, rather than replacing it.
- ‘>>‘ is safe, as compared to ‘>‘
Script 5: Check Disk Space and Sends an Email Alert
How about getting an email when disk use in partition PART is bigger than Maximum allowed, it is a life saver script for web administrators with little modification.
MAX=95EMAIL=USER@domain.comPART=sda1USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`if [ $USE -gt $MAX ]; thenecho "Percent used: $USE" | mail -s "Running out of disk space" $EMAILfi
Note: Remove “USER” with your user name. You can check mail using using ‘mail‘ command.
Script writing and programming is beyond boundaries, anything and everything could be implemented as required. That’s 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.