Welcome, Guest. Please login or register.
Amiga Kit Amiga Store Hollywood MAL AMIStore App Store A600 Memory

AuthorTopic: THOR's Shell Hacks  (Read 6589 times)

0 Members and 1 Guest are viewing this topic.

Offline Thomas Richter

THOR's Shell Hacks
« on: February 01, 2015, 08:27:47 PM »
Hi folks,  AmigaOs has two main user interfaces: The workbench and the shell. The workbench uses the mouse, icons, windows, menus... all the neat stuff. So let's talk about the other one: The shell. This is more traditional, uses the keyboard, a single window, and is less intuitive to use.  Apparently - and as it is usually the case - manuals a put aside rarely ever read. Unfortunately, this means that you may miss a lot of the new features of Os 3.9 that were introduced.  This thread, in loose sequence, will present "lesser known features" of the Amiga shell. Unless said otherwise, this thread uses the AmigaOs shell of the latest BoingBag, from Os 3.9, and the console from AmigaOs 3.9, ViNCEd. An older version of ViNCEd can be found in aminet, though in general, you'll need the latest.  All tutorials start with opening a shell window. It's usually top level on the workbench disk, with an icon named "Shell". SYS:Systems also contains an icon, usually named "CLI". That's the same shell, but a less useful user interface that lacks some of the features. We need the former one.  Where exactly these icons are is of course a matter of taste.
« Last Edit: February 02, 2015, 01:39:35 AM by Thomas Richter »

Offline Thomas Richter

Re: THORs Shell Hacks
« Reply #1 on: February 01, 2015, 08:33:52 PM »
We'll start today's thread with shell scripts. The shell takes commands, in the form of "command arguments". If you type in "list", in the shell window, that executes the list command.

If you type in "copy s:startup-sequence to ram:" this copies the startup-sequence to the ram-disk. "S:Startup-sequence" is a shell script, a series of commands, one executed by another. This one in particular is important as it is executed by the system for booting up.

One particular "nice" feature makes up today's "Shell Hack": Interactive debugging, and tracing of shell scripts. Tracing means that the shell shows you which command it is about to execute, and you may run the command, skip over it or stop tracing.

To enable tracing, use the following command in the shell:

set interactive on

If this is put top in a shell script, for example into the startup-sequence (with an editor of your choice), the shell will prompt you for each command it is going to be executed. If you press RETURN, the shell will run the command. If you press N or DEL, the shell will skip over it. If you press ESC, the shell will abort tracing and execute the rest of the script without bothering you further. If you press Control+D, the script will be aborted.

Offline DutchinUSA

Re: THORs Shell Hacks
« Reply #2 on: February 01, 2015, 08:36:34 PM »
Thanks, I didn't know the interactive thing, that's cool and helpful ! Keep 'em coming :)

Offline trekiej

Re: THORs Shell Hacks
« Reply #3 on: February 01, 2015, 08:47:12 PM »
Most excellent! (guitar sounds)
Amiga 2000 Forever :)
Welcome to the Planar System.

Offline kolla

Re: THORs Shell Hacks
« Reply #4 on: February 02, 2015, 12:50:17 AM »
Could you point out when you describe something that specifically requires shell newer than that of OS3.1? Personally I use your shell even on "3.1ish" setups.

Offline Thomas Richter

Re: THORs Shell Hacks
« Reply #5 on: February 02, 2015, 01:28:59 AM »
Quote from: kolla;782812
Could you point out when you describe something that specifically requires shell newer than that of OS3.1?

All of this thread will cover specifically 3.9 features, unless noted otherwise. Thus, "interactive" is new in 3.9 (well, not exactly "new" by any meaning of the word "new" I know, but at least "latest").

Offline kolla

Re: THORs Shell Hacks
« Reply #6 on: February 02, 2015, 03:48:59 AM »
Ok, great!

Offline Oldsmobile_Mike

Re: THORs Shell Hacks
« Reply #7 on: February 02, 2015, 04:58:31 AM »
Great thread idea!  Makes me wish we had a way to make it a "sticky", so would always appear at the top.  Looking forward to reading more of your tips!

Amiga 500: 2MB Chip|16MB Fast|30MHz 68030+68882|3.9|Indivision ECS|GVP A500HD+|Mechware card reader + 8GB CF|Cocolino|SCSI DVD-RAM
Amiga 2000: 2MB Chip|136MB Fast|50MHz 68060|3.9|Indivision ECS + GVP Spectrum|Mechware card reader + 8GB CF|AD516|X-Surf 100|RapidRoad|Cocolino|SCSI CD-RW
 Amiga videos and other misc. stuff at https://www.youtube.com/CompTechMike/videos

Offline broadblues

Re: THORs Shell Hacks
« Reply #8 on: February 02, 2015, 12:10:07 PM »

Set interactive on

works in the AmigaOS 4.x shell too. I knew about 'set echo on' but not the interactive switch, thanks!

Offline Thomas Richter

Re: THORs Shell Hacks
« Reply #9 on: February 02, 2015, 09:29:20 PM »
Quote from: broadblues;782833
Set interactive on

works in the AmigaOS 4.x shell too. I knew about 'set echo on' but not the interactive switch, thanks!

Naturally, that's because the Os 4.x shell is based on the Os 3.9 shell.

Offline Thomas Richter

THOR's Shell Hacks (2)
« Reply #10 on: February 02, 2015, 09:47:18 PM »
Today's edition is on redirection. It's less useful on AmigaOs than it is on Unix - it allows to direct the output of a command to another file (instead of the console), or to take the input of a command from a file rather than the console.

Everybody knows the classics: >filename redirects the output, filename, which redirects both input and output to the same file, and >>filename, which redirects the output, but instead of overwriting the target file, appends to it.

AmigaOs 3.9 adds more. To understand them, be reminded that there is a third stream, the "error output", which goes usually to the console. Also be reminded that the console spells "*" in AmigaOs (or, actually, Tripos, which is where the dos came from). It is not a "wild card" unlike many other popular osses. Thus, to "redirect" the output to the console, one would write ">*", and to take input from the console, you write "<*". Of course, you usually don't do that simply because that's what the input and output is connected to anyhow.

Now, to redirect the error output, AmigaOs adds *>filename, read as "console goes to file". That's most useful if the target "file" is actually some type of interactive terminal, for example AUX: (the serial console), thus "*>AUX:" makes the console the serial console, and by that also the standard error.

What ">>" is for ">" is "*>>" to "*>": The redirection *>>filename redirects the error output to a file, but instead of overwriting what is there, it appends to the end.

Then, we have "*<>", which redirects the standard error into the same steam the standard output is going to. It does not take an argument. That is, error output goes into the same file the standard output goes to. Thus "command >ram:fool *<>" redirects the error output to the RAM-disk, too.

Last but not least, there is "<<". This redirects standard input, but it's a bit special. It does not take input from a file, but rather from a script a command is placed in. With "<<", part of the script becomes the input, namely everything below the current command line and an "end marker", a special character sequence that is also placed as argument behind "<<".

To give an example, consider the following shell script:

echo "script starts here"
command <foobar
echo "script ends here"

redirects the input of "command" to the same script, and everything up to "END" is the input to the command, and the line "foobar" is the literal input to the file. That's most useful if you have to provde complex inputs to a command, but you don't want to create a temporary file in RAM: Instead, the shell does that for you.

If you know the Linux "bash" shell, that's pretty much how it works there, too. On linux "*>" would be spelled "2>", "*>>" would  be "2>>" and "*<>" would be "2>&1", but since the Amiga shell doesn't have "file descriptors" as Linux does, and only has "two and a half" standard descriptors (CIS,COS,CERR) instead of the many Linux has, "2" was a particuarly bad (and unintuitive) choice for the error output. "*>" was picked because it is just the reverse of ">*". The former redirects the console to a file, the latter redirects output to a console, so it's easy to remember.

Offline Thomas Richter

Re: THOR's Shell Hacks - it's full of stars!
« Reply #11 on: February 03, 2015, 09:16:22 PM »
Do you know all the meaning of * in an Amiga Shell? Actually, there are plenty, and it's not always easy to keep them apart.

So, first, if you put the star * *outside of quotes*, it means the current console. So, for example, if I place a

copy foobar to *

it means: Copy the contents of the file onto the screen. Strange, since the star usually means a "wild card" for many other Os'es.

Things change if you put the star in double quotes, however. There, the star is the "escape character" of BCPL, quite like the backslash in C. Since AmigaOs was traditionally Tripos, written in BCPL, it is still this way today.

That is:

echo "*e[H"

puts the cursor in the home position, i.e. top left. This is because "*e" is the escape sequence for the ASCII character "escape" (hex 0x1c, decimal 27), Escape-[ is the "CSI", the "control sequence introducer, and "CSI-H" is the console command, understood by the console window, to put the character into the top-left position.

Thus, the star means something different *in* quotes compared to *outside quotes*. If you use

echo *e[H

instead, then that's just putting the literal string on the console.

Now, in Os 2.x, CBM rewrote the dos.library. Or rather, they simply took "arp", the "Amiga Replacement Project", made a couple of modifications, and used that as a new basis. In arp, a flag exists to change the interpretation of wild-cards, i.e. patterns that match several files to run a job more efficiently. For example,

copy #? to *

copies all files in the current directory to the console, #? matching all files. The expansion of the wild card #? is done by the copy command, not by the shell (unlike in other operating systems, Linux for example, where this expansion is under shell control).

Now we come to the third meaning of the star. Unfortunately, arp included an option in its pattern matching to use * as a replacement for #? "by popular demand". Thus, if a command uses the arp = now dos.library pattern matcher, * *may* (by choice of the user) get another meaning, namely "match every file".

This now becomes really confusing, as you need to remember when a command uses the pattern  matcher (then *=#?) and when not (then *=current console).

So, for example, with the arp-"*" flag set (several patches provide this option),

copy * to *

means "copy all files to the console", where the first "*" is seen by the pattern matcher, so it means #? = all, and the second star is not, so it means console.

Unfortunately, you have to know this, and since you usually cannot know (how, anyhow?) it's usually *not* a good idea to enable this option. There are already two meanings of the star, adding a third one is not a particularly good choice, even more so as it depends on implementation decisions of the shell command and is not under direct control of the user.

Avoid this stuff, and learn that "Amiga is a bit special" and uses #? as wildcard, and not *. * is the BCPL escape sequence within quotes, or the console as file name outside quotes.

Offline Steady

Re: THOR's Shell Hacks - it's full of stars!
« Reply #12 on: February 03, 2015, 11:38:28 PM »
Thanks Thomas,

I always wondered why * as a wildcard didn't really work when enabled. That copy example clarifies it.

Personally, I always disabled the option. #? works just fine for me :-)

Offline ChaosLord

Re: THORs Shell Hacks
« Reply #13 on: February 04, 2015, 11:34:33 AM »
Is there any way when writing scripts and using the shell to use a logical replacment for *, such as CONSOLE:  ?

That would mean I could avoid using * altogether and never get confused by anything ever again.
Wanna try a wonderfull strategy game with lots of handdrawn anims,
Magic Spells and Monsters, Incredible playability and lastability,
English speech, etc. Total Chaos AGA

Offline danwood

Re: THORs Shell Hacks
« Reply #14 on: February 04, 2015, 12:45:16 PM »
Quote from: Thomas Richter;782778

set interactive on

Great tip Thomas, and there was me booting with no startup-sequence and running snoopdos before manually executing SS all these years.  

Really useful.