maux
Star on GitHub Buy me a coffee

L1. hello, world, in the shell

Learn / L1

Goal: write a two-line program with vi and run it by name.

What you need

A booted maux. L0 if cd and ls are new.

The file

Two lines. The first tells the system which program should read the rest.

examples/hellosh/hello — on the disk as /usr/src/examples/HELLOSH/hello
#!/bin/sh
echo hello, world

The session

The [ ... ] line is what to type inside vi: a key at a time, as the gate typed it. i starts inserting text, ESC stops, and :wq followed by RETURN writes the file and leaves.

$ echo hello, world
hello, world
$ echo

$ mkdir /tmp/l1
$ cd /tmp/l1
$ vi hello
[ in vi: press i, type the two lines shown above, press ESC, then type :wq and press RETURN ]
$ cat hello
#!/bin/sh
echo hello, world
$ ls -l
TXT       28 -                hello
$ ./hello
hello, world
$ cmp hello /usr/src/examples/HELLOSH/hello
$ chtyp -t TXT hello

What just happened

maux has no execute permission bit. When you name a file as a command, the system looks at what it is: a BIN or SYS file is a program and is loaded; a TXT file whose first two bytes are #! is a script, and is run by the program named on that line, here /bin/sh, with the script as its argument. Any other text file is data, and naming it as a command is refused with sh: cannot execute. The ./ in ./hello says "the file called hello in this directory". A word with a slash in it is a path, and the shell never searches for it elsewhere.

You did not need to give the file a type. vi writes ProDOS type TXT ($04) by itself; ls -l shows it in the first column. chtyp is for a file that arrived some other way, and its spelling is chtyp -t TXT hello: the -t is not optional. The last command in the session does exactly that to a file that is already text, which changes nothing and shows that the command line is a valid one.

cmp printed nothing: your file is byte for byte the file on the disk at /usr/src/examples/HELLOSH/hello. Silence is the Unix way to say "no difference".

Try next

Add a third line that runs ls. Then cd / and run the script again by its full path, /tmp/l1/hello: which directory does the ls list?

← Find your way around | All lessons | Hello, world in assembly →