LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2013, 07:39 PM   #1
picstruck
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Rep: Reputation: Disabled
Post 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
 
Old 04-29-2013, 07:42 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
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/
 
Old 04-29-2013, 08:04 PM   #3
picstruck
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
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?
 
Old 04-29-2013, 09:46 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
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.
 
Old 04-29-2013, 10:43 PM   #5
picstruck
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
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`"
 
Old 04-29-2013, 11:24 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 09:47 PM
Want to create loop devices kauuttt Linux - Kernel 12 12-10-2008 08:34 AM
Create loop devices? Vitalie Ciubotaru Linux From Scratch 3 11-30-2006 05:45 PM


All times are GMT -5. The time now is 09:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration