We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,440 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

use cat command to append to a file?

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

4
Contributors
6
Replies
1 Month
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
dancks
Junior Poster
146 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

It almost looks like you just need

echo "$2\n$3\n">>$nfile
thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

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

dancks
Junior Poster
146 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

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
thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

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).

L7Sqr
Practically a Posting Shark
866 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

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
cfajohnson
Junior Poster
196 posts since Dec 2008
Reputation Points: 25
Solved Threads: 23
Skill Endorsements: 0

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

dancks
Junior Poster
146 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by thines01, cfajohnson and L7Sqr

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1245 seconds using 2.67MB