Hello,
A few days ago I created my amateur version of the cat function in bash. Here is what I've done:
Code:
#!/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