Jump to content

Bash Script - For or While loop

spbr

Hi,

With the below code, if I wanted to increment this, so once it has done the first it moves down to the next line, would it be better to use a for loop or a while loop:

#!/bin/bash
LINE=3
( head -1 file2.log
  head -${LINE} file1.log | tail -1
  tail --lines=+2 file2.log
) > file2.new

mv file2.log file2.old
mv file2.new file2.log

Thank you

Link to comment
Share on other sites

Link to post
Share on other sites

if you need to do it a set number of times then use a for loop of you need to wait for a condition to be met but not sure how many loops ot will take use while.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, vorticalbox said:

if you need to do it a set number of times then use a for loop of you need to wait for a condition to be met but not sure how many loops ot will take use while.

 
 

So I would want to increment this, how could that be done?

Thank you

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, spbr said:

So I would want to increment this, how could that be done?

Thank you

like I said if you know how many times you need to increment it then use a for loop.

 

I'm unsure of the syntax of bash.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

I found this example online.  if you want to use while loop, just put your code betwen while and done.

 

 #!/bin/bash 
         COUNTER=0
         while [  $COUNTER -lt 10 ]; do
             echo The counter is $COUNTER
             let COUNTER=COUNTER+1 
         done

 

check out this site

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

Link to comment
Share on other sites

Link to post
Share on other sites

Hi

#!/bin/bash
LINE=1
while [ $LINE -lt 10 ];
do
LINE=$((LINE+1))
        ( head -1 file2.log
          head -${LINE} file1.log | tail -1
          tail --lines=+2 file2.log
        ) > file2.new
done

Need help with the above as it only prints the last line, but I want it to increment each line of a word list.

So everytime the loop runs it prints one line and moves down to the next one.
 

Thank you

Link to comment
Share on other sites

Link to post
Share on other sites

A quick google search turned up this thread on Stackoverflow:

http://stackoverflow.com/questions/10929453/read-a-file-line-by-line-assigning-the-value-to-a-variable

 

The top rated suggestion may be useful for you.

Intel i7 5820K (4.5 GHz) | MSI X99A MPower | 32 GB Kingston HyperX Fury 2666MHz | Asus RoG STRIX GTX 1080ti OC | Samsung 951 m.2 nVME 512GB | Crucial MX200 1000GB | Western Digital Caviar Black 2000GB | Noctua NH-D15 | Fractal Define R5 | Seasonic 860 Platinum | Logitech G910 | Sennheiser 599 | Blue Yeti | Logitech G502

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×