Use bash's for command to make for loops, just as in programming languages:
BASIC:
for i = 1 to 10
print i
next i
Outputs:
1
2
...
For instance, when making a debian startup script in /etc/init.d, then
wanting to set it up properly with links in /etc/rc2.d, rc3.d... it's useful
to do something like:
vim /etc/init.d/newscript
ln -s /etc/init.d/newscript /etc/rc2.d/S50newscript
for i in /etc/rc[3-5].d ; do cp -d /etc/init.d/newscript $i ; done
for i in /etc/rc[016].d ; do cp -d /etc/init.d/newscript $i/K50newscript ;
done
Basic structure:
for <variable> in <list> ;
do <list> ;
done
Variable right after for is of the form: i
Variable used in <list> after do is of the form: $i (see above example)
Combine this with other command line tricks such as using these things: ``
Note, they are not: ''
This is one of the coolest things about unix. '' (next to the return key)
means quote everything in between ' and ' exactly. This allows for spaces
and special characters.
`` (tilde ~ key) means execute whatever is in there as a command, and return
the output.
This allows embedding of commands. Integrated with forloops:
for i in `ls /home/cory/*.c` ; do cat $i ; done
Very simple, and does the same thing as: cat /home/cory/*.c. But the
principle is there to be expanded upon.
Rob had a search and replace function....
perl -pi -e 's/#ABCDEF/#123456/g' page.html
... to replace all the colors in an html file, or search and replace in any
file.
What if you have 100 similar html files, that you want to replace color
#ABCDEF with color #123456 in all of them!:
for i in /home/cory/public_html/sneakers/*.html ; do perl -pi -e
's/#ABCDEF/#123456/g' $i ; done
For those that don't know, here are some bash special chars or key commands.
For those of you who know some that I don't list here, please let us know!
keystrokes
-----------
ctrl+w delete last word on the command line
up/down browse through command history on command line
ctrl+a go to beginning of command line
ctrl+e go to end
ctrl+r search through history, type in letters and it will find a command
matching
what you type, if one exists
ctrl+z stop current task, and give me a command line
commands
-----------
fg make the most recent backgrounded task come to the forground.
resume if stopped.
bg make the last stopped task resume, but in the background.
Characters
-----------
'' and "" do varing levels of quotation
\x where x is any character means "BASH, don't interpret this
character!"
(ie. echo blah > blah.txt --- this will create a file called
blah.txt
containing blah.
echo blah \> blah.txt --- this will print to the screen: blah >
blah.txt )
; execute two commands on the same line: cd / ; ls ; cd /home ; ls ; cd
cory ; ls
| redirect output of one command to input of another: cat /blah.txt |
lpr -Pprinter (instead of printing blah.txt to the screen, it sends it to
lpr, which prints it
on a printer)
> redirect output to a NEW file (this will overwrite an existing file)
>> redirect output to the end of a file (this will append an existing
file, or
make a new one, if one doesn't exist) echo blah >> blah.txt
& background this task, and give me a command line:
/home/cory # tar xfz blah.tgz &
~ home directory: 'cd' or 'cd ~' or 'cd /home/cory' all do the same
thing
also useful when specifying absolute paths: ~/.bash_profile
ctrl+alt+del ;) Get root access on any system!
globs (wildcards)
------------------
* anything is a match
? any single character is a match
[] any single character found in these brackets are matches:
[abcdefg] looks only for single chars in this section
[a-z] all lowercase letters, not including -
[\-] a single character matching -
[\-\ \\\?\*] a single char matching: -?\* or space
{} Inclusive logical OR for multiple wildcards:
{bob,sam,charlie} returns all matching any
! NOT whatever I just said: [!abc] any single character except a,b or
c
Mixes:
ls /home/cory/* --all files
ls /dev/hd[a-c]* --first 3 ide drives/controllers:
hda, hda1, hda2.., hdb, hdb1, hdb2... hdc,
hdc1, hdc2...
ls /dev/hd[a-c]1 --first partition on the first 3 ide controllers:
hda1, hdb1, hdc1
grep {http,ftp,telnet} /etc/services
-- return all lines matching http,ftp or telnet from
the services
file, showing their respective ports and
descriptions.
Things msdos wishes it could do:
ls */file.c -- look for file.c in all single level subdirectories
ls */*/file.c -- look for file.c in all two-deep subdirs
rm */*/file.c -- delete all file.c's in two-deep subdirs, but not in any
other
(ie doesn't touch */file.c)
Note file.c can be a glob:
rm */*/sa10[4-9][0]??
sa1040xx is untouched
sam/sa1040xx is untouched
sam/work/sa1040xx is deleted along with...
sam/work/sa1050xy
charlie/html/sa1090xx
charlie/work/sa1060ab
sam/sa2040xx is untouched along with...
sam/sa1041xx
sam/sa104
sam/sa1040yz.lmnop.backup
Cory
-----Original Message-----
From: Rob Hudson [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 18, 2001 10:16 AM
To: EUGLUG
Subject: [EUG-LUG:362] Any have cool command line tricks?
Since I (and probably others) learn best from examples, I thought it
would be cool if we all could share command line tricks that we've
picked up and use often. Tricks with perl, awk, sed, xargs and all
the other unix tools with redirects and pipes - the works.
I've often written things in perl that someone shows me later can be
done in a single command line using a combination of awk, sed, and
xargs. Since I don't know much about these tools, and probably a lot
of the other tools available, maybe we all can share our tricks.
One that I use often is a perl one-liner to search and replace on a
file (or list of files).
To get rid of those pesky ^M DOS carriage returns, one can type this:
perl -pi -e 's/^M//g' file.txt
I've changed my colors in a webpage by using this:
perl -pi -e 's/#ABCDEF/#123456/g' page.html
-Rob