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:

! /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.

Why not

for ...
   do ...
   done > DAS_VetFed.txt
1 Like

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.

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:

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

1 Like

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.

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.

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.