Bash Script : Print lines of a file in reverse order

The task is to print the lines of a file in the reverse order in which it appears in the file. A bash script and some other tricks would be presented below to do this. We can do this with tac and sed , but here we will do it without any of these tools and use only the bash inbuilt, bash arrays, and redirection.
Check out the post for the script

Advertisements

Bash Script : Finding Digital Root of an Integer

To find a digital roots of an integer is a very common question in high school when doing introductory programming. Digital roots are defined as follows. First an integer is taken if the number of digits are greater than 1, then the sum of its digits are done, if the sum has more than one digits then again the sum of digits are done, and so on until a sum of digit has only 1 digit. This last value of the sum of digits containing a one digit value is defined as the digital root of a number. First we describe in brief what a digital root is then present a shell script.
Continue reading “Bash Script : Finding Digital Root of an Integer”

Bash Script : Check for palindrome

When learning shell script, a very common script which is given as a task in schools is to write a script to check if a string is a palindrome or not. I am presenting two solutions of this simple problem. A palindrome is a string whose reverse is same as the string itself. Like “madam” is a palindrome but “hello” is not

Continue reading “Bash Script : Check for palindrome”

Floating point math operations in bash

When writing scripts in bash, sometimes we need to work with basic math functions. Like the trigonometric functions, square root, cube root, logarithms. Bash does not support floating point operations. This is where the problem is encountered, and we cannot write a math function of our own. There must be a way to do this, and yes. Bash supports redirections so we can feed the floating point computations and the math functions into some other program’s input who understands it. The program which can be used is the bc, which is an arbitrary precision calculator language. bc can do floating point operations, and also can do basic math functions. With bash and bc we can do the above. Continue reading to know how.

Continue reading “Floating point math operations in bash”

CD Tray Open/Close infinite loop

Just feel the annoyance and irritation when the CD tray of your computer opens and closes, opens and closes, again and again, again and again, and does not stop until you shut down the computer. We will be writing a tiny shell script with which we would do the above stuff. Although this is very cruel if you apply this to some one, then please be sure to fix it before he/she shoots you with a 50 caliber rifle. So keep on reading to know how to make the simple script to make an infinite CD-ROM tray open close loop in GNU+Linux

Continue reading “CD Tray Open/Close infinite loop”

Split and Join Files

A lot of time we need to carry large sized files and we do not have enough space in a single media. At that time we need to split the large file into several small ones and then carry them in separate media. Also small files are very useful in uploading a large file for sharing or backup in online storage services. Here is a very simple way how you can split a large file in several equally sized small ones in Linux/Unix and again join them in Linux/Unix or in Microsoft Windows.

Continue reading “Split and Join Files”

Linux / Unix Shell Commands: Problems and Solutions (1st issue)

This is the first issue of the “Linux / Unix Shell Commands: Problems and Solutions” series. This issue features the questions taken from different year question papers of Computer Science Honours, Part II Practical examination of Calcutta University, India, Kolkata. This section carries 5 marks. When going through the papers I noticed that the questions were repeated in different years. Also there were common questions in different groups in the same year. I have tried by best to solve the questions and kept it as simple as possible. Each solution comes with a description of what the sequence of commands do. I have also provided more than one solution where applicable.

A note for the students: Please do not try to remember each and every command without understanding how they work. If you do not know the commands it is recommended that first learn the basics and know what the commands do. Refer any standard text book, the command manual pages, or the GNU website. If anything cannot be understood in this solution refer the man pages, if any mistake is found please leave a comment or contact me. The main intention of this solution is to act as a ready reference to save time.

Update Information: Updated at 17.03.2012

Continue reading “Linux / Unix Shell Commands: Problems and Solutions (1st issue)”