gusl: (Default)
gusl ([personal profile] gusl) wrote2006-10-13 07:23 pm

bash bleg

If you have a 'file' variable containing a whole path the a file, how do I get just its filename?

How does one iterate through space-separated strings in a big string? (I suspect this uses 'split' to create an array of strings)

What is a good bash scripting reference? http://justfuckinggoogleit.com/ is not the answer I'm looking for.

[identity profile] smandal.livejournal.com 2006-10-14 12:20 am (UTC)(link)
There's a way to do it with bash, but 'basename' is the command you're looking for.

[identity profile] bhudson.livejournal.com 2006-10-14 12:42 am (UTC)(link)
basename, dirname, and the perl package of the same name are your friends. I often also write a 'noext' perl script that I can email you if you want it (strips the last extension).

For (ba)sh, the 'for' command does what you want:
for x in $foo ; do mv $x `basename $x`/mainfile ; done
or whatever.

All my scripting I learned from man pages and looking at other sh scripts.

[identity profile] darius.livejournal.com 2006-10-14 02:52 am (UTC)(link)
O'Reilly has a book on bash. I haven't read it.

[identity profile] simrob.livejournal.com 2006-10-14 07:29 am (UTC)(link)
(registers suprise that http://justfuckinggoogleit.com/ is actually a website)

May your work become play

[identity profile] http://users.livejournal.com/_greg/ 2006-10-15 07:00 am (UTC)(link)
$ echo $f
/home/greg/2print/fuego2006-receipt.pdf
$ echo ${f##*/}
fuego2006-receipt.pdf

$ man bash
/## <--- type this literally followed by enter

Man pages are optimized for quick reference and refreshing of what you already know.
Info pages are (supposedly) optimized for both learning and reference.
Info pages are more casually browsable:

$ info bash
(or from emacs, [Control-H I], select bash documentation)
concept-index -> parameter expansion

or read Advanced Bash-Scripting Guide

and so on. Bash is fairly close to the other major shells. The solution I gave you will
work identically with either sh or ksh and, as the doc'n shows, is part of a more general
system.

Have fun,

_Greg