I was supposed to write a program that reads and writes phone numbers of people to a file called phones.txt. The reading part works. The write part doesn't. I tried to use the cat command because I thought doing cat >> file.txt would simply append to a file. What it does instead is rewrite the file with the line going in twice.
Heres the code:
#!/bin/bash
num=$#
ch=3
cn=1
if [ $num -eq $ch ]
then
n="new"
if [[ $1 == $n ]]
then
nfile="phones.txt"
echo "$2\n$3\n" | cat >> $nfile #right here
else
echo "wrong parameter"
fi
elif [ $num -eq $cn ]
then
nnfile="phones.txt"
found="0"
one="1"
while read inLine
do
if [[ $1 == $inLine ]]
then
read inLine
echo "$1: $inLine"
fi
done < $nnfile
else
echo "improper number of arguments"
fi