Register
It is currently Tue Dec 12, 2017 11:56 am

Run a command on a file multiple times + count variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 PostPosted: Thu Jan 21, 2016 10:14 am   

Joined: Thu Jan 21, 2016 10:08 am
Posts: 5
Hard to cram what I'm trying to do into the title:

dd bs=12345 if=filename.static of=filename+variable(number_counting_upwards).suffix count=(number_starting_at_1234_to_12345)

eg:

dd bs=12345 if=filename.static of=filename_1234 count=1234
to
dd bs=12345 if=filename.static of=filename_12345 count=12345

Hope that makes sense :-/


Top
 Profile  
 PostPosted: Thu Jan 21, 2016 11:21 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
Code:
#!/bin/bash

# always quote to avoid hazzles with spaces and alike
source_path="/path/to/some/"
file_name="someFile"
file_sep="_"

# putting em together
file_name_body="$source_path/$file_name/$file_sep"

interval=$((60*60))   # we need seconds; this one copies each hour
counter=0

# going to directory ; exiting with status code 1 if failing
cd $source_path || exit 1

# doing it often with script running always
while true; do   # endless loop
    # we need file with highest counter so we let the shell read all files
    for thisFile in *; do   # do not use ls!!! Will always fail on filenames containing spaces!!!
         # if thisFile matches==contains $file_name
         if [[ $thisFile =~ $file_name ]]; then
             thisCounter=${thisFile#$file_name_body}  # a bash expansion to get the trailing digits
             if ((counter<thisCounter)); then  # math operation comparing the two vars. No $ needed
                 counter=$thisCounter  # remember higher value
                 newestFile="$thisFile"   # remember the name of newest file
             fi
         fi
    done  # now we have the highest number of our file an newest file name
    ((counter++))  # increment counter by 1
    cp "$thisFile"  "$file_name_body$counter"
    sleep $interval
done
Use cp if you want to copy files. dd is somewhat different. It does not, what you expect it to do.
To understand the bash expansion ${thisFile#$file_name_body} read:
Code:
man bash | sed -rn '/Parameter Expansion/,/Command Substitution/p' | less


Top
 Profile  
 PostPosted: Thu Jan 21, 2016 12:16 pm   

Joined: Thu Jan 21, 2016 10:08 am
Posts: 5
I used 'until' all sorted, thanks for everyone's help, very much appreciated :)

Code:
#!/bin/bash
x=99000
until [ $x = "150000" ]; do
        dd bs=1 if=static.file of=static_${x}.file count=$x
    ((x++))
done


Top
 Profile  
 PostPosted: Thu Jan 21, 2016 1:29 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
th3joker wrote:
I used 'until' all sorted, thanks for everyone's help, very much appreciated :)

Code:
#!/bin/bash
x=99000
until [ $x = "150000" ]; do
        dd bs=1 if=static.file of=static_${x}.file count=$x
    ((x++))
done

Do NOT use dd unless you know, what you are doing.
With this command you are limiting the copy job a count number of blocks.
This can easily kill other files!!!!

DO NOT USE dd unless you know exactly what you're doing!!!!


Top
 Profile  
 PostPosted: Sun Jan 24, 2016 8:01 pm   

Joined: Thu Jan 21, 2016 10:08 am
Posts: 5
The following script works ok but I would like to add a further variable in somehow:

Code:
#!/bin/bash
x=40000
until [ $x = "180000" ]; do
        dd bs=1 if=static.file of=extracted${x}.file skip=12345 count=$x;
        first_ten=$(hexdump -e '1/1 "%.2X"' "extracted${x}.file" | head -c 10);
                if [ "$first_ten" == "1234567890" ]
                then
                echo "${x}" >> correct.txt;
                fi;
        rm *.file;
    ((x++))
done


I would like to add in an incremental variable to the skip section so once it's completed the 'x' variable to then increment the skip number by 1 and start the process over.

Code:
#!/bin/bash
y=12345
x=40000
until [ $x = "180000" ]; do
        dd bs=1 if=static.file of=extracted${x}.file skip=$x count=$x;
        first_ten=$(hexdump -e '1/1 "%.2X"' "extracted${x}.file" | head -c 10);
                if [ "$first_ten" == "1234567890" ]
                then
                echo "${x}" >> correct.txt;
                fi;
        rm *.file;
    ((x++))
done


Just not totally sure how to achieve this.


Top
 Profile  
 PostPosted: Sun Jan 24, 2016 9:40 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
There are lots of tools reading and printing files in binary mode. Even grepping is available.

Why do you insist in using dangerous wrong tools?


Top
 Profile  
 PostPosted: Mon Jan 25, 2016 9:07 am   

Joined: Thu Jan 21, 2016 10:08 am
Posts: 5
This did it:
Code:
#!/bin/bash
for ((y=0; y<12345; y++)); do
    for ((x=40000; x<180000; x++)); do
        dd bs=1 if=static.file of=extracted${x}.file skip=$y count=$x;
        first_ten=$(hexdump -e '1/1 "%.2X"' "extracted${x}.file" | head -c 10);
                if [ "$first_ten" == "1234567890" ]
                then
                echo "${x}" >> correct.txt;
                fi;
        rm extracted${x}.file;
    done
done


I'm extracting part of a file at specific points from and to then running a check on the content, dd seemed the best method to do this.


Top
 Profile  
 PostPosted: Mon Jan 25, 2016 12:57 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
using dd is one of the badest way doing this.


Top
 Profile  
 PostPosted: Tue Jan 26, 2016 7:28 pm   

Joined: Thu Jan 21, 2016 10:08 am
Posts: 5
How else would you extract part of a file then?


Top
 Profile  
 PostPosted: Tue Jan 26, 2016 9:29 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
grep has an option for reading binary files.
See man grep

sed or awk can too.

All three give results directly.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP