Skip to content

Commit

Permalink
making scripts POSIX compliant, where possible
Browse files Browse the repository at this point in the history
remove most references to "bash"

rename bashrc into gshrc in treasure for 02_alias_journal

all scripts (?) except some 'auto.sh' and 'test.sh' scripts that need
inserting commands in the history should work in any POSIX compatible
shell.

rename GSH_VAR to GSH_TMP

set TMPDIR to $GSH_TMP so that mktemp creates temporary files there.
  • Loading branch information
phyver committed Jun 17, 2021
1 parent 82196b7 commit 511e248
Show file tree
Hide file tree
Showing 278 changed files with 1,945 additions and 1,548 deletions.
2 changes: 1 addition & 1 deletion bin/_gsh_protect
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
chmod a-rw "$GSH_ROOT"
chmod a-rw "$GSH_MISSIONS"
chmod a-r "$GSH_CONFIG"
chmod a-r "$GSH_VAR"
chmod a-r "$GSH_TMP"
chmod a-rw "$GSH_SBIN"
4 changes: 2 additions & 2 deletions bin/_gsh_systemconfig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# #!/bin/sh
#!/bin/sh

# display some info about the system
echo "========================="
echo "OSTYPE=$OSTYPE"
echo "bash' OSTYPE=$(bash -c 'echo $OSTYPE')"
echo "========================="
echo "uname -a"
uname -a
Expand Down
2 changes: 1 addition & 1 deletion bin/_gsh_unprotect
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
chmod "$(umask -S)" "$GSH_ROOT"
chmod "$(umask -S)" "$GSH_MISSIONS"
chmod "$(umask -S)" "$GSH_CONFIG"
chmod "$(umask -S)" "$GSH_VAR"
chmod "$(umask -S)" "$GSH_TMP"
chmod "$(umask -S)" "$GSH_SBIN"
4 changes: 2 additions & 2 deletions bin/box.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ do
;;
esac
done
shift $(($OPTIND - 1))
shift $((OPTIND - 1))

# get filename, or "-"
filename=${1:--}
Expand Down Expand Up @@ -143,7 +143,7 @@ else
height=$(echo "$w_h" | cut -d' ' -f2)
fi

$AWK -v box="$box" -v width=$width -v height=$height -f ./boxes-data.awk -f ./box.awk "$tmpfile" | decode
$AWK -v box="$box" -v width="$width" -v height="$height" -f ./boxes-data.awk -f ./box.awk "$tmpfile" | decode

rm -f "$tmpfile"
fi
2 changes: 1 addition & 1 deletion bin/missionname
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ missionname() {
[ -f check.sh ] && break
[ -f static.sh ] && break
[ -f init.sh ] && break
[ $(pwd -P) = "/" ] && break
[ "$(pwd -P)" = "/" ] && break
cd ..
done

Expand Down
120 changes: 71 additions & 49 deletions bin/progress_bar
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
#!/bin/sh

# display a small animation until the given PID stops

. gettext.sh

Expand Down Expand Up @@ -29,7 +31,7 @@ init_data() {
case $design in
"dots")
# simple dots
STR=('.')
STR='.'
PRE=''
POST='\n'
MSG=''
Expand All @@ -38,7 +40,7 @@ init_data() {

"bar")
# simple dots
STR=('=')
STR='='
PRE='['
POST=']\n'
MSG=''
Expand All @@ -47,7 +49,10 @@ init_data() {

"rotation")
# rotating bar
STR=('\b|' '\b/' '\b-' '\b\\')
STR='\b|
\b/
\b-
\b\'
PRE='-'
POST='\b \b'
MSG=""
Expand All @@ -56,8 +61,15 @@ init_data() {

"bat")
# flying bat
STR=('\b\b\b \,/' '\b\b\b \,/' '\b\b\b \,/' '\b\b\b \,/'
'\b\b\b /`\' '\b\b\b /`\' '\b\b\b /`\' '\b\b\b /`\')
STR='\b\b\b \,/
\b\b\b \,/
\b\b\b \,/
\b\b\b \,/
\b\b\b /`\
\b\b\b /`\
\b\b\b /`\
\b\b\b /`\'

PRE=' '
POST='\b\b\b \r'
MSG="$(gettext "While you are waiting, a bat flies by...")\\n"
Expand All @@ -66,59 +78,56 @@ init_data() {

"snake")
# slithering snake
STR=('\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b __/\__/\__/\<:>'
'\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b _/\__/\__/\_<:>'
'\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b /\__/\__/\__<:>'
'\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \__/\__/\__/<:>')
STR='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b __/\__/\__/\<:>
\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b _/\__/\__/\_<:>
\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b /\__/\__/\__<:>
\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \__/\__/\__/<:>'
PRE='\__/\__/\__/<:>'
POST='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a snake slithers by...")\\n"
DELAY=0.2
;;

"caterpillar")
STR=('\b\b\b\b\b\b\b\b\b ___^___@'
'\b\b\b\b\b\b\b\b __/\__@'
'\b\b\b\b\b\b\b _/^\_@'
'\b\b\b\b\b\b __/\__@'
'\b\b\b\b\b\b\b ___^___@'
'\b\b\b\b\b\b\b\b\b ________@')
STR='\b\b\b\b\b\b\b\b\b ___^___@
\b\b\b\b\b\b\b\b __/\__@
\b\b\b\b\b\b\b _/^\_@
\b\b\b\b\b\b __/\__@
\b\b\b\b\b\b\b ___^___@
\b\b\b\b\b\b\b\b\b ________@'
PRE='_______@"'
POST='\b\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a caterpillar crawls by...")\\n"
DELAY=0.2
;;

"snail")
# Snail
STR=('\b\b\b\b\b\b\b_(@)__/"'
'\b\b\b\b\b\b\b\b__(@)_/"'
'\b\b\b\b\b\b\b\b _(@)_/"')
STR='\b\b\b\b\b\b\b_(@)__/"
\b\b\b\b\b\b\b\b__(@)_/"
\b\b\b\b\b\b\b\b _(@)_/"'
PRE='_(O)_/"'
POST='\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a snail goes by...")\\n"
DELAY=0.3
;;

"spider")
STR=(
'\b\b\b\b\b //o\\\\'
'\b\b\b\b\b //o\\\\'
'\b\b\b\b\b///o\\'
'\b\b\b\b\b///o\\'
)
# FIXME: not sure I understand the escaping here!
STR='\b\b\b\b\b //o\\\\\\\\
\b\b\b\b\b //o\\\\\\\\
\b\b\b\b\b///o\\\\
\b\b\b\b\b///o\\\\'
PRE='//o\\\\'
POST='\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a spider crawls by...")\\n"
DELAY=0.2
;;

"duck")
STR=(
'\b\b\b\b\\_@<'
'\b\b\b\b \\_@='
'\b\b\b\b\\_@='
'\b\b\b\b\\_@<')
STR='\b\b\b\b\\_@<
\b\b\b\b \\_@=
\b\b\b\b\\_@=
\b\b\b\b\\_@<'
PRE='\\_@<'
POST='\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a duck swims by...")\\n"
Expand All @@ -127,12 +136,22 @@ init_data() {

"fish-back-and-forth")
# Swimming fish (small) going back and forth
STR=('\b\b\b ><>' '\b\b\b ><>' '\b\b\b ><>' '\b\b\b ><>'
'\b\b\b ><>' '\b\b\b ><>' '\b\b\b ><>' '\b\b\b ><>'
'\b\b\b \b\b\b\b<><' '\b\b\b \b\b\b\b<><'
'\b\b\b \b\b\b\b<><' '\b\b\b \b\b\b\b<><'
'\b\b\b \b\b\b\b<><' '\b\b\b \b\b\b\b<><'
'\b\b\b \b\b\b\b<><' '\b\b\b \b\b\b\b<><')
STR='\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><'
PRE='><>'
POST='\b\b\b \r'
MSG="$(gettext "While you are waiting, you see a fish swimming in circles...")\\n"
Expand All @@ -141,8 +160,8 @@ init_data() {

"centipede")
# crawling centipede
STR=('\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b ,`,`,`,`,`,`,`(:)'
'\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b`,`,`,`,`,`,`,(:)')
STR='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b ,`,`,`,`,`,`,`(:)
\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b`,`,`,`,`,`,`,(:)'
PRE='`,`,`,`,`,`,`,(:)'
POST='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a centipede crawls by...")\\n"
Expand All @@ -151,7 +170,7 @@ init_data() {

"ant")
# crawling ant
STR=('\b\b\b\b\b >|<()')
STR='\b\b\b\b\b >|<()'
PRE='>|<()'
POST='\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, an ant crawls by...")\\n"
Expand All @@ -160,7 +179,7 @@ init_data() {

"fish")
# Swimming fish (small)
STR=('\b\b\b ><>')
STR='\b\b\b ><>'
PRE='><>'
POST='\b\b\b \r'
MSG="$(gettext "While you are waiting, a fish swims by...")\\n"
Expand All @@ -169,7 +188,7 @@ init_data() {

"large-fish")
# Swimming fish (large)
STR=("\b\b\b\b\b\b\b\b ><(((('>")
STR="\b\b\b\b\b\b\b\b ><(((('>"
PRE="><(((('>"
POST='\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a large fish swims by...")\\n"
Expand All @@ -195,14 +214,17 @@ animation() {

# Make progress for each character read on [stdin].
printf "$PRE"
while true
while kill -0 "$PID" 2>/dev/null
# NOTE: we need to test that PID is still active here as well, because
# the "break" in the body of the loop takes place in a subshell. It is
# not possible to return from the function from there (using "break 2" or
# "return")!
do
for s in "${STR[@]}"
printf "$STR\n" | while read -r line
do
printf "$s"
# Slow down the animation a little bit.
printf "$line"
sleep $DELAY
kill -0 $PID 2>/dev/null || return
kill -0 "$PID" 2>/dev/null || break
done
done
}
Expand Down Expand Up @@ -230,14 +252,14 @@ EOS
PID=$1

get_design "$design"
if [ "$design" == RANDOM ]
if [ "$design" = RANDOM ]
then
n=$(RANDOM)
get_design "$((n%7))" # only modulo 7 to get the "interesting" animations
fi

init_data "$design"
if kill -0 $PID 2>/dev/null
if kill -0 "$PID" 2>/dev/null
then
animation
printf "$POST"
Expand Down
2 changes: 1 addition & 1 deletion bin/realpath
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ realpath() (
command cd -- "$dir" 2> /dev/null || { echo "realpath: $arg1: cannot access final dirname $dir" >&2; return 1; }
dir=$(command pwd -P)
[ "$dir" = "/" ] && dir=""
command printf "%s/%s\n" "$dir" "$(command basename -- "$target")"
command printf '%s/%s\n' "$dir" "$(command basename -- "$target")"
return 0
fi
done
Expand Down
26 changes: 23 additions & 3 deletions bin/save_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,30 @@
# aliases and processes

{
compgen -v | sed 's/^/Variable:/'
## get alias names
# for bash
# compgen -a | sed 's/^/Alias:/'
# or
alias | sed -e 's/^alias *//' -e 's/=.*//' -e 's/^/Alias:/'

## get variable names
# for bash
# compgen -v | sed 's/^/Variable:/'
# or, for both bash and zsh
[ -n "$BASH_VERSION" ] && set -o posix
set | sed -e 's/=.*//' -e 's/^/Variable:/'
[ -n "$BASH_VERSION" ] && set +o posix

## get function names
# for bash
compgen -A function | sed 's/^/Function:/'
compgen -a | sed 's/^/Alias:/'
# for zsh
# print -l ${(ok)functions}

## get processes
ps -o pid,comm | sed '1d;s/^/Process:/'

## get working directory
pwd | sed 's/^/PWD:/'
} | grep -vE "grep|ps|sed|sort|bash" | sort

} | grep -vE "grep|ps|sed|sort|bash|zsh" | sort
2 changes: 1 addition & 1 deletion bin/sed-i
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sed_i() {
cp "$tmp" "$filename"
rm -f "$tmp" "$bak"
else
local ret=$?
ret=$?
rm -f "$tmp" "$bak"
return $ret
fi
Expand Down
10 changes: 5 additions & 5 deletions bin/seq
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

display_help() {
cat <<EOH
cat <<'EOH'
Usage: seq [OPTION] LAST
or: seq [OPTION] FIRST LAST
or: seq [OPTION] FIRST STEP LAST
Expand All @@ -17,7 +17,7 @@ EOH
seq() (

FORMAT="%d"
SEP="\n"
SEP='\n'

while getopts "hf:s:" opt
do
Expand Down Expand Up @@ -63,12 +63,12 @@ seq() (
esac

i=$start
while [ "$i" -lt $end ]
while [ "$i" -lt "$end" ]
do
printf "$FORMAT$SEP" $i
printf "$FORMAT$SEP" "$i"
i=$((i+step))
done
[ "$i" -eq $end ] && printf "$FORMAT" $i
[ "$i" -eq "$end" ] && printf "$FORMAT" $i
echo
return 0
)
Expand Down
2 changes: 1 addition & 1 deletion bin/sign_file
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sign_file() {
tempfile=$target
else
target=$source
tempfile=$(mktemp -t sign_file-XXXXXX)
tempfile=$(mktemp)
fi
rd=$(RANDOM)
if [ -s "$source" ]
Expand Down
Loading

0 comments on commit 511e248

Please sign in to comment.