Basic Shell command to make ourselves comfortable at all times

People might wonder why i need to learn a scripting language , if i am a hardcore DBA , let me tell you , if you are in the administration side of your career you cannot avoid these scripting languages. Whether it might be a

Shell Scripting
Perl Scripting
Batch Scripting

These should become the primary skill in par Database Administration

If you work on linux server make sure you are good with at these least basics of Shell commands

If you work on windows machine you should be comfortable with Batch scripting

And Perl , this is a friend of all , The moment you make yourself comfortable with Perl , you break the environment barriers doesn't matter which machine you work on , Perl is supported almost in all the Platforms

Keeping apart Perl as this post is to discuss some basic and common commands to grip yourself while working on Linux environment

Shell Basic Commands :
  • grep
  • ls,ls -lrt
  • find
  • awk
  • sed
  • crontab
  • vi editor

grep :

grep is a utility basically used for finding a text in a file or set of files . We see these with some examples
ls |grep -i somename
Very useful if you have long listing directory and searching for a file , -i make search case-insensitive
grep "sometext" filename
Find text in the filename you mentioned
grep -r 'bin' ./
This is very useful when you are searching for some text in all the files in a directory , Infact i use it most of the times .

ls,ls -lrt :

List of files in a directory
ls -lrt
This one would be more useful that the plain ls if you have long listing directory , this one shows most recently modified file to the last of the screen

find :

find / -name filename
Searches for file named 'filename' in the whole system
find ./ -name filename
Searches for file named 'filename' in the current working directory

awk :

awk is a very powerful tool when it comes to your scripting skills in shell
Here i will give you a simple demonstration Say you want to capture the load of the server , here is how you do it
uptime|awk {'print $10'}
uptime gives you the load of the cpu or server ,basically i think uptime is provided for the scripting purpose ,because the same thing you can get with the top command but its difficult to capture the desired O/P from it , Of course top -b (batch mode) gives you a solution , but uptime narrows it more for us , here is how uptime looks like
11:57am  up 58 days 23:14,  5 users,  load average: 10.66, 10.52, 10.35
We are only interested in only 0.66 marked in red
uptime|awk {'print $10'}
O/P : 10.66,
What did it do , it capture the 10th placed string starting from left with tabs as delimiter(tabs delimiter is default) If you see there is a ',' attached to the figure we need , how do with get rid of this , here is how
uptime|awk {'print $10'}|awk -F , {'print $1'}
O/P : 10.66
awk -F , {'print $1'} tells to use the delimiter as ',' , if you use ',' as delimiter you have only 2 parts left of ',' and right of ',' , any you captured the 1st part Now if you want to get rid of the decimals , you pipe another awk to the current one like
uptime|awk {'print $10'}|awk -F , {'print $1'}|awk -F . {'print $1'}
O/P : 10
Here we used '.' as delimiter . So this is one of the basic example how awk can be useful

We will continue some crontab's and working with vi editor in the next series of this post .

Have a nice day

No comments:

Post a Comment