Linux Classes
Share This With a Friend  
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
(Please feel free to answer questions posted by others!)

SloroEleksper     (23 May 2013, 22:21)
Áûëî èíòåðåñíî ïðî÷åñòü ))
ctxznlrh     (17 Jan 2013, 05:44)
pgvcjckp
ankhxxnv     (13 Jan 2013, 02:39)
eskgqs
sohail     (18 Oct 2012, 06:31)
implement a very primitive shell that knows how to launch new programs in the foreground and the background. It should also recognize a few internal commands. More specifically, it should support the following features.

• It should recognize two internal commands: exit and cd. exit should use the exit() system call to terminate the shell. cd uses the chdir() system call to change to a new directory.
• If the command line does not indicate any internal commands, it should be in the following form: <program name> <arg1> <arg2> .... <argN> [&] Your shell should invoke the program, passing it the list of arguments in the command line. The shell must wait until the started program complete unless the user runs it at the background (with &).
<a href="http://printerforum.co.uk/entry.php?149605-http-www.forexbroker.info-you-hereafter-you-shall-see-the-Son-of-man-sitting-on-t">forex broker</a>     (05 Sep 2012, 23:22)
not only out of respect or the sanctity of marriage, but also as a good 11:10. And they that dwell upon the earth shall rejoice over them and and kept his secret, adding, "O King of the time and Caliph of
Mazhar Rizvi     (23 Apr 2012, 08:32)
Hi, this is a very good website for Linux. I need a way to make a list of tar file while making the tar. i can make a list with -t option but i want to make list while creating tar.gz. somebody please help
Doug Myers     (03 Apr 2012, 17:41)
Hi Bob
How do I find where a script resides!?
I now the name of the file (script).
But I want to know where it is located so I can see the contents of the script.

Thanks
Doug
Gladiator     (02 Apr 2012, 14:08)
Hi Bob
Hi every body please tell me how and where should I start for :
write shell to simulate a web browser:
receive the URL.
Parse the URL get the name of the website.
Query the DNS server to get the Ip address of the host.
setup TCP connection.
ask for the file name and receive it display it on my own screen.

I don't know much please guide me to the detail.
Thanks god you are here.
Please respond me at: [email protected]
and [email protected]
Rohit     (23 Mar 2012, 06:19)
Some part of Linux and Unix different anther wise both are same.
Obuv     (15 Mar 2012, 17:28)
Your blog is interesting and will put your blog to bookmarks .
pradeep kumar sharma     (14 Mar 2012, 01:01)
hii,

hw to display the date when opening a terminal..plz mail me..


Liska     (02 Mar 2012, 22:52)
Something I do not see the feedback form.
pravin kumar     (19 Dec 2011, 21:36)
hii i want a script that define a palindrome no
ANCHAL     (10 Dec 2011, 05:22)
hello sir please send me the basic difference between linux and unix programming
Arlene     (29 Nov 2011, 23:56)
No offense, but i suggest adding a facebook like button for the blog!
Majed     (28 Nov 2011, 09:18)
@kumar

#!/bin/sh
/path/to/oldfile>>/path/to/newfile
Hiral Chauhan     (28 Nov 2011, 08:56)
VERY INFORMATIVE website!! Kudos!!
I can say, I learned almost all my beginner UNIX from here!! :)

Thankss
Saurabh Kumar     (27 Nov 2011, 03:28)
Hi, plz tell me how i add content of one file in another file through shell programming, plz give the code..........thanks
jatinder     (20 Nov 2011, 01:45)
how we can grep the appache tomcat PId and kill that id with the help of script
kuldeep sinha     (17 Nov 2011, 14:09)
write a shell script to count the no of characters of each command line arguments.....
Balu     (17 Nov 2011, 10:11)
hey can you help me out for the command how to catch the particular user who is creating load on the server,Also spammer who is spamming.
RAJIB KUMAR SAHOO     (17 Nov 2011, 01:03)
hello sir, i need to help, plz give me all user command,editor& shell programming in unix

and also some shell programming
sarath     (21 Oct 2011, 05:18)
I want to record time of the commands given to linux terminal .I am trying script command. will script can copy router and switch commands.OR they another way record the time of command .please read this and me the answer
ashir omary     (20 Oct 2011, 09:21)
give 5 examples of computer scripting languages with syntax examples to illustrate their coding
Akshada     (08 Oct 2011, 07:27)
hello

I am developing a project in linux wherein i can turn off or halt the processes automatically which are not required by the user.. i want to write a batch file containing process control commands how to write that..
secondly can we pass parameters to batch file and how?

where can i get the complete code of fedora.. what will be easy modifying the existing code or writing the new one for this project
bheem singh panwar     (07 Oct 2011, 01:34)
Hello Sir,

This is Bheem Singh Panwar for delhi
sir tell me how to create ldap user through command.
tazzy     (08 Sep 2011, 15:19)
gedit tazzy.sh
then type in gedit
#!/bin/bash
echo "hi i m tazzy"
# this hash means comment line
sonia     (05 Sep 2011, 08:15)
hello sir
it is gud to take help from low fat linux but m not able to find out tcsh shell commands plz send me the link 4 the same.
in menu almost commands are from bash but wat abt tcsh and pdksh.
giri     (29 Aug 2011, 10:19)
Please help me to solve this problem, I need to write a shell script that runs in background and intimates the system administrator whenever any user login/logoff.
Gayu     (26 Aug 2011, 11:08)
Thanks Bob ! Very useful site...
tina jadhav     (24 Aug 2011, 13:57)
hello sir, i need to help, plz give me all user command,editor& shell programming in unix
harishram     (22 Aug 2011, 05:11)
sir can u tell me how to create process in linux
Sergiej     (30 Jul 2011, 04:43)
I like this website, will back here everyday! This is what i was looking for
Abe lere     (07 Jul 2011, 17:26)
How can I extract softwares that I installed on my Linux machine (e.g. openoffice, SQL, ...) instead of maually colletting then from /bin, /sbin... I know how to extract the packages using the
rpm -qa > test.txt
sunny     (05 Jul 2011, 08:14)
very good
rajkumar     (02 Jul 2011, 06:03)
hi how to improve the linux programming
sagar patil     (02 May 2011, 13:18)
hi sir, i want to know about how to find out polindrom in shell programming.
bandish dave     (18 Apr 2011, 06:39)
hi doc,plz give the shell script pgm that execute openerp gtk client and server
Vida     (09 Apr 2011, 00:46)
Hi Bob
Hi every body
please tell me how and where should I start for write shell to simulate a web browser:
>receive the URL.
>Parse the URL get the name of the website.
>Query the DNS server to get the Ip address of the host.
>setup TCP connection.
>ask for the file name and receive it display it on my own screen.
I don't know much please guide me to the detail.
Thanks god you are here.
Zubair Shah     (07 Apr 2011, 01:16)
hi doc
i need how to program for shell
rwRasda     (06 Apr 2011, 05:02)
Hack again?!
Krishna     (19 Mar 2011, 03:53)
Hai sir,
how to write a shel pgm,
my question is, write a pgm to create 10 process
vaishal     (10 Mar 2011, 02:43)
what a shell script that print a list of every unique word in a file in revers order?
dharmar.N     (17 Feb 2011, 07:51)
I am studying in mepco schlenk engineering college in secound year .my operting system project is createting own shell .so please guide me and basic information to me .please replay sir .waite for your mail thinking you
dinesh     (07 Feb 2011, 02:41)
dear sir
i want to know how to create Shell Script and give the rights to user and how to excute that script
umer farooq     (31 Jan 2011, 08:22)
sir i'm working in linux first time..so i have no idea to create the shell....
so kindly send me the procedure and code of shell creating
haziz habbeboy     (31 Jan 2011, 04:39)
pls bob am a new beginner in lunix,can anybody pls teach me how to start with it...........my email is [email protected] or +2347987138866
shrenik bb     (13 Jan 2011, 00:46)
sir
i want shell script program on palindrome,terminal locking,child process fork
Martin Hookem     (10 Jan 2011, 11:48)
Hello! I want to write a small script using my text editor.
It's basically a hexdump:-

cd /system/framework
hexdump -C -x 'filename' > hexdump.txt

How would I get it to write to, for example, hexdump2.txt hexdump3.txt etc.. So that I can then compare the files afterwards.

regards

Martin
Bob Rankin     (12 Dec 2010, 12:43)
@tagel aboneh - Use the tail command and then the ">>" operator to append to another file.

ex: tail -5 input.txt >> output.txt
tagel aboneh     (12 Dec 2010, 07:08)
i need assistance on how to writte aprogram that read information from the end of one file and appends to another
germanfranc41     (08 Dec 2010, 02:41)
What a nice site!! Aside from learning you can also ask questions to the creator!! COOL
[email protected]     (06 Dec 2010, 02:37)
Hi
I want to create Script file of 5 command,
plz help me how to creat and I am not Expert in Linux.
Command 1. upload_file 2.add_to_sinker 3.upload_file 4. storecmd 5. start_tap

plz create 1 file for all command given and all command will take some time randomly.
EddyBeerke     (10 Nov 2010, 10:09)
Hi,
I'm working on a script, but the cd command dos'nt work.
I need some help here:

#!/bin/bash
dir="$(zenity --file-selection --directory --title="Selecteer een map...")"
echo "Map is geselecteerd:"
echo $dir

# the next line dos'nt work
cd $dir
Moni Boro     (05 Nov 2010, 01:56)
write a shell script that prints a list of every unique word in a file in reverse order?
alexc210     (22 Sep 2010, 02:27)
Very nice site!
Leif     (14 Sep 2010, 18:31)
Dr. Bob,
While doing a quick search for shell scripting for linux I found your site. I like your style man, and style you have!!!
Keep up the cool stuff, I had to pass the url to a few friend.
Take care,
Leif Hanson
alexf94     (06 Sep 2010, 14:08)
Very nice site!
Bob Rankin     (06 Sep 2010, 09:41)
@satish - To save time, here are all the letters of the alphabet: abcdefghijklmnopqrstuvwxyz. Write a simple program to generate all possible combinations of those letters, and then you'll have a list of all the linux commands.
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

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.