0

I have the following command which works ok but I would like to add a further variable in somehow:

#!/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 extracted${x}.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) 'y' variable by 1 and start the process over.

#!/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 "${y}_${x}" >> correct.txt;
                fi;
        rm extracted${x}.file;
    ((x++))
done

Just not totally sure how to achieve this.

2 Answers 2

1

You can use two for loops i.e. one for y and one for x (or while/until in the y's loop i.e. the outer loop):

#!/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=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 extracted${x}.file;
    done
done

Change the values to meet your need.

Example:

% cat scr.sh 
#!/bin/bash
for ((y=0; y<=3; y++)); do
    for ((x=0; x<=2; x++)); do
        echo "This is $x : $y"
    done
done

% ./scr.sh  
This is 0 : 0
This is 1 : 0
This is 2 : 0
This is 0 : 1
This is 1 : 1
This is 2 : 1
This is 0 : 2
This is 1 : 2
This is 2 : 2
This is 0 : 3
This is 1 : 3
This is 2 : 3
9
  • It doesn't increment the y, at the end of the first specified 'y' number run of 'x' it halts without going to the next iteration of 'y'.
    – th3joker
    Jan 25, 2016 at 3:52
  • @th3joker It starts y at 0, then exhaust one run of x (from 40000 to 180000) and increment y by 1 and do the x operation again, then increment y by 1 again....this loop continues until y is exhausted i.e. y become 12345....It's not what you want?
    – heemayl
    Jan 25, 2016 at 3:56
  • it is but it doesn't do that, after it exhausts x on the first round of y it stops.
    – th3joker
    Jan 25, 2016 at 3:57
  • also 'y' is the variable for skip, so skip should read: skip=$y
    – th3joker
    Jan 25, 2016 at 4:01
  • @th3joker my bad..fixed..
    – heemayl
    Jan 25, 2016 at 4:29
1

If you mean you want to test both variables use && operator and another test

until [ $x  -eq 180000" ] && [ $y -eq  9999 ]; do
2
  • I want it to do 'x' the specified number of times and then increment 'y' by 1 then complete 'x' again.
    – th3joker
    Jan 25, 2016 at 3:18
  • Hmmm, you could use an extra counter variable, but i think it would be possible with modulo operator as well. I'll edit when I've time. It's hard to post answers using phone Jan 25, 2016 at 3:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .