The Beginner's Guide to Linux Part 4: Introduction to the Terminal
Posted 04/07/09 at 08:00:00 PM by Will Kraft
Program targets
Many programs provide you with the option of immediately acting against a specific target entity when the program starts. This target entity may be practically anything, (a file, directory, network address, or system device) depending on the nature of the program. A target may be located anywhere in the filesystem, and it may be invoked from any directory (provided the pathname is correct) For example, if you are in your home director and wish to edit a file in a subfolder, there is no need to change to the subfolder before opening the file.
Generally, the target should be specified after the base command and any switches. (e.g. “vim -R myfile.txt”) Sometimes, switches may have targets of their own in addition to the main program target. In such instances, the manual page will tell you what types of switch/target combinations the program will accept.
Although most filenames are either single words or separated by underscore characters (e.g. file_1) The terminal supports multiple worded filenames as target entities. However, spaces in filenames must be preceded by a backslash, otherwise the shell will treat each word independently. (often causing an error) For instance, Bash would correctly process “multiple\ worded\ filename.txt” as “multiple worded filename.txt”. Omitting the backslashes would cause the shell to incorrectly interpret the filename as “multiple”, “worded”, and “filename.txt”.
Working with Input and Output
Joining commands together
If you have two or more commands that you wish to run in sequence, type them on the same line and place “&&” between each separate command. The commands will then be executed in order, from left to right.
Pipelines
Software pipelines allow you to feed the output from one program directly into another. Like the && operator, pipelines allow you to execute multiple commands in a specific sequence. However, a pipeline automatically takes the output from the previous command and feeds it into the next command whereas the && operator treats each command independently. Pipelines are useful primarily because they allow further processing of a command and allows multiple interdependent commands to be chained together when necessary. The pipeline is invoked by placing the “|” symbol between two separate commands on the same line.
For example, the grep utility is designed to search for a specific keyword you provide. If you were to run a program like ls, you can use grep to isolate a certain file (e.g. file1.jpg) from what could be a very long file list, even with wildcard filtering. If you were to run “ls | grep file1.jpg”, only file1.jpg would be listed in the terminal (if it exists) even though ls might return much more output under normal conditions.
Output Redirection
Sometimes, you may want to save terminal output. While you could use a pipeline to dump it into a text editor, there is an easier way. Using the greater-than (>) symbol, you can save terminal output to a text file. For example, running “cat file1 file2 > file3” will join the contents of file1 and file2 together and will then save the resulting output as file3.
Be careful with this, since any existing file with the same name as the one you specify will automatically be replaced. To append the output to an existing file, use “>>” instead of “>”.
Shell Scripting Concepts
While the simple operators described in the previous section can allow you to create complex commands, true shell scripting can do much more by taking the principles described in the previous section to a whole new level. A Linux shell script works just like a Windows batch file in a sense; both are little more than a set of instructions that rely exclusively on external programs to do the actual work. This differs fundamentally from programming languages like Perl, which use internal language features instead of external programs. Shell scripting is based on the principle of using various simple programs to produce a complicated result.
Shell scripting is a useful tool for creating a stopgap solution for a problem in a few minutes using existing tools instead of having to re-invent the wheel by developing a program of your own. In this way, shell scripting has its strengths and weaknesses like any other language: Shell scripts are easy to write if you have any programming experience at all and can save considerable development time by using programs that already have the functionality you need. On the downside, shell scripts are written for specific shells and are not very portable as a result. (According to an old saying, it is often easier to port an entire shell than a complicated script that relies heavily on the features of one particular shell) Likewise, shell scripts are not ideal for permanent solutions and are not really designed with security in mind. As with any other language, you should decide beforehand whether the benefits of shell scripting are enough to offset its downsides for your particular situation.
Shell scripts support all the essential features of any scripting language, including conditional statements, variables, loops, etc. Since a shell script is little more than a bunch of individual commands kludged to work together, all the functionality the script might use is available for individual commands as well. (although it is inconvenient to do things that way)
To run a shell script, navigate to the directory it is in and run “./<script name>” (e.g. ./shellscript.sh) Do not omit the period and forward slash at the beginning or the script will not work. Also, you may have to make scripts executable with chmod before you will be able to run them.
Read on for our table of Essential Terminal Commands!
Why?
Submitted by RavenStandsAlone on Thu, 05/07/2009 - 7:05am
Why? Isn't it easier to just click on an icon on the desktop? Why would any sane person want to do it the hard way?
These are the InterWebs? Where's the bicycles, babes and beer?
Sweet Jeebus... thats a lot.
Submitted by Stry8993 on Mon, 05/04/2009 - 12:52am
Well, practice, practice, practice. Thanks.
Feature
Review
Feature
Feature
Feature






