The Shell as a Programming Language

Now that you’ve seen some basic shell operations, it’s time to move on to some actual shell programs. There
are two ways of writing shell programs. You can type a sequence of commands and allow the shell to execute
them interactively, or you can store those commands in a file that you can then invoke as a program.
Interactive Programs
Just typing the shell script on the command line is a quick and easy way of trying out small code fragments,
and is very useful while you are learning or just testing things.

Suppose you have a large number of C files and wish to examine the files that contain the string POSIX.
Rather than search using the grep command for the string in the files and then list the files individually,
you could perform the whole operation in an interactive script like this:

$ for file in *
> do
> if grep -l POSIX $file
> then
> more $file
> fi
> done
posix
This is a file with POSIX in it - treat it well
$

Note how the normal $ shell prompt changes to a > when the shell is expecting further input. You can
type away, letting the shell decide when you’re finished, and the script will execute immediately.
Related Posts:
  • The Shell as a Programming Language Now that you’ve seen some basic shell operations, it’s time to move on to some actual shell programs. There are two ways of writing shell programs. You can type a sequence of commands and allow the shell to execute them inte… Read More
  • TCP/IP Firewall The combined set of protocols is called the Transmission Control Protocol and Internet Protocol TCP/IPprotocol suite. As a network administrator, it is important that you understand the nature of potential attacks on co… Read More
  • Linux Port Scanning Listing your open ports and firewall configuration with netstat and iptables is a good start for staying on top of unwanted traffic, but you may want to go a step further with Nmap Network Mapper, a program that … Read More
  • what is IMAP IMAP, fully documented in RFC 3501, was designed to provide a robust, mobile mail delivery and access mechanism. For more detail on the protocol and how it functions on the network layer, or for additional information on… Read More
  • What Is a Shell? Before jumping in and discussing how to program using a shell, let’s review the shell’s function and thedifferent shells available for Linux. A shell is a program that acts as the interface between you and the Linuxsystem, e… Read More
  • Linux-Essential Root Subdirectories bin Contains binary programs also known as an executables, which are programs that are ready to run. Most of the basic Unix commands such as ls and cp are in /bin. However, some of the files in /bin are not in binary … Read More
  • Linux Software websites  Linux Software websites Internet Sites Description ftp.redhat.com Red Hat distribution RPM packages rpmfind.net RPM package repository sourceforge.net Source Forge open source software reposit… Read More
  • How to create update or remove symbolic or soft link Linux Symbolic links , Symlink or Soft link in Unix are very important concept to understand and use in various UNIX operating systems e.g. Linux , Solaris or IBM AIX. Symlinks gives you so much power and flexibility that you can… Read More
  • Configuring IPv6 Networks At the beginning, IANA gave requestors an entire class A network space thereby granting requestors 16.7 million addressesmany more than necessary. Realizing their error, they began to assign class B networksagain, provid… Read More
  • What Is an Operating System? An operating system is made up of software instructions that lie between the computer hardware (disks, memory, ports, and so on) and the application programs (word processors, Web browsers, spreadsheets, and so on). At t… Read More
  • The Role of the Device Driver As a programmer, you are able to make your own choices about your driver, andchoose an acceptable trade-off between the programming time required and the flexibilityof the result. Though it may appear strange to say that a d… Read More
  • Common Linux Features? Multiuser — Not only can you have many user accounts available on a Linux system, you can also have multiple users logged in and working on the system at the same time. Users can have their own environments arranged th… Read More
  • What Is IP Filtering? IP filtering is simply a mechanism that decides which types of IP packets will be processed normally and which will be dropped or rejected. By dropped we mean that the packet is deleted and completely ignored, as if it h… Read More
  • What is Shell Script Normally shells are interactive. It means shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of 'n' number of commands) , the you can store this sequence of command … Read More
  • mkdir and rmdir You can create and remove directories using the mkdir and rmdir system calls. #include <sys/types.h>#include <sys/stat.h>int mkdir(const char *path, mode_t mode); The mkdir system call is used for creating direc… Read More