The Previous following articles of ‘Shell Scripting‘ series were highly appreciated and hence I am writing this article to extend the never ending process of learning.
- Understand Basic Linux Shell Scripting Language Tips – Part I
- 5 Shell Scripts for Linux Newbies to Learn Shell Programming – Part II
Bash Keywords
A keyword is a word or symbol that has a special meaning to a computer language. The following symbols and words have special meanings to Bash when they are unquoted and the first word of a command.
! esac select }case fi then [[done function whiledo for until ]] elif if time else in {
Unlike most computer languages, Bash allows keywords to be used as variable names even though this can make scripts difficult to read. To keep scripts understandable, key-words should not be used for variable names.
A command is implemented in shell as $(command). You might have to include the full path of command. e.g., $(/bin/date), for correct execution.
You may know the path of specific program using ‘whereis‘ command. e.g., whereis date
[root@tecmint /]# whereis datedate: /bin/date /usr/share/man/man1/date.1.gz
That’s enough for now. We won’t be talking much about these theory now. Coming to Scripts.
Move Current Working Directory
Move from current working directory to any level up by just providing the numerical value at the end of script while executing.
#! /bin/bashLEVEL=$11; i <= LEVEL; i++)) do CDIR=for ((i = ../$CDIR done cd $CDIRec /bin/bashecho "You are in: "$PWD ex
Save the above codes as “up.sh“, on your desktop. Make it executable (chmod 755 up.sh). Run:
Use and Area of Application
In larger scripts which contains folder inside folder inside… containing libraries, binaries,icons, executables, etc at different location, You as a developer can implement this script to move to the desired location in a very automated fashion.
Note: For is a loop in the above script and it will continue to execute till the values are true for the loop.
Sample Output
[root@tecmint /]# chmod 755 up[root@tecmint /]# ./up.sh 2/]# ./up.shYou are in: / [root@tecmin t4 You are in: / [root@tecmint /]#
Create a Random File or Folder
Create a random file (folder) with no chance of duplication.
#! /bin/bashcho "Hello $USER";eecho "$(uptime)" >> "$(date)".txtecho "Your File is being saved to $(pwd)"
This is a Simple script but it’s working is not that much simple.
- ‘echo‘ : Prints everything written within the quotes.
- ‘$‘ : Is a shell variable.
- ‘>>‘ : The output is redirected to the output of date command followed by txt extension.
We know the output of date command is date, and time in hour, minute, second along with year. 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 time stamp for future reference.
Sample Output
[root@tecmint /]# ./randomfile.shHello servereing saved to /home/server/DesktopYour File is b
You can view the file which is created on desktop with Today’s Date and current time.
[root@tecmint /]# nano Sat\ Jul\ 20\ 13\:51\:52\ IST\ 2013.txt13:51:52 up 3:54, 1 user, load average: 0.09, 0.12, 0.08
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 Linux server.
Script to Collect Network Information
Gathers network information on a Linux server. The script is too large and it’s not possible to post the whole code and output of the script here. So, it’s better you can download the script using below download link and test it yourself.
Note: You might need to install lsb-core package and other required packages and dependency. Apt or Yum the required packages. Obviously you need to be root to run the script because most of the commands used here are configured to be run as root.
Sample Output
[root@tecmint /]# ./collectnetworkinfo.shhe Network Configuration Info Written To network.20-07-13.info.txt. Please email this file to your_name@service_provider.com. ktopT
You can change the above email address in your script to get it being mailed to you. The Automatically generated file can be viewed.
Download collectnetworkinfo.sh
Script to Converts UPPERCASE to lowercase
A script that converts UPPERCASE to lowercase and redirects the output to a text file “small.txt” which can be modified as required.
#!/bin/bashcho -n "Enter File Name : "eread fileNameme ]; then echo "Filenameif [ ! -f $fileN a $fileName does not exists" exit 1 fi tr '[A-Z]' '[a-z]' < $fileName >> small.txt
This above script can convert the case of a file of any length with a single click fromuppercase to lowercase and vice-versa if required, with little modification.
Sample Output
[root@tecmint /]# ./convertlowercase.shEnter File Name : a.txt Initial File: A B C D E F G H I J K ...
New File (small.txt) output:
abdc ehf g i.j k ..
Simple Calculator Program
#! /bin/bashclear sum=0Enteri="y" echo " one no." read n1." read n2 while [ $iecho "Enter second n o= "y" ] do echo "1.Addition"tiplication" echo "4echo "2.Subtraction" echo "3.Mu l.Division" echo "Enter your choice" read chho "Sum ="$scase $ch in 1)sum=`expr $n1 + $n2` e cum;; 2)sum=`expr $n1 - $n2`pr $n1 \* $n2` echoecho "Sub = "$sum;; 3)sum=`e x "Mul = "$sum;; 4)sum=`expr $n1 / $n2`choice";; esac echo "Doecho "Div = "$sum;; *)echo "Invalid u want to continue (y/n)) ?" read i if [ $i != "y" ] then exit fi done
Sample Output
[root@tecmint /]# ./simplecalc.shEnter one no. 121.Addition 2.SEnter second no. 1 4ubtraction 3.Multiplication1 Sum =264.Division Enter your choicey 1.Addition 2.Subtraction 3Do u want to continue (y/n)) ? .Multiplication 4.Division Enter your choice 3 mul = 14812 Do u want to continue (y/n)) ? n
So did you saw how easy it was to create a powerful program as calculations such a simple way. Its’ not the end. We will be comping up with at least one more article of this series, covering broad perspective from administration view.
That’s all for now. Being the reader and the best critic don’t 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 healthy, safe and tuned. Like and Share us and help us spread.