Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Loop

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-05-2013
hadinetcat hadinetcat is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 7 February 2015, 1:53 PM EST
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Loop

Hi
I wrote shell script to do the flowing count from 5 and down but doesn’t work please see my program

Code:
#Bash

x=0
y=10


while  (( x < y ))
do
(( x = x + 1 ))

grep -A $x 5 numbers >>file4.txt
done

these the output

Code:
5
6
5
6
7
5
6
7
8
5
6
7
8
9
5
6
7
8
9
10
5
6
7
8
9
10

5
6
7
8
9
10

Moderator's Comments:
Loop Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-05-2013 at 03:57 PM.. Reason: code tags, please!
Sponsored Links
    #2  
Old Unix and Linux 01-05-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 5 December 2016, 4:51 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 Posts
Your requirement is not so clear. Can you provide desired output?

BTW If your requirement is to generate sequence of numbers ranging from 0 - 10 in BASH. Then you can simply use a for loop

Code:
#!/bin/bash

for x in {0..10}
do
  echo $x
done

Sponsored Links
    #3  
Old Unix and Linux 01-05-2013
hadinetcat hadinetcat is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 7 February 2015, 1:53 PM EST
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
loop

I would like to have the output like this
5
6
7
8
9
    #4  
Old Unix and Linux 01-05-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 5 December 2016, 4:51 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 Posts

Code:
#!/bin/bash

for x in {5..9}
do
  echo $x
done

Sponsored Links
    #5  
Old Unix and Linux 01-05-2013
hadinetcat hadinetcat is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 7 February 2015, 1:53 PM EST
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
loop

Im using grep -A

---------- Post updated at 02:25 AM ---------- Previous update was at 02:20 AM ----------

in my program grep –A $x is lasting to down lines
grep -A $x 5 numbers >>file4.txt

so I have file called number, numbers saved inside it numbers from 1 to 10
grep –A $x going to start greping from 5 to 10
but what im getting output scramble instead of
5
6
7
8
9
10
Sponsored Links
    #6  
Old Unix and Linux 01-05-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 5 December 2016, 4:51 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 Posts

Code:
#!/bin/bash

for x in {5..9}
do
   grep -A$x PATTERN FILE
done

Note: Replace PATTERN with the pattern you want to grep and FILE with the file name you want to grep.

---------- Post updated at 15:36 ---------- Previous update was at 15:29 ----------

Please note that -A option is used to print NUM lines of trailing context after matching lines.
Sponsored Links
    #7  
Old Unix and Linux 01-05-2013
hadinetcat hadinetcat is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 7 February 2015, 1:53 PM EST
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
loop

Is print many lines instead of one round of numbers from 5 to 10

Code:
5
6
7
8
9
10
5
6
7
8
9
10

5
6
7
8
9
10

5
6
7
8
9
10

5
6
7
8
9
10

Moderator's Comments:
Loop Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-05-2013 at 05:42 PM.. Reason: once again - please use code tag!
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array Variable being Assigned Values in Loop, But Gone when Loop Completes??? mrm5102 Shell Programming and Scripting 5 10-19-2012 11:00 AM
BASH loop inside a loop question rethink Shell Programming and Scripting 4 09-15-2010 08:58 AM
Null Handling in Until loop. . .loop won't stop brandono66 Shell Programming and Scripting 4 11-24-2009 04:57 PM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 05:59 PM



All times are GMT -4. The time now is 06:17 PM.