DragonOSA RedPower revival

The dsh shell

DragonOS's Linux-like shell, its scripting language, and how to make your own commands.

The Terminal app on the DragonOS desktop opens dsh, a Linux-like shell. It has files, pipes, processes and a full scripting language, plus commands that reach the computer's hardware and the network.

Getting around

help
man ls
ls -l /
cd /mnt
df -h

help lists every program and man COMMAND shows how to use one. The full list is on Shell commands.

The shell keeps a history, supports alias, export and NAME=value assignments, and runs commands in the background with & (see them with jobs, bring one back with fg). Commands chain with ;, && and ||. Ctrl+C interrupts the running command. Pipes (|) and redirection (>, >>, <, 2>, 2>&1) work as on Linux:

ls -l /mnt | grep hd
dmesg > boot.log
sort -n numbers.txt | uniq -c

Edit files

edit FILE (or nano) opens the text editor in the terminal; the Text Editor desktop app edits files in a window. less FILE (or more) pages through a file, and top shows processes live.

Scripts

dsh compiles scripts and runs them one command at a time. Scripts support:

  • if / elif / else, while and until (including done < file), for … in, for ((;;)) and case;
  • functions with local and return; break and continue, also inside && chains;
  • arithmetic: $(( )), (( )) and let;
  • command substitution with $( ) or backticks;
  • $#, $@, $RANDOM, ${#v} and $'…' quoting;
  • read with -p (prompt), -n (characters), -t (timeout) and -s (silent). Arrow keys arrive as ESC [ A to ESC [ D.

Unfinished compound commands show a > prompt, so you can type a loop over several lines.

#!/bin/sh
# blink.sh: blink the white channel of the I/O expander at address 3
count=5
if [ $# -gt 0 ]; then count=$1; fi
for ((i = 1; i <= count; i++)); do
  io 3 0 on
  sleep 0.5
  io 3 0 off
  sleep 0.5
  echo "blink $i of $count"
done

Make it a command and run it:

chmod +x blink.sh
./blink.sh 3

Make your own commands

  • chmod +x makes a file executable; ls -l shows the mode bits.
  • The PATH is /bin:/usr/local/bin:~/bin. Put executables in ~/bin to run them by name.
  • The first line chooses the interpreter: #!/bin/basic runs the file as a DragonBASIC program; any other #! line runs that command with the file's path.
  • Floppies use a FAT file system: their files always show as 0755, and chmod is refused there.

Draw on the screen

The terminal understands VT100 escapes: cursor position and movement, clearing the screen and line, reverse video, and hiding and showing the cursor. tput names them (tput clear, tput cup ROW COL, tput civis, tput cnorm), and printf and echo -e print them. Together with read -n 1 -t this is enough for games:

#!/bin/sh
tput clear
tput civis
x=40
while true; do
  tput cup 20 0
  tput el
  tput cup 20 $x
  printf '@'
  read -s -n 1 -t 0.2 key
  case "$key" in
    a) ((x > 0)) && ((x--)) ;;
    d) ((x < 79)) && ((x++)) ;;
    q) break ;;
  esac
done
tput cnorm

Running scripts are saved with the world and continue after a reload.