LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script questions. (https://www.linuxquestions.org/questions/programming-9/bash-script-questions-539081/)

ArthurHuang 03-20-2007 01:39 PM

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!

acid_kewpie 03-20-2007 02:04 PM

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


b0uncer 03-20-2007 02:06 PM

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

ArthurHuang 03-20-2007 02:55 PM

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



acid_kewpie 03-20-2007 04:22 PM

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.

jlliagre 03-20-2007 05:03 PM

Moreover, you are not setting an initial value to the index variable.

cfaj 03-20-2007 10:59 PM

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



All times are GMT -5. The time now is 10:02 AM.