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."
Square or Curly brackets? (Score:4, Informative)
Typo (Score:3, Informative)
This article... (Score:5, Informative)
Comment removed (Score:5, Informative)
Re:Why? (Score:2, Informative)
*nix is a highly modular component-based software system with a standard interface (flat byte streams) between components, and a basic set of standard components (given in the POSIX standard) that can be relied upon to always be present.
absolute drivel (Score:4, Informative)
Re:Don't use shell (Score:4, Informative)
This code is not pure shellscript : it uses awk and wget to get the job done...
A Python equivalent might be :
It's not that much longer, it's much easier to read and less error-prone (especially the awk part), and it uses fewer external utilities.
To me, the *only* advantage of shellscript is that it's the only language that you are sure to find on any Unix system.
Re:Square or Curly brackets? (Score:5, Informative)
Eh? There's more to Unix than shell scripting (Score:3, Informative)
"10 good habits that improve your UNIX command line efficiency" would probably have been a better title.
The title did however bring back fond memories of Eric Raymond's The Art of Unix Programming [catb.org]. The book is available online, and if you were hoping for something a bit more substantial as well, then the section Basics of the Unix Philosophy [catb.org] might be worth a read.
Re:Don't use shell (Score:5, Informative)
wget -i filename
Or have I missed something?
Monkeyboi
tar comment (Score:3, Informative)
tar xvf
without the dash. (E.g., tar -xvf)
While that does work, I prefer to add
the dash as it makes it more consistent
with the other commands. So I consider
that a bad example. tar is one of the
older commands like dd that have weird
command line syntax.
Actually useful hints (Score:5, Informative)
In scripts, prefix dangersous commands with an 'echo' for a test run (So you can catch all those rm -rf
Single quotes are the best quotes for plain strings. The only reasion to use double quotes is if you need to quote a variable or a single quote.
Completion is fun, but using wildcards is more flexible (though you'll only want to use benign commans like cd, less, etc):
nano
nano
Note that the use of subpaths reduces the amount of flexibility.
cd
cd
nano
Finally, as a comment for the article, using:
test -e $DIR || mkdir -p $DIR
is much better than their suggestion and probaly faster anyway. Though I'd just do "mkdir -p $DIR" and maybe "&>/dev/null" under most circumstances anyway.
That's all I can think of at this point. Anyone else have tips?
Re:Very helpful (Score:3, Informative)
When timing commands, it's best to repeat the command several times and see if the times change significantly.
Re:lowercase uppercase (Score:2, Informative)
Re:welll.. (Score:5, Informative)
That list was fairly arbitrary, but the piping cat thing is something that basically only annoys the most anal of anal, and they probably do it sometimes too.
Its common for me to do cat foo and then hit the up arrow and append a pipe to another command instead of editing the whole command line. Computers are pretty fast, and real anal people would use fgrep instead of grep, but again I always use egrep, because I never know when a regular expression will be edited into a more complex one, and to me all of the speeds are the same.
My #1 habit to tell people, although it is not a habit, but just where to start it to learn your shell. No science guys, csh is not a worthy shell in 2006. If you have to suffer with the wacky behavior of a csh variant, at least use tcsh.
My #2 thing to learn is a text editor.
As far as habits go. First and foremost, unalias cp, mv, rm to have the -i flag. In my opinion, that is a BAD habit to start. You WILL lose files sooner or later, and the more painful the better so that you will think so you will stop doing it. the -i flag will NOT stop you from redirecting into a file, and the most dangerous is the -rf flag with rm will override that -i. Remote copies via rcp or scp will not honor the -i flag. Unarchiving an archive will not honor the -i flag. There are tons of ways to lose files, and you will lose them. Its a much better habit to universally save yourself from yourself to not lose them by testing with -i, working off of a copy, and thinking before you hit return, creating new directories to eliminate clobbering a file, NEVER, EVER, do tar cf foo.tar
Actually, this top 10 list is pretty lame, and should be ignored.
Re:welll.. (Score:4, Informative)
First you are telling cat to output the entire file, and then you are telling grep to go through the entire output of cat. If you're working with gigabytes of data here, that can quickly be a frustrating exercise! Folks who are in the mentality of using cut | grep and even a visual editor like vi instead of sed are up the creek when they find themselves needing to manipulate and get portions of very large data sets.
Re:lowercase uppercase (Score:3, Informative)
Why is the perl script so hard? The command line would have a bunch of sed going on, whereas the perl script only requires running perl.
I'm guessing... perl -e 'for(`ls`) { chomp; $n = lc $_; system("mv $_ $n"); }'
Re:welll.. (Score:2, Informative)
a.txt: foo
b.txt: foo
c.txt: foo
whereas cat ?.txt|grep foo produces
foo
foo
foo
I've also seen Unixes where their shells are linked against spectacularly broken libc's. Under Tru64's Bourne and Korn shells, for example, a multithreaded program foo fork bombs when run as foo < z.txt, but works fine as cat z.txt|foo (foo < z.txt under Bash works, though, because Bash is linked against the GNU libc).
Re:This article... (Score:4, Informative)
If you are encountering data created by untrusted users, don't forget the strange consequences of filenames that contain newlines.
Failing to use -0 is dangerous malpractice.
Re:lowercase uppercase (Score:4, Informative)
Break it down into its constituent parts: Iterate, rename. Whoo! Simple now, eh?
# cd cameraDir
# find . -type f -prune | while read file
> do
> mv "$file" "`echo \"$filename\" | tr '[A-Z]' '[a-z]'`"
> done
#
Re:lowercase uppercase (Score:2, Informative)
function pmv ()
{
local src dest oifs
oifs=$IFS
export IFS=$'\n'
src="${1:? 'Error: input pattern not specified'}"
dest="${2:? 'Error: destination pattern not specified'}"
for i in $src ; do
mv "$i" $(sed -e "s/$dest/" <<< "$i")
done
export IFS=$oifs
}
Now I can move files by regexps by typing pmv <file-match-pattern> <sed-matcher>/<replacement>
very very convenient.
Re:I don't think I ever use xargs (Score:1, Informative)
bar `foo` can fail if `foo` happens to make the argument list too large.
Also use find -print0 | xargs -0 instead of find -exec if you prefer not forking a jillion times.
Re:FP? (Score:3, Informative)
-Lasse
Re:lowercase uppercase (Score:3, Informative)
This isn't the first time I've seen this, but it will result in a file MYJPG.JPG being called MYjpg.JPG, ${i//JPG/jpg} would be better as at least the it would end up with the
Again, there's lots of ways to do it. To use the trivial JPG -> jpg, example, yes, you're correct in that using the shortest match at the end would be a better approach (excluding other issues). I just wanted to illustrate the redundant (and typically overused) use of basename with a simple example, and remind the folks that using parameter expansion is preferrable both in interactive form, and in scripts.
Me, I've always relied on Larry Wall's script [fsu.edu] exclusively to rename files interactively. Scripts, on the hand, are often best written with
Re:FP? (Score:2, Informative)
Proprietary? From the man page:
bad ibm no cookie (Score:4, Informative)
Let's say I'm a sysadmin and I'm running as root, trying to remove all the files in the
find
User 1001 has a directory in
Can you guess what happens?
find prints:
xargs sees: ["/tmp/tmp43cc91","/tmp/haxor","","/tmp/haxor","/
Oops!! You just hosed your system!
The correct way to use xargs is to use the -0 switch, which will separate the input by null characters, which cannot appear in filenames. find has a handy -print0 option which will output the correct output:
find
And your system is safe.