Showing posts with label Shell Scripting. Show all posts
Showing posts with label Shell Scripting. Show all posts

Wednesday, January 7, 2015

Linux Tutorial: 5 Cool Vim Command Line Tricks

Like most “hardcore” Linux/Unix hackers I love Vi/Vim, and I’ve found there is no end to the things you can learn about Vim so I decided to add a few command line tricks that I just learned recently.
I’m going to use “file.txt” as our example file.
Here are some cool Vim command line tricks:
1. Open at the end of a file
vi + file.txt
The plus sign added before the file name opens up the file with the cursor at the end of the file. This can be handy if you have a huge text file you and you don’t want to scroll down.
2. Open file at specific line
vi +n file.txt
If you add a number (replace n with a number in case you hadn’t guessed) the cursor will go exactly to that line number. Extremely handy for large text files or error messages that specify a line number in a config or source file.
3. Open file at a pattern
vi +/pattern file.txt
This will open the file on the same line as the pattern you specify (first instance of it). This is great if you have a specific phrase to search for.
4. Recover a file after a crash
vi -r file.txt
Use -r to open a file and recover it after a crash. This could save your butt someday.
5. Run a script against a file
vi -s file.txt < script.sh
This trick could be super powerful, but also dangerous. This allows you to run a script against the file while opening, usually a search or something of that sort.
I hope this helps, I’m going to post more stuff as I learn it, there’s a reason they’ve written whole books about Vim, it has a ton of features. If people enjoy these tips, I’ll add some more later.

What is a shell script or shell scripting

A Shell script can be defined as - "a series of command(s) stored in a plain text file". A shell script is similar to a batch file in MS-DOS, but it is much more powerful compared to a batch file. Shell scripts are a fundamental part of the UNIX and Linux programming environment.  Normally shells are interactive. It means the shell will accept command from you (via keyboard) and execute them. However, if you store a sequence of commands to a text file and tell the shell to execute the text file instead of entering the commands, that is known as a shell program or shell script.

Each shell script consists of - Shell keywords such as if-else, do-while. Shell commands such as pwd, test, echo, continue, type. Linux binary commands such as w, who, free etc. Text processing utilities such as grep, awk, cut. Functions - add frequent actions together via functions. For example, /etc/init.d/functions file contains functions to be used by most or all system shell scripts in the /etc/init.d directory. Control flow statements such as if-then-else or shell loops to perform repeated actions.

Why we use Shell Scripting?
§  Shell scripts are useful for automating processes that you repeat at the prompt.
§  Shell scripts can take input from a user or file and output them to the screen.
§  Whenever you find yourself doing the same task over and over again you should use shell scripting, i.e., repetitive task automation.
§  Creating your own power tools/utilities.
§  Automating command input or entry.
§  Customizing administrative tasks.
§  Creating simple applications.
§  Since scripts are well tested, the chances of errors are reduced while configuring services or system administration tasks such as adding new users.

Shell Scripts used by me mostly:-
Shell scripting makes your Linux easy and here some examples of scripts that I use mostly while working on my Linux Box:
§  Find out what that site is running (just like www.thehackernews.com or www.cyberarmy.in).
§  Download RSS feeds and display them as you login or in your email (Updates from my favourite websites).
§  Find out the name of the MP3 file you are listening to (My own playlist).
§  Monitor your domain expiry date every day (100’s of Website I manage, for me it’s impossible to remember that which of the domain going to expire and when :P ).
§  Collecting the Web stats of my all websites and their comparison with previous day stats. This way I concentrate on Seo of my Projects.
§  A Notice Board, where I note down all task that have to do on any particular day or time, The script automatically popup a message on screen to remember me tasks.

Practical examples of shell scripting:-
§  Monitoring your Linux system.
§  Data backup and creating snapshots.
§  Dumping Oracle or MySQL database for backup.
§  Creating email based alert system.
§  Find out what processes are eating up your system resources.
§  Find out available and free memory.
§  Find out all logged in users and what they are doing.
§  Find out if all necessary network services are running or not. For example if web server failed then send an alert to system administrator via a pager or an email.
§  Find out all failed login attempt, if login attempt are continue repeatedly from same network IP automatically block all those IPs accessing your network/service via firewall.
§  User administration as per your own security policies.
§  Find out information about local or remote servers.
§  Configure server such as BIND (DNS server) to add zone entries.

How to use Bash scripts?
Open the terminal on your Linux machine and enter the command as follows:
./cyberarmy.sh [Arguments]
Here the script name is “cyberarmy”, format of script is “.sh” and [arguments] – If any input required (Depends upon script).

Advantages
§  Easy to use.
§  Quick start and interactive debugging.
§  Time Saving.
§  Sys Admin task automation.
§  Shell scripts can execute without any additional effort on nearly any modern UNIX / Linux / BSD / Mac OS X operating system as they are written an interpreted language.
Disadvantages
§  Compatibility problems between different platforms.
§  Slow execution speed.
§  A new process launched for almost every shell command executed.