Hello,
A few days ago I created my amateur version of the cat function in bash. Here is what I've done:
#!/bin/bash
#This is mycat. Similar to cat.
#For detailed information use path/to/mycat.sh -h option
arguments=$#
if [[ "$arguments" -eq 0 ]] #in case of standard input
then
while [[ "$input" != "EndInput" ]]
do
read input
echo $input
done
else
case $1 in #take action according to specified option
"-n")
nl < $2 #number lines
;;
"-E")
sed 's/$/$/' < $2 #redirect file and add $ at the end of each line
;;
"-A"
sed 's/ /^I/g' < $2 > newfile.list
sed l < newfile.list
;;
"-T"
sed 's/ /^I/g'
;;
"-h")
echo
echo "mycat. Concatenates and/or displays files from standard input to standard output for free."
echo
echo "SYNOPSIS"
echo " /path/to/mycat.sh [OPTION] [FILE]"
echo
echo "OPTIONS"
echo " -n number all output lines"
echo " -E displays $ at the end of each line"
echo " -h to display help"
echo " -T display tabs as ^I"
echo " -A show -all"
echo
echo "EndInput to end"
;;
*) #case with more than one file.
while [ "$arguments" -ne 0 ]
do
line_number=1
last_line=$(tail -1 < $1)
while [[ "$last_line" != "$line_to_display" ]] #go through the whole file
do
last_line=$(tail -1 < $1)
line_to_display=$(head -$line_number < $1 | tail -1) #display current line
echo "$line_to_display"
((line_number++))
done
shift #go to next file and decrease $# with 1
arguments=$#
done;;
esac
fi
exit 0
I am turning to you for help because I want to enhance it a bit and make it look more like the real cat utility. The first thing I want to do is replace the EndInput with CTRL+D as the terminating character. And the second one is to make it possible to use more than one option and more than one file, like cat -n -e file1.list file2.list. The latter could be done with the getopt function in C but I would appreciate if anyone could tell how to accomplish it in bash.
I am looking forward to your advice.
Kind regards,
Valentin
I think I am giving that up. I started a C implementation of the cat and it is much easier to implement.
why do you need to "reinvent the wheel"? cat is already there for you to use.
No particular reason. I just thought it would be a useful thing for me to do because I'm a beginner and I can learn something interesting like this.
Whatever...
Hi,
I created a program that emualted the rm command in UNIX but stored the data in a seperate directory after deletion, a bit like the recycle bin in windows. This isn't the finished version but isn't to far off and it uses getopts which might be of some use.
#/bin/bash
# A program to emulate the "rm" command in UNIX.
# INITIALIZE VARIABLES
NO_ARGS=0
FLAG_R=""
FLAG_F_I=""
FLAG_V=""
TRASH=$HOME/deleted
# FUNCTIONS
function errors() {
if [ "$#" -eq "$NO_ARGS" ] ; then
echo "rm: too few arguments"
echo "Try \`rm --help' for more information."
exit 0
elif [[ ! -f "$1" && ! -d "$1" ]] ; then
echo "rm: cannot remove $ARG : no such file or directory"
exit 0
elif [[ -d $ARG && "$FLAG_R" = "" ]] ; then
echo "rm: \`$ARG' is a directory"
exit 0
else
checkExisting $1
fi
}
function checkExisting (){
if [ -d $TRASH/$1 ]; then
find "$TRASH/$1" -type f -exec shred -fu {} \; 2>/dev/null
find "$TRASH/$1" -type d -exec rmdir -p {} \; 2>/dev/null
directoryDelete $1
else
directoryDelete $1
fi
}
function directoryDelete () {
if [[ -d "$1" && "$FLAG_R" == "R" && "$FLAG_F_I" == "i" ]]; then
echo -n "rm: descend into directory \`$1'?"
read A
if [[ "$A" = [Yy] ]] ; then
echo "removing all entries of directory \`$1'?"
for FILE in $1/*
do
if [ -d $FILE ] ; then
directoryDelete $FILE
else
writePro $FILE
fi
done
echo -n "rm: remove directory \`$1'?"
read A
if [[ "$A" = [Yy] ]] ; then
mv $1 $TRASH 2>/dev/null
echo "rm: removing directory itself: \`$1'"
fi
fi
else
writePro $1
fi
}
function writePro () {
if ! [ -w "$1" ] ; then
echo -n "rm: remove write-protected file \`$*'?"
read A
if [[ "$A" = [Yy] ]] ; then
delete $1
fi
else
delete $1
fi
}
function delete() {
if [ "$FLAG_F_I" = "i" ] && [ -w "$1" ] ; then
interactive $1
elif [ "$FLAG_F_I" = "f" ] ; then
force $1
elif [ "$FLAG_R" = "R" ] ; then
remove $1
else
remove $1
fi
}
function force () {
mv -f $1 $TRASH 2>/dev/null
verbose $1
}
function remove () {
mv $1 $TRASH 2>/dev/null
verbose $1
}
function interactive () {
echo -n "rm: remove $1 ?"
read A
if [[ "$A" = [Yy] ]] ; then
remove $1
else
exit 0
fi
}
function verbose () {
if [ "$FLAG_V" = "v" ] ; then
echo "removing \`$1'"
fi
}
# PARSE OPTIONS WITH GETOPTS
while getopts :rRfvi o
do case $o in
r|R) FLAG_R=R
;;
f) FLAG_F_I=f
;;
v) FLAG_V=v
;;
i) FLAG_F_I=i
;;
*) echo "rm: invalid option -$1"
echo "try \`rm --help' for more information"
exit 0
esac
done
shift `expr $OPTIND - 1`
# START OF FLOW
if ! [ -d "$HOME/deleted" ] ; then
mkdir $HOME/delete
elif [ $# -eq $NO_ARGS ] ; then
errors
else
for ARG in $@
do
errors $ARG
done
fi