Shell Programming Blog Shell Programming Blog

Thursday, 11 October 2012

Linux Bash Scripting: how to print a big chess board anywhere on the screen

Unknown | 11:12 | Be the first to comment!


Bash Linux Unix shell scripting


In this post, we will see how to create a big chess board that can be displayed anywhere on the screen rather than being displayed left aligned. Below is the script which displays the Chess board on the screen.



If you want to make changes to the above script for changing the size and the position of the chess board, you can change the following variables in the script.

1) row - to change the number of lines to leave from top before printing the chess board
2) col - to change the number of columns to leave from left before printing the chess board
3) j in second for loop - to increase or decrease the height of the block
4) No. of spaces in echo statement to change the width of the block


The same script using C style for loop:



Friday, 5 October 2012

Have questions?? Need Clarifications???

Unknown | 08:34 | 6 Comments so far


Hi All,

I know that lot of us would get stuck while doing programming in Shell scripting or doing something in Linux. If you are badly in help and need to find an answer, you can make use of this page. Please post your questions on shell scripting in the comments box along with your email. Your questions will be answered and will be sent to your email. Please make use of this free service :)

Thanks,
Beneficial Knowledge

Thursday, 4 October 2012

how to change the default size of the terminal window in Solaris

Unknown | 09:13 | | Be the first to comment!


To change the default size of the terminal window in Solaris, you need to do the following.

1) Go to the directory /usr/share/vte/termcap/ and edit the file xterm

     i.e., $ vi /usr/share/vte/termcap/xterm

2) Look for :co#115:it#8:li#36:\

3) In the above line you can change the numbers like for example, put 77 after "co#" and put 48 after "li#"

4) Then every terminal you open will open with the size that you have mentioned in the above line.

OR

You can also add an alias as below in the .profile or .bashrc or .cshrc, wherever it is applicable for you.

alias xterm='xterm -geometry 77x48'

Any one of the above mentioned methods should do the work of changing the default terminal size. 

Note : You need to reboot your PC after you have made the above changes for your changes to be effective.

Tuesday, 2 October 2012

Linux Shell Scripting: how to list the files alone in a directory

Unknown | 09:21 | Be the first to comment!


Bash Linux Unix shell scripting

 Often we tend to look into the directories to check what files are present in the directory. When we try to list the files using ls command, all files along with the directories will be displayed. What if we want to list only the files and not the directories???

We will see how to do that in this post.

1) Using "find" command:

$ find . -type f -maxdepth 1

The above command will list also the hidden files. But if you want to avoid the hidden files to be displayed in your results, you need to use the below command.

$ find . -type f -maxdepth 1 \( ! -iname ".*" \)


2) To derive it from the "ls" command, we can use the below commands.

$ ls -l | grep -v ^d

$ ls -l | awk 'NR!=1 && !/^d/ {print $NF}'


Monday, 1 October 2012

Perform Arithmetic operations or mathematical calculations in Shell scripting - expr command

Unknown | 09:54 | Be the first to comment!


 Bash Linux Unix shell scripting

 The command which is used to perform mathematical or Arithmetic calculations is "expr" command.

Syntax:
expr <operand 1> operator <operand 2>



Examples:
$ expr 2 + 3
$ expr 5 - 3
$ expr 7 % 3
$ expr 12 \* 3
$ expr 12 / 6

However, even though the operations looks easy for performing, the "expr" command has a drawback. It uses special character (*) for multiplication which is recognized as a wild character by Linux/Unix. So, if you try to do multiplication as

$ expr 12 * 3

Then you will receive an error. You need to use an escape character (\) along with the (*) character to indicate that this should not be considered as a wild character. Hence the expression should be

$ expr 12 \* 3

If you are going to display the result or assign the result to some variable, then you need to include the expr command in a back tick(`). This symbol is not commonly used. You can find this symbol along with the (~) key in the US keyboard. This is not a single quote('). This is different from single quote and the double quote. Let's see the examples below illustrating the usage.



The back tick is used to inform the Linux system that whatever included within the back tick should be considered as a command and not a string.

To overcome this complexity, Bash shell introduced a new method of doing arithmetic calculations by using brackets. But still the Bash shell includes the expr command to stay compatible with Bourne shell.

Using Brackets:
Another easy way of doing Arithmetic calculation is to enclose the calculation using a dollar sign($) and square brackets ([]).

Examples:



When you are using brackets for doing arithmetic calculations, you don't need to worry about the wild character (*). Linux will consider whatever inside the bracket as  an expression and not as a wild character.

Finally, there is a major limitation in performing math in Bash shell. That is the Bash supports only  integer numbers and not floating numbers. For example if you do the below calculation, you will get only the integer as a result.

$ echo $[100 / 45]
2

But, the z shell (zsh) provides full floating point arithmetic operations. There is also an alternative way to do the floating point calculations in Bash shell. That is by using BC (Bash Calculator).

How to print the last field in a file in Linux/Unix Shell scripting

Unknown | 08:32 | Be the first to comment!


Bash Linux Unix shell scripting


Let us see how to print the last field alone from a file which contains several fields.

Let's say that the input is the below file.






Now we need to Print the last field. i.e.

The required Output would be:
----------------------------------
7
4
8
8

We can do this by three ways.

1) The normal awk way of doing it:

$ awk 'BEGIN {FS=":"} {print $NF}' priority.txt

2) Using sed:

$ sed -n 's/.*://;p' priority.txt

3) Using grep:

$ grep -o '[^:]*$' priority.txt

How to find the latest file in a directory

Unknown | 07:21 | Be the first to comment!


Bash Linux Unix shell scripting

Suppose if you hava a directory with a huge set of files(more than 500) in a log directory, you would want to find the latest file in the directory. Now we can see few ways to that.

for example, Let us take the below scenario




1) Now our goal is to print the last field of the last line (except the dir old) from the "ls -lrt" output in that directory.




So basically to print the last field of last line(in this case the last field of last line of "ls -lrt" command), we can use the below command.

$ ls -lrt | awk '{ f=$NF }; END{ print f }'


2) This can also be acheived by using Head command.

$ ls -t1 | head -n1


3) This can also be acheived by using Find command.

$ find . -not -type d -printf "%T+ %p\n" | sort -n | tail -1

Linux/Unix Shell Scripting topics explained on requests

Unknown | 02:11 | Be the first to comment!

  • Are you new to Shell scripting? 
  • Are you learning Shell Scripting either in Linux or Unix?
  • Is it tough for you to understand? 
  • Do you feel confused while trying to learn a topic?
  • Is there anything in shell scripting that you are not able to understand?

Please let us know (leave your comments below). We are here to help you understand(Don't worry!!! We won't charge u for it :P). We can explain things neatly for you with good examples and pictures.

Thanks,
Beneficial Knowledge
 

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming