freebsd at edvax.de
Fri Jun 5 12:26:48 UTC 2020
On Fri, 5 Jun 2020 04:27:20 -0700, Donald Wilde wrote:
> On 6/4/20, Michael Schuster <michaelsprivate at gmail.com> wrote:
> > On Fri, Jun 5, 2020 at 8:22 AM Brenda J. Butler <bjb at sourcerer.ca> wrote:
> >> As for "what is shell programming" ... it's the same as regular
> >> programming
> >> but written in a language that doesn't need to be compiled. Often the
> >> shell program calls on other programs to do the work. So learning "shell
> >> programming" is about more than learning the shell language - it is
> >> also about learning enough of those other little utilities to be
> >> able to do something useful in the shell.
> > One of the best books on the subject (in my experience) is "The Unix
> > Programming Environment" by Kernighan and Pike - written in 1984, it is
> > still a good text to teach you some of the fundamentals of the ... unix
> > programming environment ;-).
> > I highly recommend it.
> I agree! Very good book! Another good one is 'UNIX Network
> Programming' by Stevens, and soon enough you'll be ready for 'Advanced
> Programming in the UNIX Environment', also by Stevens (Sorry, I'm a
> 'dead tree' addict!).
Dead tree: "The magic garden explained. The internals of
UNIX System 5 release 4." by Benny Goodheart & James Cox,
if you want to dive into system programming.
> While compiled binaries on 'NIX systems like FreeBSD are distributed
> in ELF format (which you can look up if you are interested), files
> intended for a scripting language such as a shell have a very special
> sequence of characters the shell looks for at the beginning: '#!',
> also called 'she-bang'. These MUST be the first two characters in the
> text file, followed by the location of the program you want to use to
> execute the file, such as '#!/usr/local/bin/ruby27' for the latest
> version of Ruby.
This is needed (and generally recommended) for files that
have the executable attribute set, so you can run the
script as if it was a real program - except that you
implicitely call the appripriate interpreter, indicated
by the #! line, that will interpret the script.
Related terminology: interpreter, compiler.
Further words: compiler, assembler, preprocessor, linker.
> You can also specify a different shell binary as your
> program. For example, I execute a file with '#!/bin/sh' with the
> simple Bourne shell 'sh' from my root prompt when root's normal shell
> (on FreeBSD) is tcsh. The syntax of each shell's language is somewhat
> different, so take care. Just dive in with your text editor, create a
> few files, and try to run them. If you haven't already, use 'chmod + x
> myfile' to make them run directly from the prompt, like:
> the period-slash combo tells your shell that the current directory is
> where to find that file; otherwise, it'll merrily look for your
> program in /bin, /usr/bin, etc.
Sidenote: The command is
chmod +x <filename>
No space after "+"; surely just a typo.
If you forgot to make the file executable, you can still
have sh process it:
The extension ".sh" for shell scripts is of course optional.
It is not entirely uncommon that scripts which have the
executable bit set and are intended to be run directly do
not have an extension, while scripts to be called with the
interpreter do keep the extension, for example:
is an awk script, it starts with "#!/usr/bin/awk -f", while
is to be used like this:
blah | foo | awk -f somethingelse.awk > results.txt
As mentioned, a #! line doesn't have to exist in such a case.
This isn't exactly a rule, but you will often find it in
reality. Of course there's nothing wrong with doing things like
if it is set executable. :-)
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
More information about the freebsd-questions