Linux Classes
Linux Training
* Linux Classes *

LINUX CLASSES - PROGRAMMING

Can Non-Geeks Do Linux Shell Programming?

Even if you think you're not a programmer, Linux has some features to make your life a bit easier. Any time you have a repetitive task that involves entering Linux commands or changing the contents of a file, you should think about automating it with a program.

If you've ever written a word processor macro or a spreadsheet formula, you're a programmer. By taking advantage of Linux's built-in programming features, you can automate repetitive tasks and build simple interactive applications without a degree in computer science.

This section covers basic shell script programming and introduces you to other, more powerful programming tools available in Linux, such as Perl and the C and C++ programming languages.

Basic Shell Programming

The Bash shell is your main port of entry to Linux, since the shell interprets everything you enter on the command line before passing it along to the operating system for execution. But in addition to providing the niceties of command resolution, wildcard handling, and piping, Bash has its own powerful built-in programming language.

A shell script is a program written for Bash. The simplest shell script is a sequence of Linux commands, but when you add the power of variables and flow control, you can do a lot more with it. Shell scripts are similar to DOS batch files (those files that end in .bat), but shell scripts are more powerful and actually easier to create.

Shell scripts are interpreted, which means that the shell reads each line and acts on it immediately. This process differs from that of a formal programming language like C or C++, where the program is compiled and optimized for faster execution. So there's a tradeoff--it's easier to create quick little shell scripts, but if the task at hand requires serious number crunching or complicated logic, a compiled language is better.

Note: All of the shell script syntax and examples in this section will work in both the Bash and Korn (pdksh) shells. The C shell (tcsh) has subtle differences in many areas, so scripts written for one shell may not work in another. If you decide to use the C shell instead of Bash, use the man tcsh command for more information on writing shell scripts for that environment.

Creating a Shell Script

To create a shell script, use a text editor and enter your Linux commands as if you were typing them at the command prompt. For example, try this:

cd /tmp
echo "Removing temp files..."
ls -al
rm junk*

If you save those four lines in a file named deltemp, you will have a simple shell script that automates the process of switching to the /tmp directory, listing all the files there, and deleting the ones that start with the word junk.

Previous Lesson: Pipe Fitting
Next Lesson: Executing a Script

[ RETURN TO INDEX ]

Comments (most recent first)

satish hambarde     (29 Aug 2010, 04:32)

please
send me all commands used in linux
Johnboy     (24 Aug 2010, 15:56)
....assuming you have files in your working directory called junk*
Johnboy     (24 Aug 2010, 15:52)
I have one question about the script above.If the /tmp directory does not exist (I know it should on any Linux box), will the contents of your current working directory be deleted instead?
ralson     (08 Aug 2010, 07:27)
hii,

hw to display the date when opening a terminal..plz mail me..
Bob Rankin     (16 Jul 2010, 15:04)
@Tebogo - I did! My book "The No BS Guide to Linux" was published by NoStarch Press, and served as the basis for this website. :-)
Tebogo     (16 Jul 2010, 05:44)
Man, u GREAT. U should write ur own Linux Book. Things makes sense, when executing/viewing LINUX commands and scripts.
KK     (24 Jun 2010, 05:10)
@Bob

string="xyz is 123,abc is 345, ....."
can anyone help me writing regular expression to match "abc is 345" ?
Mwaura Sammie     (16 Jun 2010, 00:45)
Bob thanks for your great work in devoting your precious time to maintain this helpfule website.Its really helpful
Bob Rankin     (21 May 2010, 13:25)
@Krogen - Ideas? Yes, I have the idea that you want me to do your homework! :-)
I am Krogen     (21 May 2010, 12:11)
Hey Doc,
I am trying to create a script that asks the user for a file and then appends the date to the file the user has entered. I need to let the user know if the file can or cannot be written too. Also, if the file does not exist create the file and write the date in the newly created file. Lastly display to the user the file, including a statement letting them know that the file has changed.

I know how to do some parts of this, but not all. Nor can I figure out how to put it all together with IF, ELSE, etc into one script.

Any ideas?
Bob Rankin     (17 May 2010, 06:56)
@Ben - Very briefly, you want a web page with a form that invokes a CGI script via the ACTION parameter. That CGI script will then call rigctl.

You can find lots of examples of simple web forms and CGI scripts if you search online, but here it is in skeleton form:

The HTML form:

<form method="POST" action="/cgi-bin/doit.pl">
<input type="submit" value="Do It!">
</form>

--------------------------------

The "doit.pl" script:

#!/usr/bin/perl --
print "Content-type: text/html\n\n";
# Run a system command
system("command", "arg1", "arg2", "arg3");
Ben Z     (16 May 2010, 00:26)
Bob,
Youre information is very helpfull. But, there's always a but, I still have a question. Google is currenly overheating from my searches so I'll try to ask you. Currently I'm running the Hamlib utils from an embedded Linux system. All's running well, I can use the rigctl tool from the command line and let the connected radio do what I want.

Now I'm looking for the way to control rigctl from a website. Apache is running, CGI is enabled but I do not have a clue how to start.

All I want is a page with buttons where each button sends it's displayed frequency to rigctl.

I hope you can give me a direction where to look.

Thanks in advance,
Ben Zandstra
(B.T.W: How about a donation page for your good work ?)
Ravi Kudache     (02 Mar 2010, 22:49)
vrey nice
Philemon leha demang     (10 Feb 2010, 03:10)
hi doc,pls notify me about new facts on linux and all document it entails.out

*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 

Ask Bob Rankin - Free Tech Support
<Send This Link to a Friend>         <Bookmark This Page>


Copyright © by Bob Rankin
All rights reserved - Redistribution is allowed only with permission.