| Home | About Us | Sybase Training | Synergy | Consulting | Job Openings | Tech Videos | Rules and Disclaimer | Search |
![]() |
| Home | About Us | Sybase Training | Synergy | Consulting | Job Openings | Tech Videos | Rules and Disclaimer | Search |
|
UNIX tips: Learn 10 good UNIX usage habits or best practices in unix usage
|
|
03-18-2009, 06:47 PM
Post: #1
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
UNIX tips: Learn 10 good UNIX usage habits or best practices in unix usage
[h2]UNIX tips: Learn 10 good UNIX usage habits : standard or best practices in unix usage[/h2] Introduction When you use a system often, you tend to fall into set usage patterns. Sometimes, you do not start the habit of doing things in the best possible way. Sometimes, you even pick up bad practices that lead to clutter and clumsiness. One of the best ways to correct such inadequacies is to conscientiously pick up good habits that counteract them. This article suggests 10 UNIX command-line habits worth picking up -- good habits that help you break many common usage foibles and make you more productive at the command line in the process. Each habit is described in more detail following the list of good habits. Ten good habits to adopt are:
Make directory trees in a single swipe Listing 1 illustrates one of the most common bad UNIX habits around: defining directory trees one at a time. Listing 1. Example of bad habit #1: Defining directory trees individually
It is so much quicker to use the Listing 2. Example of good habit #1: Defining directory trees with one command
You can use this option to make entire complex directory trees, which are great to use inside scripts; not just simple hierarchies. For example: Listing 3. Another example of good habit #1: Defining complex directory trees with one command
In the past, the only excuse to define directories individually was that your For the few systems that still lack the capability, use the
Change the path; do not move the archive Another bad usage pattern is moving a .tar archive file to a certain directory because it happens to be the directory you want to extract it in. You never need to Listing 4. Example of good habit #2: Using option -C to unpack a .tar archive file
Making a habit of using Combine your commands with control operators You probably already know that in most shells, you can combine commands on a single command line by placing a semicolon ( Run a command only if another command returns a zero exit status Use the Listing 5. Example of good habit #3: Combining commands with control operators
In this example, the contents of the archive are extracted into the ~/tmp/a/b/c directory unless that directory does not exist. If the directory does not exist, the Run a command only if another command returns a non-zero exit status Similarly, the Listing 6. Another example of good habit #3: Combining commands with control operators
You can also combine the control operators described in this section. Each works on the last command run: Listing 7. A combined example of good habit #3: Combining commands with control operators
Always be careful with shell expansion and variable names. It is generally a good idea to enclose variable calls in double quotation marks, unless you have a good reason not to. Similarly, if you are directly following a variable name with alphanumeric text, be sure also to enclose the variable name in curly braces ({}) to Listing 8. Example of good habit #4: Quoting (and not quoting) a variable
Use escape sequences to manage long input You have probably seen code examples in which a backslash (\) continues a long line over to the next line, and you know that most shells treat what you type over successive lines joined by a backslash as one long line. However, you might not take advantage of this function on the command line as often as you can. The backslash is especially handy if your terminal does not handle multi-line wrapping properly or when your command line is smaller than usual (such as when Listing 9. Example of good habit #5: Using a backslash for long input
Alternatively, the following configuration also works: Listing 10. Alternative example of good habit #5: Using a backslash for long input
However you divide an input line over multiple lines, the shell always treats it as one continuous line, because it always strips out all the backslashes and extra
Group your commands together in a list Most shells have ways to group a set of commands together in a list so that you can pass their sum-total output down a pipeline or otherwise redirect any or all of its streams to the same place. You can generally do this by running a list of commands in a subshell or by running a list of commands in the current shell. Run a list of commands in a subshell Use parentheses to enclose a list of commands in a single group. Doing so runs the commands in a new subshell and allows you to redirect or otherwise Listing 11. Example of good habit #6: Running a list of commands in a subshell
In this example, the content of the archive is extracted in the tmp/a/b/c/ directory while the output of the grouped commands, including a list of extracted files, is mailed to the The use of a subshell is preferable in cases when you are redefining environment variables in your list of commands and you do not want those definitions to Run a list of commands in the current shell Use curly braces ({}) to enclose a list of commands to run in the current shell. Make sure you include spaces between the braces and the actual commands, or the shell might not interpret the braces correctly. Also, make sure that the final command in your list ends with a semicolon, as in the following example: Listing 12. Another example of good habit #6: Running a list of commands in the current shell
Use the Listing 13. Example of the classic use of the xargs tool
However, do not think of Passing a space-delimited list In its simplest invocation, Listing 14. Example of output from the xargs tool
You can send the output of any tool that outputs file names through Listing 15. Example of using of the xargs tool
The Listing 16. Example of good habit #7: Using the xargs tool to filter text into a single line
Technically, a rare situation occurs in which you could get into trouble using Know when grep should do the counting -- and when it should step aside Avoid piping a Listing 17. Example of good habit #8: Counting lines with and without grep
An addition to the speed factor, the However, regardless of speed considerations, this example showcases another common error to avoid. These counting methods only give counts of the number of lines containing matched patterns -- and if that is what you are looking for, that is great. But in cases where lines can have multiple instances of a particular pattern, these methods do not give you a true count of the actual number of instances matched. To count the number of instances, use Listing 18. Example of good habit #8: Counting pattern instances with grep
In this case, a call to Match certain fields in output, not just lines A tool like The following simplified example shows how to list only those files modified in December: Listing 19. Example of bad habit #9: Using grep to find patterns in specific fields
In this example, Listing 20. Example of good habit #9: Using awk to find patterns in specific fields
See Resources for more details about how to use A basic-but-common Listing 21. Example of good and bad habit #10: Using grep with and without cat
This mistake applies to many tools. Because most tools take standard input as an argument using a hyphen (-), even the argument for using Conclusion: Embrace good habits It is good to examine your command-line habits for any bad usage patterns. Bad habits slow you down and often lead to unexpected errors. This article presents 10 new habits that can help you break away from many of the most common usage errors. Picking up these good habits is a positive step toward sharpening your UNIX command-line skills. Reference : http://www.ibm.com/developerworks/aix/li...abits.html |
||||||||||||||||||||||
|
« Next Oldest · Next Newest »
|
| Possibly Related Threads... | |||||
| Thread: | Author | Replies: | Views: | Last Post | |
| SED tips & tricks | varungwl | 1 | 3,293 |
07-15-2010 12:48 PM Last Post: mkumar |
|
| 8 Unix Tricks You Probably Didn't Know-Really Nice one | zaadmin | 0 | 1,418 |
11-10-2008 01:33 PM Last Post: zaadmin |
|
| How to install PERL modules on Sun Solaris Unix | zaadmin | 0 | 1,885 |
11-10-2008 01:28 PM Last Post: zaadmin |
|
| AWK Introduction -Learn the Basics | Nagendra | 0 | 5,406 |
06-15-2008 06:31 AM Last Post: Nagendra |
|
| UNIX IN A NUTSHELL | sybaseteam | 0 | 1,272 |
06-04-2008 01:58 PM Last Post: sybaseteam |
|
| Unix Shell Scripting Tutorial - Learning the Korn Shell | sybaseteam | 0 | 4,734 |
06-04-2008 12:46 PM Last Post: sybaseteam |
|