28 June, 2011

Command Line Magic: Scripting, Part One

Here is one good article on linux scripting…

 

Command Line Magic: Scripting, Part One

Creating shell scripts is the lazy (smart) system administrator's way to automate and build intelligence into repetitive tasks.

 

http://www.linux-mag.com/id/8747/     

This scripting miniseries will help you learn some basic scripting and perhaps launch you on a more productive career in Linux system administration. If you find yourself typing the same commands over and over again, do yourself a favor and write a script to do it. Smart administrators have scripts for everything. There are three things you have to remember about scripting: Don’t test your scripts on production systems, document the script’s action, and name the script descriptively. This week’s article introduces you to simple shell scripting.

There’s no great secret to creating good scripts. You don’t have to be afraid of criticism about script security, programming style, or which editor you use to create them. You don’t need to be a programmer or even a Linux expert to create some pretty cool scripts. All you really must do is test your scripts, document your scripts, and give your scripts descriptive names.

Test your scripts to make sure they work like you expect them to. Document your scripts inside and outside the script so that you don’t forget what the script’s original purpose is and that anyone else using the script will know how to use it. Name your scripts such that its functionality is at least hinted at from the name.

Those of you who are new to Linux might ask, “Why do I need to create scripts?” The answer is simple, you don’t need to but once you’re worn out from typing the same commands several times per day, you’ll want to create them.

Scripts help you manage your system(s) more efficiently. And, the rule of thumb is, that if you’re typing it, you can script it.

The Basics

I only use the Bourne-Again Shell (BASH)* for scripting. It’s the default shell in Linux and it has the best features from the other major shells. But don’t worry if you don’t use BASH, you can still write scripts that use BASH, even if you use a different user shell. The prerequisites for gaining the most out of this series are that you know how to use basic shell commands. For example, cd, ls, ps, cat, chmod and a command line text editor such as vi, emacs, or nano.

I only use the vi** editor in the examples.

This series is interactive, so you’ll need access to a Linux system, physical or virtual, with which to work. If you’re required to use root user privileges or sudo access, I’ll note that in the example.

At the Command Line

To keep my scripts in this separate from everything else, I created an area in my home directory for them under scripts/intro. Shell scripts are simply commands contained in a file (a shell script file) that run as if you had run them individually at the command line. Enough introduction, let’s get started with some shell scripts.

Open your favorite text editor and enter some commands into it on a one command per line basis. To run the script within a BASH shell, enter #!/bin/bash.

#!/bin/bash

This line, which must be the first line in your script, is the only line that may begin with the # (shell) symbol. Placed anywhere else in the script and the shell interpreter ignores it and anything on the line after it. This is the comment symbol everywhere but on the first line. Continue by adding some commands as shown.

#!/bin/bash
touch file.txt
echo "Hello World" >> file.txt
echo "Create another file" > file2.txt
ls -la
cat file.txt
echo " "
cat file2.txt

Save the file with a descriptive name (no spaces) and with a .sh at the end of the name. For example, file_create.sh. The file is simply a text file, not a script at this point. Do you know why? It’s because the file permissions don’t allow execution. To change the behavior, enter the following command:

$ chmod +x file_create.sh

Now, execute the file and allow it to create the two files and display their contents.

./file_create.sh
 
total 20
drwxr-xr-x 2 khess khess 4096 2011-05-30 16:01 .
drwxr-xr-x 3 khess khess 4096 2011-05-30 15:35 ..
-rw-r--r-- 1 khess khess   20 2011-05-30 16:01 file2.txt
-rwxr-xr-x 1 khess khess  141 2011-05-30 15:55 file_create.sh
-rw-r--r-- 1 khess khess   12 2011-05-30 16:01 file.txt
Hello World
 
Create another file

As demonstrated in the script, you can enter as many commands as you want into a script. They will execute one after the other, from top to bottom, until the script completes.

For a practical application of the script you created, try this one that checks the lastlog and then mails you the list of user who’ve logged in during the current month.

#!/bin/bash
 
echo $HOSTNAME > logins.txt
echo " " >> logins.txt
last >> logins.txt
mail ken@kenhess.com < logins.txt

Note that this file records the hostname to help you identify which hosts your messages originate from, in case you have more than one that you manage.

Interactive Scripting

What if you need more flexibility in your scripts that you just can't program to run autonomously? You program them to run interactively and provide the intelligence as needs and circumstances change.

This script prompts you for a filename, some initial content for the file, and then writes the provided information to the specified filename.

#!/bin/bash
 
echo "Enter the name of the file you wish to create."
read FILENAME
echo "Enter any info you want to enter into the file."
read INFO
touch $FILENAME
echo $INFO >> $FILENAME
cat $FILENAME

Grant execute permission to the file and run it. Does it produce the expected results? Do you see any other useful applications for the scripts you've learned today? How about interactively adding information into a database?

Next week, you'll gain considerably more experience writing scripts now that all of the trivia and introductory material is out of the way. If you have particular problems you'd like to solve, use the Comments section to address them. Other readers might have some good ideas or I can tackle them in upcoming posts. Try out a few scripts on your own from the ones you've learned here and post them for others to use.

Remember, that we're an interactive, positive community that wants to foster open communications. Everyone comes in from a different place and it's our job to nurture learning and discovery. Until next week, happy scripting!

* Watch for my upcoming, Korn Shell tale post.

** It's the first one I learned from old UNIX nerds who thought that vi is the only editor. What can I say?

Kenneth Hess is a Linux evangelist and freelance technical writer on a variety of open source topics including Linux, SQL, databases, and web services. Ken can be reached via his website at http://www.kenhess.com. Practical Virtualization Solutions by Kenneth Hess and Amy Newman is available now.

 

 

Best Regards,

Chirag Gandhi

Project Manager

 

Sun Point

Nr. Parimal Garden  Ellisbridge

Ahmedabad 380 006   India

 

M   +91 9376109333

T   +91 7939820000

MudraCorporate

www.mudra.com

http://tinyurl.com/5v5h52f

 

Railways blocks 4.6L user IDs for e-ticket fraud through irctc website

 

MUMBAI: The Indian Railway Catering and Tourism Corporation (IRCTC), which looks after online booking of tickets, has blocked over 4.6 lakh user IDs and 71,484 e-mail IDs since March to curb malpractices in train reservations.

 

The IRCTC has restricted the number of bookings in a day through a single user ID to two. Also, individual users cannot book more than 10 tickets in a month. Agents have been barred from booking tickets online during the first hour after the bookings open at 8 am. This has been done to curb black marketing of ticket as many agents (authorized and unauthorized) had create multiple user IDs to corner the tickets.

 

More details on the following link:-

 

http://timesofindia.indiatimes.com/india/Railways-blocks-46L-user-IDs-for-e-ticket-fraud/articleshow/9007215.cms    

 

Best Regards,

Chirag Gandhi

Project Manager

 

Sun Point

Nr. Parimal Garden  Ellisbridge

Ahmedabad 380 006   India

 

M   +91 9376109333

T   +91 7939820000

MudraCorporate

www.mudra.com

http://tinyurl.com/5v5h52f

 

17 June, 2011

FW: Friends spread it like a wild fire - Jan lokpal bill in Simplified English

Jago Bharat ke Nagrik Jago…

 

Truly

 

Of the people, by the people, for the people...

 

cg

 

Relativity applies to physics, not ethics.

~~~ Albert Einstein

 

From:
Sent: Thursday, June 16, 2011 6:30 PM

Subject: Friends spread it like a wild fire

 

Friends,

 

This is a presentation on Jan lok pal bill.. 

 

Request you to kindly share it with everyone and if you get opportunity discuss it with your friends and family.

 

Lets spread it like wild fire.. 

We will not get this opportunity again. This is the time to make the governmental realize that enough is enough. They have fooled and looted the country for last 63 years but now no more.

 

They will have to pass Jan lokpal or they will be booted out of power in next election and only that party will win who will implement Jan lokpal bill. 

 

Please do this at least for your mother land so that your future generations can live in peace and prosperity. 

 

----- Original Message -----

From:
Sent: June 15, 2011 4:30 PM

Subject: Friends spread it like a wild fire

 

 

>>>>> 

>> 

----- Original Message -----

From:
Sent: June 15, 2011 1:30 PM

Subject: Friends spread it like a wild fire

 

> 

> 

> 

> 

 

 

----- Original Message -----

From:
Sent: June 15, 2011 11:30 AM

Subject: Friends spread it like a wild fire

 

15 June, 2011

Aaj ki Mahabharat - Political

Good one…

 

http://www.youtube.com/watch?v=n-LuNG6pybE&feature=player_embedded 

 

Best Regards,

Chirag Gandhi

Project Manager

 

Sun Point

Nr. Parimal Garden  Ellisbridge

Ahmedabad 380 006   India

 

M   +91 9376109333

T   +91 7939820000

MudraCorporate

www.mudra.com

http://tinyurl.com/5v5h52f

 

07 June, 2011

Hackers target Sony, Nintendo and FBI partner Web

Just for your info…

 

If website and data of FBI/NASA/Sony/gmial/VISA / MasterCard/American Express etc.. credit card/Incometax Dept. India can be hacked then I thing nothing is secure on internet…

 

HACK : Hackers leak FBI passwords on internet

http://www.smh.com.au/technology/technology-news/hackers-leak-fbi-passwords-on-internet-20110606-1fnvc.html

FBI partner attacked by hackers, passwords taken

http://www.forbes.com/feeds/ap/2011/06/05/technology-tec-fbi-partner-data-breach_8501273.html

Hackers report breach of FBI passwords

http://www.computerworld.com.au/article/389107/hackers_report_breach_fbi_passwords/      

 

Hackers claim to have hit Sony again

http://www.reuters.com/article/2011/06/06/us-toni-cybersecurity-sony-idUSTRE75563220110606

 

Hackers target Sony, Nintendo and FBI partner Web site

Read more: http://news.cnet.com/8301-27080_3-20068890-245/hackers-target-sony-nintendo-and-fbi-partner-web-site/#ixzz1OZCpBvic

Sony Pictures ‘Looking Into’ Claims of New Data Theft From Site by Hackers

http://www.bloomberg.com/news/2011-06-02/sony-pictures-looking-into-claims-of-new-data-theft-from-site-by-hackers.html

 

 

Best Regards,

Chirag Gandhi

Project Manager

 

Sun Point

Nr. Parimal Garden  Ellisbridge

Ahmedabad 380 006   India

 

M   +91 9376109333

T   +91 7939820000

MudraCorporate

www.mudra.com

http://tinyurl.com/5v5h52f