Scripts and the System
Single-page view · as markdown
Printing Output and Reading Command Status
Peios / Using Peios / Scripts and the System
A command reports results in two ways: the data it writes to its output, and an exit status, a number where zero means success. Scripts use output for data and status for decisions. These commands are the building blocks for both. The man page for each command lists every option.
Your shell has its own versions of some of these commands, such as echo,
printf, test, and kill. To run the utility itself, put peiosutils
before its name, as some examples here do.
The examples use sample file names such as report.txt. Create those files
first, or substitute your own.
Print messages and values #
echo prints its arguments, which suits a short fixed message:
For any value that comes from elsewhere, use printf with a fixed format, so
that the value can't be read as an option or as formatting:
Keep the format under your control and pass data as the later arguments. The format repeats if more arguments remain.
seq 2 2 8 prints numbers from 2 to 8 in steps of 2, and yes sample prints
a line repeatedly until it is stopped. Don't pipe yes into a program to
answer prompts you haven't read: use the program's own option for running
without prompts. numfmt --to=iec-i shortens a byte count with a binary
prefix, which scales by powers of 1024. The result is rounded, so keep the
exact number for any calculation.
Run a command with a changed environment #
env runs one command with an extra or changed environment variable, without
changing your shell:
printenv prints one variable when you name it, and fails if the variable
doesn't exist, which tells an empty value apart from a missing one. Without a
command, env prints the whole environment, which can contain credentials, so
don't share that output. A cleared environment helps make a command
repeatable, but it isn't a security boundary.
Test conditions #
test reports a condition through its exit status and prints nothing:
if ; then
fi
Quote variables so that an empty value stays one argument. A file test
describes one moment: the file can change before you use it. true and
false exist only for their status. Don't add || true to hide a failure
unless you've handled the failure you expect.
expr 7 + 5 evaluates a small integer expression; quote operators such as *
that the shell would interpret. factor 84 prints the prime factors of a
number.
Read exit statuses #
A false condition isn't always an error. expr, for example, uses one status
for a zero or empty result, another for a true result, and a third for an
invalid expression. In a pipeline, each command has its own status, and the
shell decides which one the pipeline reports. Read a command's man page before
treating every nonzero status the same way.
Save a stream while showing it #
tee writes its input both to standard output and to a file:
|
tee replaces the file's contents unless you use append mode. A new file
inherits security from its directory, and an existing file keeps its own, so
choose the directory with care when the stream holds private data.
Commands on this page #
This page covers these commands:
| Command | Use it to | Manual |
|---|---|---|
echo | Print a short message | echo(1) |
printf | Print values with a fixed format | printf(1) |
seq | Print a sequence of numbers | seq(1) |
yes | Print a line repeatedly | yes(1) |
numfmt | Format numbers for people to read | numfmt(1) |
env | Run a command with a changed environment | env(1) |
printenv | Print an environment variable | printenv(1) |
test | Check a condition | test(1) |
true, false | Succeed or fail | true(1), false(1) |
expr | Evaluate a small expression | expr(1) |
factor | Print prime factors | factor(1) |
tee | Copy a stream to a file and to output | tee(1) |
Reading System Information
Peios / Using Peios / Scripts and the System
Use these commands to read facts about the running system: the time, what is running, how much processing capacity is available, and the terminal you are using. Each value describes one moment, so don't rely on it staying the same. The man page for each command lists every option.
Read the time #
Print the current time in Coordinated Universal Time (UTC), in a form that other programs can read unambiguously:
Printing a time doesn't change the clock. Setting the clock requires the relevant authority, and can affect scheduled jobs. To manage the clock and time zone, see Keeping the Time.
Identify the system #
uname -s prints the kernel's name and uname -o the operating system's.
arch prints the machine architecture. These values don't tell you which
version of each installed component you have, or whether a particular program
will run: use the package information and each program's own checks for that.
hostname prints the machine's host name. Another machine can't necessarily
find this one by that name; that depends on the network and its name service.
Make a lasting change through the network
configuration, because a
configuration service can restore its own value over a name you set directly.
hostid prints a short identifier kept for compatibility. It can repeat
across machines, so never use it to identify a machine securely.
Check capacity and load #
nproc prints the number of processors available to the current program,
which can be fewer than the machine has. Memory and storage can limit parallel
work too, so use it as a starting point when choosing how many jobs to run.
uptime shows how long the system has been running and its load: a measure of
work that is running or waiting, not a percentage of processor use. For
signed-in sessions, use Inspecting and Ending
Sessions instead.
Check the terminal #
tty prints the terminal connected to standard input, and fails if input
comes from a pipe or a file. A script can use that status to tell whether a
person is typing. stty -a shows the terminal's settings, such as whether
typed characters are echoed. If a script changes those settings, save them
first and restore them even when the script fails.
Commands on this page #
This page covers these commands:
| Command | Use it to | Manual |
|---|---|---|
date | Print or set the time | date(1) |
uname | Print system identifiers | uname(1) |
arch | Print the machine architecture | arch(1) |
hostname | Print the host name | hostname(1) |
hostid | Print the host identifier | hostid(1) |
nproc | Print the processors available | nproc(1) |
uptime | Print how long the system has run, and its load | uptime(1) |
tty | Print the terminal connected to input | tty(1) |
stty | Read or change terminal settings | stty(1) |
Running and Controlling Commands
Peios / Using Peios / Scripts and the System
Use these commands to control how and how long another command runs. They suit short jobs in a terminal or a script. A job that must keep running, restart when it fails, or start when the machine starts belongs to the service manager instead; see Services and Jobs. The man page for each command lists every option.
Wait and set time limits #
sleep 0.1 pauses for a tenth of a second. A pause doesn't prove that
anything finished, so wait for an explicit sign that a service is ready rather
than sleeping and hoping.
timeout stops waiting for a command after a time limit:
When the limit expires, timeout sends a signal, and the command must still
respond to it. An option can force a stop after a grace period, but a command
stopped part way through can leave partial output behind. See timeout(1) for
its exit statuses.
Keep a command running after you disconnect #
nohup starts a command that keeps running when its terminal disconnects.
Output that would have gone to the terminal goes to nohup.out in the working
directory instead. A new nohup.out gets security that admits only you; an
existing one is added to and keeps its own security. nohup doesn't put the
command in the background (end the line with & for that), and it doesn't
protect the command from failing, other signals, or shutdown.
Adjust priority and buffering #
nice -n 5 runs a command with a lower scheduling preference, so it yields to
other work. Raising a command's preference needs extra authority. Niceness
doesn't change the command's identity or what it can access.
When a pipeline's output arrives late because a program holds it in a buffer,
stdbuf -oL asks the program to write each line as soon as it is complete. It
works only for programs that use the standard C output functions; a program's
own option for flushing output is more reliable.
Send signals #
peiosutils kill -l lists the signals. kill sends a signal to a process: a
request to change state, which a program can handle. The usual stop signal
lets a program clean up; a forced kill doesn't. Process numbers are reused
after a process ends, so check the target first. Whether you can signal a
process is an access decision, and knowing its number grants nothing.
Run a command in another root directory #
chroot runs a command with a different directory as its root, so that
absolute paths are found inside that directory. The directory must contain
everything the command needs, including its libraries. A changed root only
changes where paths are looked up: it doesn't change the command's access
token, privileges, network access, or open files, so it isn't a way to contain
untrusted code. It also requires the relevant authority.
Flush pending writes #
sync asks the system to write pending changes to storage. How durable the
data then is still depends on the filesystem and the device. sync doesn't
make a half-written file complete or check that its contents are right.
Commands on this page #
This page covers these commands:
| Command | Use it to | Manual |
|---|---|---|
sleep | Pause for a time | sleep(1) |
timeout | Run a command with a time limit | timeout(1) |
nohup | Keep a command running after a disconnect | nohup(1) |
nice | Run a command with a different priority | nice(1) |
stdbuf | Change a command's output buffering | stdbuf(1) |
kill | Send a signal to a process | kill(1) |
chroot | Run a command in another root directory | chroot(1) |
sync | Write pending changes to storage | sync(1) |
Checking Who a Command Runs As
Peios / Using Peios / Scripts and the System
whoami, id, groups, and logname give quick answers about the identity
a command runs with. They show the account name and group memberships, but not
everything that decides what the command can do. For that, read the command's
access token with token.
Read the account name #
whoami prints the name of the account the command runs as. If the identity
service can't find a name for the account, whoami fails, but the command
still has an identity: a failed lookup is only a failed lookup.
logname prints the account that signed in to the session. It can differ from
whoami when a program has taken on another identity.
Read identity numbers and groups #
id shows the account and its groups as numbers, with their names, and adds
the user's security identifier (SID) and integrity level. The numbers are kept
for compatibility. Peios decides access with SIDs and the other contents of
the token, and the numbers can't show all of them: some groups have no number,
and a deny-only group looks the same as an ordinary one. Two commands can
print the same numbers and still be allowed different things.
groups prints the group memberships that have numbers.
Ask about the command or about an account #
Without an account name, id and groups describe the command you run them
from, using its token. With an account name, they ask the identity service
about the account record instead. A token includes groups added at sign-in,
such as Everyone, that aren't stored in the account record, so the two answers
can differ without either being wrong.
See the full token #
When an access decision depends on more than names and numbers, read the token:
It shows every group with its attributes, the privileges, and the integrity level. See Inspecting Effective Identity.
Commands on this page #
This page covers these commands:
| Command | Use it to | Manual |
|---|---|---|
whoami | Print the account a command runs as | whoami(1) |
logname | Print the account that signed in to the session | logname(1) |
id | Print identity numbers, names, SID, and integrity level | id(1) |
groups | Print group memberships | groups(1) |
token | Show the full access token | token(1) |