Very nice rowanthorpe.. I try to use bash builtins to spawn as few subshells and processes with fd's as possible.. always on a some server.
The pipe vs. redir question is one I've been trying to figure out too.. One of the best ways I've found to handle that is by using exec manually on fds. Consider this dos2unix clone and it's alternate way of determining input. N6=/dev/null personal pref..
dos2unixx ()
{
[[ $# -eq 0 ]] && exec tr -d '\015\032' || [[ ! -f "$1" ]] && echo "Not found: $1" && return;
for f in "$@";
do
[[ ! -f "$f" ]] && continue;
tr -d '\015\032' < "$f" > "$f.t" && cmp "$f" "$f.t" > $N6 && rm -f "$f.t" || ( touch -r "$f" "$f.t" && mv "$f" "$f.b" && mv "$f.t" "$f" && rm -f "$f.b" ) >&$N6;
done
}
And strangely enough, earlier today I was doing some work on my own builtin MORE command, basically I wanted a cat pager, this does pretty good but I've only had it a day..
shmore ()
{
local l L M="`echo;tput setab 4&& tput setaf 7||echo -en \"\e[34;01\"` --- SH More --- `tput sgr0||echo -e \"\e[m"`";
L=1;
while read l; do
echo "${l}";
((L++));
[ "$L" == "${LINES:-80}" ] && {
L=1;
read -p"$M" -u1
};
done
}
Finally, here's the shcat I use.. and if you do $ cat file | shcat | head, you get an error from the pipe issue you talk about. However you can work around it with an $ exec 2>&1 in the correct place.
shcat ()
{
local l f e IFS="";
e=0;
if [ $# -eq 0 ]; then
while read -r l; do
echo "${l}";
done;
else
for f in "$@";
do
if [ -r "${f}" ]; then
while read -r l; do
echo "${l}";
done < "${f}";
else
< "${f}";
e=1;
fi;
done;
return $e;
fi
}
Also, these 2 aliases I have created over time that work very well for stuff like this.
cata='exec 2>&1 cat -A'
cate='exec 2>&1 cat -v | sed s/\\^\\[/\\\\033/g'
---------- Post updated at 02:13 AM ---------- Previous update was at 02:08 AM ----------
seq -s`echo -ne \012` --format=%03g 0 128
seq -s`echo -ne \\012` --format=%03g 0 128
seq -s`echo -ne "\\012"` --format=%03g 0 128
seq -s`echo -ne "\\011"` --format=%03g 0 128
seq -s`echo -ne "\\010"` --format=%03g 0 128
seq -s`echo -ne "\\009"` --format=%03g 0 128
seq -s`echo -ne "\\002"` --format=%03g 0 128
seq -s`echo -ne "\\02"` --format=%03g 0 128
seq -s`echo -ne "\\2"` --format=%03g 0 128
seq -s`tput cols`` --format=%03g 0 128
seq -s`tput cols` --format=%03g 0 128
seq -s`tput sgr` --format=%03g 0 128
seq -s`tput eol` --format=%03g 0 128
seq -s`tput erase` --format=%03g 0 128
seq -s`tput bs` --format=%03g 0 128
seq -s`tput kbs` --format=%03g 0 128
seq -s'`tput kbs` ' --format=%03g 0 128
seq -s"`tput kbs` " --format=%03g 0 128
seq -s" `tput kbs` " --format=%03g 0 128
seq -s" `tput kbs`" --format=%03g 0 128
seq -s" `tput kbs` " --format=%03g 0 128
part of my shell session from earlier today... I was actually trying to make the separator be a null.. might be useful to know there are several ways to output nulls..
aa_print_ascii_chart ()
{
local i;
for i in `seq ${1:-0} ${2:-256}`;
do
echo -e "\\0$(( $i/64*100 + $i%64/8*10 + $i%8 ))";
done
}