Created
December 6, 2025 19:16
-
-
Save Lohann/39816e43b5dae0091fa903d636eeaa20 to your computer and use it in GitHub Desktop.
Shell script that prints recursively what is the current shell emulator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Prints recursively what is the current shell emulator. | |
| pid=$$ | |
| printf 'pid="%s"\n' "${pid}" | |
| test "${pid}" = '1' && exit 1 | |
| die(){ | |
| echo "${1}" | |
| exit 1 | |
| } | |
| [ ! -x "$(command -v ps)" ] && die 'ps not found' | |
| [ ! -x "$(command -v tail)" ] && die 'tail not found' | |
| [ ! -x "$(command -v awk)" ] && die 'awk not found' | |
| [ ! -x "$(command -v grep)" ] && die 'grep not found' | |
| walk_ppid(){ | |
| pid="$(ps -h -o ppid -p "${pid}" 2>/dev/null | tail -n -1 | awk '{print $NF}')" | |
| printf 'pid="%s"\n' "${pid}" | |
| cmd="$(ps -h -o comm -p "${pid}" 2>/dev/null | tail -n -1)" | |
| printf 'command="%s"\n\n' "${cmd}" | |
| } | |
| re='^[0-9]+$' | |
| while [ "${pid}" -gt 1 ] | |
| do | |
| if ! echo "${pid}" | grep -Eq "${re}"; then | |
| printf 'not an integer "%s"' "${pid}" | |
| exit 1 | |
| fi | |
| walk_ppid "${pid}" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment