LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   create file in for loop. (https://www.linuxquestions.org/questions/linux-newbie-8/create-file-in-for-loop-4175460078/)

picstruck 04-29-2013 06:39 PM

create file in for loop.
 
I'm stuck and I've been searching and can't figure out how to do the script to this question.

Make a bash script that will use a for loop to create 130 test outputs called Outputs/txxx.out where xxx is the test number 1 to 130. Each .out file will have the contents:

Test: xxx
Date: current date

chrism01 04-29-2013 06:42 PM

Start by showing use your code so far and exactly what errors/msgs you get.
Also, bookmark/read
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

picstruck 04-29-2013 07:04 PM

This is all I have.

Quote:

for ((i = 0 ; i <= 130 ; i++))
do
touch Outputs/t$i.out
echo "Tests: $i"
echo "Date:"
date
done
I don't know how to send the test and date into the file. How do I send echo "Tests: $i" echo "Date:" date into the output file?

chrism01 04-29-2013 08:46 PM

Just append the filename
Code:

for ((i = 0 ; i <= 130 ; i++))  # here you want <130 or you'll get 131 ie 0-130 inclusive
do
    touch Outputs/t$i.out
    echo "Tests: $i" >Outputs/t$i.out
    echo "Date:" >>Outputs/t$i.out
    date
done

Notes:
1. indent your code
2. 'touch' is redundant; the echo will create the file
3. '>' means output to file, overwriting what's there! '>>' means append.
In fact, you could use append on both cmds; it also creates the file if not extant
4. why 'date'; that doesn't do anything ... see note 5
5. I think what you mean for the last 2 lines is
Code:

echo "Date: $(date)" >>Outputs/t$i.out
Do read those links above.

picstruck 04-29-2013 09:43 PM

Thanks, I thought that appending with >> deleted the content of the file before appending.

Note:
On the date part of the code i meant to write it like this
Quote:

echo "Date: `date`"

shivaa 04-29-2013 10:24 PM

If your ultimate purpose is to create 130 files, ranging from 1 to 130, then the touch command can do the same without using a for loop or script, like this:
Code:

~$ cd Outputs
~$ touch {1..130}.out



All times are GMT -5. The time now is 10:20 PM.