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.

6
  • What is "blah "blah"? Commented Jul 15, 2014 at 5:55
  • :) this is example of last file was created from running bash Commented Jul 15, 2014 at 5: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? Commented Jul 15, 2014 at 5: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... Commented Jul 15, 2014 at 6: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. Commented Jul 15, 2014 at 6:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.