function built with builtins | Tag | ohneKontur - der Blog https://www.ohnekontur.de ohne Linien und Kanten und trotzdem gefangen Fri, 28 Nov 2014 09:53:25 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.5 `cat` as a shell builtin https://www.ohnekontur.de/2013/02/17/making-cat-a-shell-builtin/ https://www.ohnekontur.de/2013/02/17/making-cat-a-shell-builtin/#comments Sun, 17 Feb 2013 08:48:32 +0000 http://www.ohnekontur.de/?p=2259 Recently i blogged about how to get a process list if you aren’t able to fork any processes . Also a process list is allways handy, i allways find cat a very usefull tool too. But as you might have … Continue reading

The post `cat` as a shell builtin first appeared on ohneKontur - der Blog.]]>
Recently i blogged about how to get a process list if you aren’t able to fork any processes . Also a process list is allways handy, i allways find cat a very usefull tool too. But as you might have noticed cat is not a shell builtin. So everytime cat is called a new process needs to be forked. In case you can’t fork processes it’s a problem, and even if you can fork them you may save resources by “creating” a shell builtin cat.
So lets do this,
open your .profile for usage with your user only or /etc/profile for global usage (You may also use your .bashrc or .zshrc instead).
Append the following lines to the file:

#pseudo bash builtin cat function
function cat { echo “$(< $1)" }

Siehe Kommentar :)

function cat {
  if [ $# -eq 1 -a "${1:0:1}" != "-" ]
  then
    exec 3< "${1:-/dev/stdin}"
    while read -r -u3 line
    do
      echo -E ''"${line}";
    done
    echo -nE ''"${line}";
    exec 3<&-
    else
     /bin/cat "$@"
fi
}

Voila, if you start a new loginshell the cat should refer to a shell function and not to a binary.
(to test this simply type type cat result should be something like cat is a shell function )

The post `cat` as a shell builtin first appeared on ohneKontur - der Blog.]]>
https://www.ohnekontur.de/2013/02/17/making-cat-a-shell-builtin/feed/ 2