Loop | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Loop

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-05-2013
hadinetcat hadinetcat is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 1 January 2014, 2:10 PM EST
Posts: 10
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:
Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-05-2013 at 02:57 PM.. Reason: code tags, please!
Sponsored Links
    #2  
Old 01-05-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 July 2014, 9:08 PM EDT
Location: Galactic Empire
Posts: 3,354
Thanks: 230
Thanked 1,190 Times in 1,123 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 01-05-2013
hadinetcat hadinetcat is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 1 January 2014, 2:10 PM EST
Posts: 10
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 01-05-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 July 2014, 9:08 PM EDT
Location: Galactic Empire
Posts: 3,354
Thanks: 230
Thanked 1,190 Times in 1,123 Posts

Code:
#!/bin/bash

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

Sponsored Links
    #5  
Old 01-05-2013
hadinetcat hadinetcat is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 1 January 2014, 2:10 PM EST
Posts: 10
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 01-05-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 July 2014, 9:08 PM EDT
Location: Galactic Empire
Posts: 3,354
Thanks: 230
Thanked 1,190 Times in 1,123 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 01-05-2013
hadinetcat hadinetcat is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 1 January 2014, 2:10 PM EST
Posts: 10
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:
Please use code tags when posting data and code samples!

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 10:00 AM
HELP PLS - calling function in a while loop ends the loop ?? newbie_01 Shell Programming and Scripting 3 05-29-2012 04:38 PM
BASH loop inside a loop question rethink Shell Programming and Scripting 4 09-15-2010 07:58 AM
Null Handling in Until loop. . .loop won't stop brandono66 Shell Programming and Scripting 4 11-24-2009 03: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 04:59 PM



All times are GMT -4. The time now is 04:26 AM.