echo to a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers echo to a file
# 1  
Old 12-22-2006
Data echo to a file

I have executeable program: test.exe

echo enter a number
read number
echo a >> file
echo b >> file
echo $x >> file

./test.exe

enter a number
5

cat file I have:

a
b
5

what is another way to save
a
b
5
to a file without using " >> file" like I have!

Thanks!
# 2  
Old 12-22-2006
The real problem it looks like you were facing is reading "number" and then trying to pass it to the variable "$x" I've corrected that in the code below. The "-n" switch for echo doesn't terminate the command with a new line. I changed "file" to "file.txt" because there is a "file" command on linux, which your script would append to if it was run in the same directory as find. It would be a good idea to write a check into your script, for the existence of the file you wish to append to. -- Kent

#!/bin/bash

echo -n "Enter a number: "
read number
echo a >> file.txt
echo b >> file.txt
echo "$number" >> file.txt
# 3  
Old 12-22-2006
You can use the "tee" command with "-a" flag to append the output to a file.

Code:
echo $number | tee -a file.txt

# 4  
Old 12-22-2006
Just to follow on from my last post. You can redirect the standard output to /dev/null if you don't want the output to be displayed on the screen like in your original example by using the following code:

Code:
echo $number | tee -a file.txt &>/dev/null

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to read specified value from file and echo to the same location to other file.

Hello. I want to to backup some "default:" values from a file do some other job and after restore that "default:" values back. The problem is that the source and destination file has a lot of default: strings in it but with different values... So.. Here is an example: A part of my source... (6 Replies)
Discussion started by: ausdim
6 Replies

2. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

3. Shell Programming and Scripting

Echo all numbers in file +/-1

I have a plain text file, 344 392 352 341 405 327 456 765 234 457 874 154 . . 301 356 789 554 I need a bash script or command (Solaris 10 or Fedora 18) that echos or prints each of the values alongside with the value PLUS ONE, and the value MINUS ONE. echo $value $value+1 $value-1 ... (3 Replies)
Discussion started by: ajp7701
3 Replies

4. Shell Programming and Scripting

Help echo | sed > file

Hello, I am trying to build a very compact script for monitoring FS. I am stuck in a very simple problem (see code comment). Please for your help SERVER_NAME=`uname -n` SCRIPT_DIR=/export/home/scripts/fs_mon4 FLAG_DIR=${SCRIPT_DIR}/flags TEMP_DIR=${SCRIPT_DIR}/tmp FS="\/$"... (1 Reply)
Discussion started by: drbiloukos
1 Replies

5. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

6. Shell Programming and Scripting

echo ls to a file and then read file and selectively delete

I'm trying to write a script that will do an ls of a location, echo it into a file, and then read that file and selectively delete files/folders, so it would go something like this: cd $CLEAN_LOCN ls >>$TMP_FILE while read LINE do if LINE = $DONTDELETE skip elseif LINE =... (2 Replies)
Discussion started by: MaureenT
2 Replies

7. UNIX for Dummies Questions & Answers

Echo file name as header

I'm trying to create a header row that contains the filename of a file for all columns. e.g. file1.txt first middle last 1 2 3 4 5 6 desired output file should be: file1 file1 file1 first middle last 1 2 3 4 5 6 my code is: for file in *.txt; do echo... (3 Replies)
Discussion started by: calitiggr
3 Replies

8. UNIX for Dummies Questions & Answers

Echo own file name

Hi, I tried to google this but still unable to find the right reseverd word to echo my own file name. Example, I have a script called mytest.sh. Inside mytest.sh, how do I echo its own file name? (2 Replies)
Discussion started by: ngaisteve1
2 Replies

9. Shell Programming and Scripting

How to echo Brackets from a file

I'm a beginner of c shell scripting. How I can echo a whole line from a file with bracket in it? Ex. Inside the file.txt abcd efghi This is what inside the c shell set IN_FILE = file.txt set GREP_KEY = 'abcd' set IN_LINE = `grep ${GREP_KEY} ${IN_FILE}` echo $IN_LINE After... (7 Replies)
Discussion started by: zeahr
7 Replies

10. Shell Programming and Scripting

echo to both screen and a file

Please help me to simplify my below script. #!/bin/bash LOG_FILE=/tmp/log.txt echo "Hello" echo "Hello" >> /tmp/log.txt Is there anyway, that I can echo "Hello" to both screen and file with one single echo command ? (6 Replies)
Discussion started by: phamp008
6 Replies
Login or Register to Ask a Question