xref: /illumos-gate/usr/src/test/util-tests/tests/mdb/shell/tst.pipe.ksh (revision 3d4e20a23e1894c03df6b83e39a88625f74ee0e4)
1*3d4e20a2SAndy Fiddaman#!/usr/bin/ksh
2*3d4e20a2SAndy Fiddaman#
3*3d4e20a2SAndy Fiddaman# This file and its contents are supplied under the terms of the
4*3d4e20a2SAndy Fiddaman# Common Development and Distribution License ("CDDL"), version 1.0.
5*3d4e20a2SAndy Fiddaman# You may only use this file in accordance with the terms of version
6*3d4e20a2SAndy Fiddaman# 1.0 of the CDDL.
7*3d4e20a2SAndy Fiddaman#
8*3d4e20a2SAndy Fiddaman# A full copy of the text of the CDDL should have accompanied this
9*3d4e20a2SAndy Fiddaman# source.  A copy of the CDDL is also available via the Internet at
10*3d4e20a2SAndy Fiddaman# http://www.illumos.org/license/CDDL.
11*3d4e20a2SAndy Fiddaman#
12*3d4e20a2SAndy Fiddaman
13*3d4e20a2SAndy Fiddaman#
14*3d4e20a2SAndy Fiddaman# Copyright 2026 Oxide Computer Company
15*3d4e20a2SAndy Fiddaman#
16*3d4e20a2SAndy Fiddaman
17*3d4e20a2SAndy Fiddaman#
18*3d4e20a2SAndy Fiddaman# Exercise the "dcmd ! command" form, which sends the output of the preceding
19*3d4e20a2SAndy Fiddaman# dcmd to $SHELL -c and the command's output to the terminal. This drives the
20*3d4e20a2SAndy Fiddaman# posix_spawn path in mdb_shell_pipe().
21*3d4e20a2SAndy Fiddaman#
22*3d4e20a2SAndy Fiddaman
23*3d4e20a2SAndy Fiddamanexport SHELL=/bin/sh
24*3d4e20a2SAndy Fiddaman
25*3d4e20a2SAndy Fiddamanfunction check {
26*3d4e20a2SAndy Fiddaman	typeset desc="$1" expect="$2" got="$3"
27*3d4e20a2SAndy Fiddaman
28*3d4e20a2SAndy Fiddaman	if [[ "$got" != "$expect" ]]; then
29*3d4e20a2SAndy Fiddaman		print -u2 "FAIL [$desc]: expected [$expect], got [$got]"
30*3d4e20a2SAndy Fiddaman		exit 1
31*3d4e20a2SAndy Fiddaman	fi
32*3d4e20a2SAndy Fiddaman}
33*3d4e20a2SAndy Fiddaman
34*3d4e20a2SAndy Fiddaman#
35*3d4e20a2SAndy Fiddaman# A plain, unquoted filter. ::echo appends a trailing space after the last
36*3d4e20a2SAndy Fiddaman# word, which the pass-through filter preserves, so trim it before comparing.
37*3d4e20a2SAndy Fiddaman#
38*3d4e20a2SAndy Fiddamanout=$($MDB <<'EOF'
39*3d4e20a2SAndy Fiddaman::echo abc ! tr a-z A-Z
40*3d4e20a2SAndy FiddamanEOF
41*3d4e20a2SAndy Fiddaman)
42*3d4e20a2SAndy Fiddamanout=${out% }
43*3d4e20a2SAndy Fiddamancheck "plain" "ABC" "$out"
44*3d4e20a2SAndy Fiddaman
45*3d4e20a2SAndy Fiddaman#
46*3d4e20a2SAndy Fiddaman# A single-quoted awk filter, quoted with \', as a user is most likely to
47*3d4e20a2SAndy Fiddaman# write it.
48*3d4e20a2SAndy Fiddaman#
49*3d4e20a2SAndy Fiddamanout=$($MDB <<'EOF'
50*3d4e20a2SAndy Fiddaman::echo alpha beta gamma ! 'awk \'{print $3}\''
51*3d4e20a2SAndy FiddamanEOF
52*3d4e20a2SAndy Fiddaman)
53*3d4e20a2SAndy Fiddamancheck "squote-awk" "gamma" "$out"
54*3d4e20a2SAndy Fiddaman
55*3d4e20a2SAndy Fiddaman#
56*3d4e20a2SAndy Fiddaman# The same with the double-quoted form, where the single quotes around the awk
57*3d4e20a2SAndy Fiddaman# program need no escaping.
58*3d4e20a2SAndy Fiddaman#
59*3d4e20a2SAndy Fiddamanout=$($MDB <<'EOF'
60*3d4e20a2SAndy Fiddaman::echo alpha beta gamma ! "awk '{print $2}'"
61*3d4e20a2SAndy FiddamanEOF
62*3d4e20a2SAndy Fiddaman)
63*3d4e20a2SAndy Fiddamancheck "dquote-awk" "beta" "$out"
64*3d4e20a2SAndy Fiddaman
65*3d4e20a2SAndy Fiddaman#
66*3d4e20a2SAndy Fiddaman# A double-quoted awk program with the inner double quotes escaped as \" and
67*3d4e20a2SAndy Fiddaman# the field reference protected from the shell with \$, so that the $1 reaches
68*3d4e20a2SAndy Fiddaman# awk rather than being expanded by the shell.
69*3d4e20a2SAndy Fiddaman#
70*3d4e20a2SAndy Fiddamanout=$($MDB <<'EOF'
71*3d4e20a2SAndy Fiddaman::echo alpha beta gamma ! "awk \"{print \$1}\""
72*3d4e20a2SAndy FiddamanEOF
73*3d4e20a2SAndy Fiddaman)
74*3d4e20a2SAndy Fiddamancheck "dquote-escdollar" "alpha" "$out"
75*3d4e20a2SAndy Fiddaman
76*3d4e20a2SAndy Fiddamanexit 0
77