Stream files and shell scripts

Write a script, then edit a stream file.

Qshell runs shell scripts over the IFS. EDTF is the full-screen editor for stream files and source members.

Start an interactive Qshell

Type STRQSH or QSH with no command at the CL command line. The QSH Command Entry display accepts one line at a time. An unfinished compound command such as a for loop shows a > continuation prompt until you close it with done.

fruit=apple; echo "value=$fruit"
for word in one two; do
echo "item-$word"; done
read -p "Name? " name; echo "hello $name"

F9 retrieves an earlier command, F7 and F8 page through the output, F11 wraps long lines, F13 clears the display, F6 prints it and F21 opens CL command entry. F12 disconnects and leaves the shell running; enter STRQSH again to resume it. F3 ends the shell and sends HUP to background work. On a long-running command, press System Request, choose option 2 and press Enter to interrupt it.

Write and run a script

Create the practice directory, then edit a script with EDTF. Type the script lines, press F2 to save, and F3 to leave the editor.

MKDIR DIR('/home/guide')
QSH CMD('printf ''nut\nbolt\nnut\n'' > /home/guide/parts.txt')
EDTF STMF('/home/guide/report.sh')
#!/usr/bin/qsh
for file in /home/guide/*.txt; do
  echo "== $file"
  wc -l "$file"
done

Run it from Qshell. A script run by path needs *X authority, which chmod grants; qsh reads it without that authority.

STRQSH
cd /home/guide
qsh report.sh
chmod +x report.sh
./report.sh
exit

The script prefix selects the interpreter when you run the file by path. Learn400 also runs a file without one under its default shell, but keeping the line makes the script behave the same elsewhere. Commands are found through PATH; a script can use . script to read another file into the current shell.

Pipes and useful utilities

printf '%s\n' pear apple pear | sort | uniq -c
printf 'a 1\nb 2\n' | awk '{ s += $2 } END { print s }'
cd /home/guide
sed -i.bak 's/bolt/BOLT/' parts.txt
cmp -l parts.txt.bak parts.txt
tar cvf guide.tar parts.txt report.sh
tar tf guide.tar

Qshell supports quoting, variables, pipes, redirections, conditions, loops, functions, aliases, traps and background jobs. Background work is concurrent, with $!, jobs, wait and kill %1. nohup keeps a command running after you leave the terminal and appends its output to nohup.out. A writer whose reader closes receives IBM i signal 13 (PIPE), so yes qshell | head -3 ends.

Use IBM i commands and SQL from the shell

system "DSPLIBL"
liblist
sysval QCCSID
getjobid
ps
db2 "create table qgpl.demo (id int, name varchar(20))"
db2 "insert into qgpl.demo values (1, 'Ada')"
db2 "select * from qgpl.demo"

system runs a CL command in the emulator job and sends its messages to stderr and its printed output to stdout. liblist changes the job library list through ADDLIBLE, RMVLIBLE and CHGCURLIB; the original list is restored when the session ends. db2 uses SQL naming with your profile as the default schema. Rfile reads and writes source members and database members; the IFS guide uses it on a source member.

Edit a stream file with EDTF

QSH CMD('printf ''first\nsecond\n'' > /tmp/edtf-demo.txt')
EDTF STMF('/tmp/edtf-demo.txt')

Put a line command in the prefix area at the left of a record and press Enter:

The Control field runs FIND, CHANGE with NEXT, PREV or ALL, TOP, BOTTOM, SAVE, FILE, CANCEL and SET. For example, enter CHANGE 'second' 'SECOND' ALL, then TOP, to change every occurrence and start the find again from the top.

CHANGE 'second' 'SECOND' ALL
TOP

F2 saves and keeps the editor open. F3 saves and exits; when changes are pending it asks for a second F3. F5 reloads the file and F12 asks for confirmation before discarding changes. F11 switches between text and a hexadecimal view. F15 sets the CCSID, LF/CRLF/CR line endings and tab width. A file tagged CCSID 65535 is edited as byte pairs.

Edit a source member

CRTSRCPF FILE(QGPL/EDTSRC) MBR(DEMO) RCDLEN(92)
EDTF FILE(QGPL/EDTSRC) MBR(DEMO)
DSPF FILE(QGPL/EDTSRC) MBR(DEMO)

EDTF and DSPF accept a stream file path, a QSYS.LIB file and member, or a directory with a wildcard selection. DSPF is read-only and shares the same navigation; F11 shows its hex view. The IFS guide covers copying text between a stream and a source member, and lesson 18 and lesson 24 walk through a script and a stream file step by step.

What is not supported yet

See also the EDTF, DSPF and STRQSH command pages.