Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 5 Jun 2020 14:26:44 +0200
From:      Polytropon <freebsd@edvax.de>
To:        dwilde1@gmail.com
Cc:        freebsd-questions <freebsd-questions@freebsd.org>
Subject:   Re: Mininal skills
Message-ID:  <20200605142644.add4cf07.freebsd@edvax.de>
In-Reply-To: <CAEC73907pTMW1LE2=WQCX%2B-sa0ivPf7141xFWOyurdHdJ5g0Cg@mail.gmail.com>
References:  <CY4PR19MB0104A2C03F4D66A1DA251A23F9880@CY4PR19MB0104.namprd19.prod.outlook.com> <CY4PR19MB0104E74C96FDA086AF18C0F8F9890@CY4PR19MB0104.namprd19.prod.outlook.com> <CY4PR19MB01040BA77657FD7AB1D0B1E9F9890@CY4PR19MB0104.namprd19.prod.outlook.com> <20200605062217.cjtkdgwqwqqienct@blueeyes.stuffed.animals> <CADqw_gJpjAOAL_VAsppqCQBJZixY=PCFFmkE7is6xJGX_U4BZQ@mail.gmail.com> <CAEC73907pTMW1LE2=WQCX%2B-sa0ivPf7141xFWOyurdHdJ5g0Cg@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 5 Jun 2020 04:27:20 -0700, Donald Wilde wrote:
> On 6/4/20, Michael Schuster <michaelsprivate@gmail.com> wrote:
> > On Fri, Jun 5, 2020 at 8:22 AM Brenda J. Butler <bjb@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.
> [snip]
> 
> 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:
> 
>      ./myscript
> 
> 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:

	sh myscript

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:

	./something

is an awk script, it starts with "#!/usr/bin/awk -f", while

	somethingelse.awk

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

	./thescript.sh

if it is set executable. :-)




-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20200605142644.add4cf07.freebsd>