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

It almost looks like you just need

echo "$2\n$3\n">>$nfile

Sorry for responding so late. Nothing happens when I replace the line with what you wrote.

So, narrowing it down to the simplest form:

SERVER:/export/home/user/science/shell$ more multiEcho.ksh
echo one > text.txt
echo two >> text.txt
echo three >> text.txt

SERVER:/export/home/user/science/shell$ more text.txt
one
two
three

...then replacing the hard-coded file name with a variable

SERVER:/export/home/user/science/shell$ more multiEcho.ksh
fileName=text.txt
echo one > $fileName
echo two >> $fileName
echo three >> $fileName

SERVER:/export/home/user/science/shell$ more text.txt
one
two
three

Either form should work correctly:

echo -e "$1\n$2\n" | cat >> $file

# Or
echo -e "$1\n$2\n" | cat - >> $file

In the second form it is explicit that STDIN is the input file.

Perhaps you should present a minimal working example of what fails so we can duplicate (including input/output and code).

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.

Your code works for me. I've cleaned it up a bit, and enclosed it in code-tags:

## use descriptive variable names, e.g.:
output="phones.txt"
input="phones.txt"

n="new"

num=$#

ch=3
cn=1

found=0
one=1

if [ "$num" -eq "$ch" ]
then
  if [ "$1" = "$n" ]
  then
    printf '%s\n' "$2" "$3" >> "$output" # echo with unknown values is deprecated
  else
    echo "wrong parameter"
  fi
elif [ "$num" -eq "$cn" ]
then
  while read inLine
  do
    if [ "$1" = "$inLine" ]
    then
      read inLine
      printf '%s: %s\n' "$1" "$inLine"
    fi
  done < "$input"
else
  echo "improper number of arguments"
fi

Please post a sample run (also within code-tags), for example:

$ xx.sh new uio jhg
$ cat phones.txt 
uio
jhg

I'm posting again to say thanks to everybody. It still prints an extra line in phones.txt but it doesn't effect program usage

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.