LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-20-2007, 01:39 PM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
Bash script questions.


Hi guys:

Do you know how to create a file in script? And how to "connect" the file name?
For example, I want to create N files, each file name is Rand_i.out, (i from 1 to N)

Then How can I connect "Rank_" and "i" in a loop?

Appreciate!
 
Old 03-20-2007, 02:04 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,406

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
you need to give us more to go on than that. you "create" files just as normal...

maybe this is what you're after...

Code:
for i in $(seq 1 10) 
do
  echo this is file $1 > file_$(i).txt
done
 
Old 03-20-2007, 02:06 PM   #3
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Take a look at linuxcommand.org website, it's got what you're asking for (I think) and lots of more. If I understand you right, you need a loop (like FOR for example, or whatever you prefer) and then use (again -- for example) touch to create the files and depending on what you want to do with them, use > or >> to write data into the files (for example; > overwrites existing data and >> appends to the file; it's explained in the website I mentioned).
 
Old 03-20-2007, 02:55 PM   #4
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
Thanks a lot,
Here is my code:

NPROCS=10
index
while [$index -lt $NPROCS]; do
echo file $(index) > Rank_$(index).out
done

It can't pass compilation, what's wrong with it??


Quote:
Originally Posted by acid_kewpie
you need to give us more to go on than that. you "create" files just as normal...

maybe this is what you're after...

Code:
for i in $(seq 1 10) 
do
  echo this is file $1 > file_$(i).txt
done
 
Old 03-20-2007, 04:22 PM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,406

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
you could start by telling us the errors... kinda helps..


but for starters 1) your [ syntax is wrong, put spaces either side of [ and ]. and 2) you're not incrementing your value, the test will always be true.
 
Old 03-20-2007, 05:03 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,502

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Moreover, you are not setting an initial value to the index variable.
 
Old 03-20-2007, 10:59 PM   #7
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by ArthurHuang
Thanks a lot,
Here is my code:

NPROCS=10
index
while [$index -lt $NPROCS]; do
echo file $(index) > Rank_$(index).out
done

It can't pass compilation, what's wrong with it??

1. You don't compile a shell script.
2. "index" is not a command (unless you have created one). Or did you mean to initialize index as a variable (which you haven't done anywhere)?
3. You need spaces around [ and ].
4. You are using command substitution instead of variable expansion.
5. You haven't incremented index.

It should be:
Code:
NPROCS=10
index=1
while [ $index -lt $NPROCS ]; do
  echo "file $index" > Rank_$index.out
  index=$(( $index + 1 ))
done
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM
bash script: ssh to server and reply to any questions cambie Programming 2 09-28-2006 09:44 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
Bash script programming questions dianea Linux - Newbie 4 03-03-2004 12:17 AM


All times are GMT -5. The time now is 12:59 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration