Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Unix Operating Systems Software

How To Adopt 10 'Good' Unix Habits 360

An anonymous reader writes to mention an article at the IBM site from earlier this week, which purports to offer good Unix 'habits' to learn. The ten simple suggestions may be common sense to the seasoned admin, but users with less experience may find some helpful hints here. From the article: "Quote variables with caution - 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 square brackets ([]) to distinguish it from the surrounding text. Otherwise, the shell interprets the trailing text as part of your variable name -- and most likely returns a null value."
This discussion has been archived. No new comments can be posted.

How To Adopt 10 'Good' Unix Habits

Comments Filter:
  • Re:Don't use shell (Score:4, Insightful)

    by Anonymous Coward on Saturday December 16, 2006 @07:31AM (#17267448)
    or even better -- use perl.
  • mkdir (Score:3, Insightful)

    by pfafrich ( 647460 ) <richNO@SPAMsingsurf.org> on Saturday December 16, 2006 @07:34AM (#17267468) Homepage
    His example of good habit with mkdir did not convince me

    $ cd tmp/a/b/c || mkdir -p tmp/a/b/c

    If the directory exists you end up in the directory, if it does not it creates the directory but leaves you where you first started. Hence you don't know which directory you will be in after the command is executed!

  • by petes_PoV ( 912422 ) on Saturday December 16, 2006 @07:39AM (#17267484)
    This article is really just about good (in the authors opinion) TTY shell usage. There's more to Unix than just its shell
    (plus he didn't mention my favourite shortcut: shell history)

    How about being more inclusive and expanding this to deal with security features (surely the single biggest benefit?) and the ease of working on remote boxes?

  • Re:Don't use shell (Score:5, Insightful)

    by Anonymous Coward on Saturday December 16, 2006 @07:46AM (#17267512)
    OK, I agree. Please provide a concise Python script that unpacks a tarball (a .tar.gz or .tar.bz2 file), copies new files in to a said tarball, patches based on the contents of said new files, runs make from various directories in said extracted tarball, and then changes the name of the top-level directory created by the tarball to a new name and repacks the tarball.

    Or a concise Python script that opens up a text file of URLs, and extracts the files listed in the URLs:

    #!/bin/sh
    for a in $( cat file | awk '{print "'\''" $0 "'\''"}' ) ; do
                  wget $a
    done


    Python has it place, and is far better for medium to large projects, and projects where the code needs to be maintainable. Shell, however, works a lot better for automating UNIX tasks than Python does. Not to mention embedded systems: I can compile Busybox to have both a good shell and all of the commands that one would run from shell scripts (including grep, cut, sed, and, yes, awk) in only about 300k. A Python binary is about a megabyte big, and you need about ten megabytes to fit all of the libraries Python 2.4 comes with.
  • Re:welll.. (Score:5, Insightful)

    by AchiIIe ( 974900 ) on Saturday December 16, 2006 @07:55AM (#17267556)
    Some of the points he is making are BS. They are not good `Unix habits` they are simply hacks that marginally reduce the workload but (arguably) increase complexity.

    Ie there is NOTHING bad about piping cats. While you might indeed get a ~30% performance increase if you skip the cat, the complexity increases. We often sacrifice performance in order to increase abstraction and understanding.

    What makes unix so powerful is its modularity, the fact that you can pipe any output from any application to any applications stdin. This makes it possible to use common tools app1 | app2, app1longoutput | grep thingsIwant. The possibility to mix and match common elements that (arguably) makes unix powerful.

    Advice that says "stop piping cats" is akin to "stop using helper functions, they overload the stack, instead do everything in one function"

    --
    A better articulated article on the programmers intellectual ability vs proper abstraction techniques:
    http://www.acm.org/classics/oct95/ [acm.org] - Dijkstra, Edsger - "Go To Statement Considered Harmful"
  • Re:Don't use shell (Score:3, Insightful)

    by Anonymous Coward on Saturday December 16, 2006 @08:03AM (#17267574)
    Someone mod up parent and grandparent, PLEASE!

    No, don't mod up anybody in this thread. Perl and Python are abominations. Pure, unadulterated Bourne shell is for the true, seasoned *nix user. Just like Java is an answer to a question nobody asked in the GUI world, so too is Perl and Python in the command line world.
  • Re:mkdir (Score:5, Insightful)

    by SharpFang ( 651121 ) on Saturday December 16, 2006 @08:36AM (#17267716) Homepage Journal
    Especially the habit of using || and && on command line seems ridiculous to me. These have room in two situations:
    - scripts
    - commands that take long enough that you go have a coffee.

    This makes sense:

    make install && lilo && reboot

    This doesn't:

    cd tmp/a/b/c || mkdir -p tmp/a/b/c

    If you fail the first part, well, you typed " || " instead of pressing enter.
    If you succeed the first part, you typed " || mkdir -p tmp/a/b/c" without a bloody reason.

    Type first part. Press enter. Observe result.
    If necessary, type the second part, otherwise correct the first without baggage of the second one hanging around.
  • Re:welll.. (Score:3, Insightful)

    by t_ban ( 875088 ) <tathagatabNO@SPAMgmail.com> on Saturday December 16, 2006 @08:39AM (#17267732)
    Some of the points he is making are BS. They are not good `Unix habits` they are simply hacks that marginally reduce the workload but (arguably) increase complexity. Ie there is NOTHING bad about piping cats. While you might indeed get a ~30% performance increase if you skip the cat, the complexity increases. We often sacrifice performance in order to increase abstraction and understanding. What makes unix so powerful is its modularity, the fact that you can pipe any output from any application to any applications stdin. This makes it possible to use common tools app1 | app2, app1longoutput | grep thingsIwant. The possibility to mix and match common elements that (arguably) makes unix powerful. Advice that says "stop piping cats" is akin to "stop using helper functions, they overload the stack, instead do everything in one function"

    but he never said you should stop using pipes. he is talking only about a specific situation -- cat-ing a file and then piping it to grep. surely that is a good point he is making, because grep already takes filenames as an argument?

  • Re:This article... (Score:1, Insightful)

    by Anonymous Coward on Saturday December 16, 2006 @08:59AM (#17267808)
    Running subshell commands using ; instead of && ?

    Ah, you must be a teacher, seeing something that remotely resembles a common error, and immediately assuming that it was an error. The article explains both ; and &&...

    Now why don't you go read the entire article and take back some of your criticism. I think it was a fairly nice article. (and sure, there were some errors, but I strongly prefer this over some typo-less corporate soap-opera)
  • Re:Don't use shell (Score:4, Insightful)

    by Haeleth ( 414428 ) on Saturday December 16, 2006 @09:30AM (#17267970) Journal
    This code is not pure shellscript : it uses awk and wget to get the job done...
    That's what a pure shellscript is! The whole point of shell scripting is that you use the shell script as glue to tie together simple single-purpose utilities that come as standard with every flavour of Unix ever.*

    To me, the *only* advantage of shellscript is that it's the only language that you are sure to find on any Unix system.
    No shit, Sherlock! You have clearly never worked in a large organisation, where - believe it or not - you, as a standard user, do not actually get to insist that the already-overworked IT department jump through bureaucratic hoops to install your favourite bloated scripting language, unless you have a damn good business case for it. And probably not even then.

    Hint: if the task you want that scripting language to accomplish is trivial to achieve with a simple shell script, you don't have a good business case.

    * This doesn't apply to wget, obviously, but if your platform really has no standard alternative, you are more likely to persuade IT to install something small and simple like wget, fetch, curl, etc. than a complete programming environment like Python.
  • Re:welll.. (Score:1, Insightful)

    by AchiIIe ( 974900 ) on Saturday December 16, 2006 @09:46AM (#17268048)
    I argue that using > grep file1 file2 file3 regex can be more confusing than > cat file1 file2 file3 | grep regex since grep is almost always used with a single argument. When you go back 1 month later and you try to figure out what's going on, the first example is more likely to confuse you. ~ $ time cat tmp/a/longfile.txt | grep and 2811 real 0m0.015s user 0m0.003s sys 0m0.013s ~ $ time grep and tmp/a/longfile.txt 2811 real 0m0.010s user 0m0.006s sys 0m0.004s ~ $ I argue that the extra 0.005 (!) seconds you saved by avoiding the cat is worthless when compared against the loss of abstraction.
  • Re:welll.. (Score:5, Insightful)

    by johnw ( 3725 ) on Saturday December 16, 2006 @09:56AM (#17268124)

    I argue that using> grep file1 file2 file3 regex can be more confusing
    Not least to the grep utility, which expects the regex first, not last.

  • Re:welll.. (Score:5, Insightful)

    by Znork ( 31774 ) on Saturday December 16, 2006 @09:57AM (#17268128)
    I'd tend to agree with the GP. Consider for example if you have excessively badly named files like '-whatever' in a particular directory; cat has very few destructive ways it can go wrong, other commands may be less forgiving, and cause much more surprise.

    Further, the assembly line abstraction of cat as 'input the contents of these files into the beginning of my pipeline' is predictable, simple and very clear and readable. Using the filenames in the commands means you have to be certain each command will take filenames, and if you replace the first step (from a grep to an awk, for example), you have to rethink your input method semantics again.

    Any typing speed gains and performance improvements you may get will probably get shot the first time some command does something unexpected, or by the extra steps of thought.

    And if performance really was a serious concern you probably shouldnt be writing it as a shell script...
  • Re:Anal Unix Guy (Score:3, Insightful)

    by jgrahn ( 181062 ) on Saturday December 16, 2006 @10:03AM (#17268158)
    The title should be 10 Good Unix Hints. Not Habits.

    Yes -- and habits is what people desperately need. The people I know primarily need three habits: RTFM when they don't understand something; adjusting their behavior based on the FM; and managing their use of the current directory (i.e. you don't have to cd into a directory to use a file which lives there).

  • Re:Don't use shell (Score:3, Insightful)

    by gmack ( 197796 ) <gmack@[ ]erfire.net ['inn' in gap]> on Saturday December 16, 2006 @11:24AM (#17268632) Homepage Journal

    2. how about accepting command line arguments in bash? in perl it's just $ARGV[0]. nice and simple and like C++ (except for the offset by one) so i don't want to have to bother learning another one.

    Command line args? $1 $2 etc or $* for all of them.

  • by xyloplax ( 607967 ) on Saturday December 16, 2006 @11:37AM (#17268706)
    Here are some more important general IT rules (Unix rules can easily be OS and version dependent and frequently come from usage in YOUR environment)
    • Copy before edit
    • Tape backup before delete/decommission
    • READ YOUR COMMAND before hitting return
    • Check where things are symlinked to
    • Echo in your scripts instead of destructive commands as a test run
    • Test your changes on a lesser-importance box
    • Use proper Change Control procedures
    • Cover your ass and capture your terminal output
    • When taking something out of service, turn it off for a few days/weeks before deleting/purging it
  • Re:Don't use shell (Score:5, Insightful)

    by JohnFluxx ( 413620 ) on Saturday December 16, 2006 @12:10PM (#17268926)
    Wow, do you think you could be just a little bit more polite next time?
  • E.g.,
    grep someFn `find . -name "*.cpp"`
    Personally, I was suprised he didn't mention the backtick. Now, that's useful. (Although they can be annoying while camping.)
  • Re:welll.. (Score:3, Insightful)

    by camperdave ( 969942 ) on Saturday December 16, 2006 @03:58PM (#17270880) Journal
    Speaking as a novice, cat|grep is easier to understand. Until I skimmed the fancy article, I was unaware that grep could take a filename like that. I thought that to do filenames you needed fgrep. To an experienced unix guru, it may not be any clearer, but to a novice there's a world of difference.
  • by lahi ( 316099 ) on Sunday December 17, 2006 @08:01AM (#17276306)
    Right. So 10 *really* good Unix "habits" would be:
    1. Never use csh or any derivative thereof.
    2. Know the portable behaviour of your Unix tools.
    3. Learn to use ed, one day you'll be glad you did. You can also use ed and ex from scripts or from a command.
    4. A shell command is a small program. If you are unsure about a command, test it first, like you would any program.
    5. Learn to use the standard shell on your system.
    6. Learn useful nonstandard extensions of utilities, but use them with care.
    7. Never rely on an extension to the point that you forget how to do it portably. The definition of "portably" is up to you.
    8. Learn to use csh enough that you can make do in an emergency, and learn *why* you shouldn't use it.
    9. If your standard shell is Bash, learn Korn too. And vice versa. Learn both, how they differ, and how they differ form your standard shell.
    10. Sometimes a real C program or a script in a different language is better than using shell.

    -Lasse

Be careful when a loop exits to the same place from side and bottom.

Working...