0

Imagine I have a directory with these files:

file-1.txt  file-2.txt ...

I want to write a condition for my bash and check if file-2.txt exists then write output to file-3.txt

sample bash script file:

while read line;do
    <A command here> #could be anything
    if [ "$?" -eq "0" ]; then
        echo "$line" >> file-$counter.txt  #Pay attention to counter here
    fi
done < $LIST

When bash is finished running and stopped. then I want to run it again. now I want to chech if last file is blah blah then tell the counter to write output to new file.

MLSC
  • 5,872
  • 8
  • 55
  • 89
  • What is "blah "blah"? – David Grayson Jul 15 '14 at 05:55
  • :) this is example of last file was created from running bash – MLSC Jul 15 '14 at 05:56
  • How about having a loop where $counter starts at 0 and it counts up by 1, and terminates as soon as `file-$counter.txt` does not exist? Is there any specific part of that that you are unable to write? – David Grayson Jul 15 '14 at 05:58
  • No , First time I run bash the output is file-1.txt . I want when it stops running next time I run the bash and output be file-2.txt. next time file-3.txt and so on... – MLSC Jul 15 '14 at 06:00
  • OK, then you could trivially modify my idea above so that `$counter` starts at 1 instead of 0. Do you understand my idea? In pseudocode: `counter = 0; while(file-$counter.txt is a file){ $counter = $counter + 1 }` At the end of the loop, you can use the string `file-$counter.txt` as the next unused file to write to. Now you just have to Google around to figure out how to write that in bash. – David Grayson Jul 15 '14 at 06:02
  • Could you post it as an answer to be well checked? thank you – MLSC Jul 15 '14 at 06:04

1 Answers1

1

You can get the last number of a file with a series of commands, then add one to get the next:

pax> ll file-*.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-1.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-10.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-11.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-12.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-13.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-14.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-15.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-2.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-3.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-4.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-5.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-6.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-7.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-8.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-9.txt

pax> next=$(expr $(ls -1 file-*.txt |sed 's/file-//;s/.txt$//' |sort -nk1 |tail -1) + 1)

pax> echo $next
16

Then you can just use that to create the next one:

pax> touch file-${next}.txt

pax> ll file-*.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-1.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-10.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-11.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-12.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-13.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-14.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-15.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:23 file-16.txt  <== here it is
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-2.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-3.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-4.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-5.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-6.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-7.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-8.txt
-rw-r--r-- 1 pax None 0 Jul 15 14:01 file-9.txt

Breaking that line down command by command:

ls -1 file-*.txt           # get all files one per line.
sed 's/file-//;s/.txt$//'  # get rid of "file-" and '.txt', leaves numbers only.
sort -nk1                  # numerically sort (1..15).
tail -1                    # get last entry only (15).
expr $(... + 1)            # add one (16).
next=$(...)                # assign it to "next" variable.

It's not always a good idea to parse the output of ls since filenames can sometimes contain unusual characters. But, if you can guarantee they'll meet your specifications, this will work fine.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953