-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrev
More file actions
executable file
·102 lines (89 loc) · 3.56 KB
/
rev
File metadata and controls
executable file
·102 lines (89 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/sh
######################################################################
#
# REV - A POSIX Compatible Implementation of the "rev" Command
# Also Works on Just a POSIX Environment
#
# USAGE: rev [--by-myself] [filename ...]
#
# --by-myself .... (write as the 1st argument when use)
# Not use the "built-in" rev command and always
# do seq by myself but it is inferior to the
# built-in in performance
# with leading zeros.
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2020-05-06
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/misc-tools
#
######################################################################
######################################################################
# Initial Configuration
######################################################################
# === Initialize shell environment ===================================
set -u
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH 2>/dev/null)${PATH+:}${PATH-}"
case $PATH in :*) PATH=${PATH#?};; esac
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage and error message ======
print_usage_and_exit () {
cat <<-USAGE 1>&2
Usage : ${0##*/} [--by-myself] [filename ...]
Arg&Opts: Almost compatible with the original rev command
But --by-myself is the only original option, it prevent
from use the built-in same name command even if available
Version : 2020-05-06 22:42:19 JST
(POSIX Bourne Shell/POSIX commands)
* Although the bult-in rev produces better performance
than the POSIX commands set
USAGE
exit 1
}
error_exit() {
${2+:} false && echo "${0##*/}: $2" 1>&2
exit $1
}
warning() {
${1+:} false && echo "${0##*/}: $1" 1>&2
}
# === Exec the built-in rev command if OK and exists =================
by_myself=0
case "${1:-}" in
'--by-myself')
shift; by_myself=1
;;
*) export mydir=$(d=${0%/*}/;[ "_$d" = "_$0/" ]||cd "$d";echo "$(pwd)")
path0=${PATH:-}
PATH=$(printf '%s\n' "$path0" |
tr ':' '\n' |
awk '$0!=ENVIRON["mydir"]{print;}' |
tr '\n' ':' |
grep -v '^:$' |
sed 's/:$//' )
CMD_builtin=$(command -v rev 2>/dev/null || :)
case "$CMD_builtin" in '') by_myself=1;; esac
PATH=$path0
unset mydir
;;
esac
case $by_myself in 0) exec "$CMD_builtin" ${1+"$@"}; exit 1;; esac
######################################################################
# Argument Parsing and Reversing
######################################################################
# === print help if requested ========================================
case "${1:-}" in -h|-v|--help|--version) print_usage_and_exit;; esac
# === do it ==========================================================
awk 'BEGIN { ORS=""; }
{ for(i=length($0);i>0;i--){print substr($0,i,1);} print "\n";}' ${1+"$@"}
######################################################################
# Finish
######################################################################
exit 0