Echo For ... Loop output into a file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Echo For ... Loop output into a file
# 1  
Old 12-05-2019
Echo For ... Loop output into a file

Hi, All:

I have one for ... loop code to generate a list of rename statement. However, echo the loop output on screen is OK. But store the echo output into a file is not working. So come here to seek help. My basic code is:
Code:
! /bin/ksh
echo > DAS_VetFed.txt
dir=/mydirectory
cd $dir
files=`ls -h *.csv`
for file in $files
do
echo "rename " "$file " "$file " ".ack" >> DAS_VetFed.txt
done

Because for each file, echo will output as " rename filename1.csv filename1.csv.ack" on screen and should be in file. I used >> to append next output into file. But it didn't work. I am working on Solaris 11.2. Please put your comment and code. Thanks in advance.
# 2  
Old 12-05-2019
Why not

Code:
for ...
   do ...
   done > DAS_VetFed.txt

This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-05-2019
RudiC:

Thanks for advice. I have tried that and many other things such as taking off double quote, using single quote and so on. It all didn't work. This rename statement is to use to rename files on remote SFTP server. Due to command available on FTP server is limited. I want to generate this rename statement and call from SFTP to reach my goal. Thanks.
# 4  
Old 12-05-2019
Quote:
Originally Posted by duke0001
Hi, All:

I have one for ... loop code to generate a list of rename statement. However, echo the loop output on screen is OK. But store the echo output into a file is not working. So come here to seek help. My basic code is:
Code:
! /bin/ksh
echo > DAS_VetFed.txt
dir=/mydirectory
cd $dir
files=`ls -h *.csv`
for file in $files
do
echo "rename " "$file " "$file " ".ack" >> DAS_VetFed.txt
done

Because for each file, echo will output as " rename filename1.csv filename1.csv.ack" on screen and should be in file. I used >> to append next output into file. But it didn't work. I am working on Solaris 11.2. Please put your comment and code. Thanks in advance.
first off: #!/bin/ksh
secondly, where do you expect your DAS_VetFed.txt file to be? Looks like the working dir where the script was invoked from.
But then I see:
Code:
dir=/mydirectory
cd $dir

So I'd expect the DAS_VetFed.txt file to be in the /mydirectory directory - unless you specify a diff directory for the output file.....
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 12-05-2019
vgersh99:

Thanks for your attention. I need to explain here. The code I posted here is part of whole script. The script will be run on local Solaris server to generate the rename files for SFTP remote server to call. If off #, will it work? Secondly, the dir=mydirectory is working directory where script is invoked. I also want to put DAS_VetFed.txt into this working directory. Do you mean I need to put output file in different directory? The it will work? Please advise. Thanks.
# 6  
Old 12-05-2019
Please become accustomed to supplying decent and sufficient information. "But it didn't work." is not a good starting point for an analysis beyond guessing. Is That file created at all? Is it empty? Does it contain insufficient or even wrong data?


I'd expect two files to be created: one (empty!) in the starting directory, and one in /mydirectory, where you end up when the script is finished.
# 7  
Old 12-05-2019
RudiC:

Sorry for not providing detailed information. I will pay attention future.
Based on vgersh99's advice. I looked at directory settings. I found that my setting had problem. Since no data in working directory, I do go to /mydirectory/archive to execute script and put output file in /mydirectory. This may be the cause to make echo for ...loop can put output on screen and cannot log output into a file. Then I copy some files to /mydirectory and invoke script in the same directory. Now it works. This is my ignorance on directory setting. But thanks for RudiC and vgersh99's help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

For loop output to excel file

Hi, I have a shell script which analyses the log folder for a specific string and throws me the output. I have used for loop since it does this in multiple servers. Now I want to save the output in a excel in the below format. Can someone please help? The output which I get Server1 : count... (1 Reply)
Discussion started by: srilaxman
1 Replies

2. UNIX for Beginners Questions & Answers

Need to save output of echo and awk to a file

Hi, I am generating a YAML file from a hosts file, but am having trouble saving it to a new file. hosts file 127.0.0.1 localhost 192.168.1.2 host1 192.168.1.3 host2 192.168.1.4 host3 192.168.1.5 host4 YAML file $ echo 'host_entries:' && awk '{printf " %s:\n ip:... (3 Replies)
Discussion started by: sand1234
3 Replies

3. Shell Programming and Scripting

BASH - Need to echo for loop output to one line

I'm trying to echo the release version of some of our Linux servers. Typically I do these types of things by "catting" a text file with the host names, "ssh-ing" to the host and running my string. This is what I've written for i in `cat versions.txt` ; do echo $i ; ssh $i cat /etc/issue |... (5 Replies)
Discussion started by: lombardi4851
5 Replies

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

5. Shell Programming and Scripting

Echo prompt and redirecting the output to a file

Hi Help, I have a script which looks like below. echo "Train count??" set train_count = $< cat tmp | awk -v var=${train_count} '{print $0"var"}' > tmp The echo "Train Count??" does not show up in the terminal due to redirecting to output file tmp. How is it possible to have the prompt... (3 Replies)
Discussion started by: Indra2011
3 Replies

6. UNIX for Dummies Questions & Answers

Redirect output of echo to a file

Hi , I am trying to redirect output of echo to a file.So i wrote a function named printline.Here is my sample script myscript.sh function printline() { echo "$1" >> myfile.log } usage() { printLine "********************USAGE*************************" printLine "Script takes... (12 Replies)
Discussion started by: ASC
12 Replies

7. Homework & Coursework Questions

Loop to Convert a list from an input file and output it to another file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: A) Write a script, which will take input from a file and convert the number from Centigrade to Fahrenheit... (5 Replies)
Discussion started by: AliTheSnake
5 Replies

8. Shell Programming and Scripting

Script to loop line in a file and add info or do echo

I have a record.txt it will update weekly, and it could be 2 lines or more ... it just echo each line to the script san jose,23.34% tampa,2.15% dallas,30.20% seattle,44.29% Unknown,16.72% How do i write a shell script to give me a test.pl or bash file which contain #!/home/perl... (8 Replies)
Discussion started by: sabercats
8 Replies

9. Shell Programming and Scripting

problem with suppressed output to file using echo and tee command

Hi, When I run the following command in terminal it works. The string TEST is appended to a file silently. echo TEST | tee -a file.txt &>/dev/null However, when I paste this same line to a file, say shell1.sh, and use bourne shell . I run this file in terminal, ./shell1.sh. However I... (1 Reply)
Discussion started by: shahanali
1 Replies

10. Shell Programming and Scripting

ksh script that echo " please insert your name " and store the output to a login.log file.

Hello All Nice to meet you all here in this forum, it's my 1rst time here i'm asking about a little issue that i face i added a ksh script that echo " please insert your name " and store the output to a login.log file. the script is working fine with normal telnet but Xstart is not working... (8 Replies)
Discussion started by: islam.said
8 Replies
Login or Register to Ask a Question