Learning curve to understand bash iteration


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Learning curve to understand bash iteration
# 1  
Old 03-22-2012
Learning curve to understand bash iteration

Hi Folks,
I know on one side there is script and on another side there is smart script. I am able to achieve what I got but thought there has to be a code which doesn't require multiple lines. So if you guys can help me out than it will be awesome. Also I wrote in cshell but if can get both the cshell and bash version (since I am new to bash) than I will achieve complete success. So task is building up numbers in each iteration, that is first iteration it should look for any file having 1 in it , second it should look for any file having 1,2 and third iteration look for 1,2,3 and so on..here is the code I wrote (not a smart one) which does this:
Code:
@ i = 1
while ($i <= 2)
if ($i == 1) then
ls -1d *find*[1]*run*lst
endif
if ($i == 2) then
ls -1d *find*[1,2]*run*lst
endif
@ i++
end

Please help me in this. Once again I can get both csh and bash version that will help in my learning curve to understand bash.
Thank you so much in advance.
# 2  
Old 03-22-2012
Being less familiar with C-shell than other shells, I'm not quite sure I understand what you're doing. So your first loop would look for
Code:
filename1.txt

and your second loop would look for
Code:
filename1,2.txt

and your third loop would look for
Code:
filename1,2,3.txt

are those commas supposed to be in the filename? Or are you expanding the range of filenames you're looking for?
# 3  
Old 03-22-2012
I'm not sure if the kind of meta-substitution you want is possible in the C-shell.

This will work in any bourne shell, including bash, dash, ksh, zsh, ash, and probably others I haven't heard of.
Code:
#!/bin/sh

I=1

while [ "$I" -lt 3 ]
do
        RANGE="[1-$I]"

        echo
        echo "Files in range $RANGE"
        echo *${RANGE}* # Will expand to *[0-1]*, etc
        I=`expr $I + 1`
done

# 4  
Old 03-22-2012
sorry for utter confusion. Comas are not in filename, I guess in cshell when you have to look for file filename having 1 or 2 in txt file you can use [1,2] example
Code:
ls -1 filename[1,2].txt
 
output
filename1.txt
fielname2.txt
 
ls -1 fielname[1,2,3].txt
 
o/p
filename1.txt
fielname2.txt
fielname3.txt

Thanks

Last edited by Corona688; 03-22-2012 at 12:36 PM..
# 5  
Old 03-22-2012
My code ought to do what you want, then.

With a little testing I find you can do nearly the same idea in csh, too. Put the expression you want in a variable, and keep changing it.

Code:
#!/bin/sh
# Bourne shell version

I=0

while [ "$I" -le 2 ]
do
        RANGE="[0-$I]"

        echo
        echo "Files in range $RANGE"
        echo *${RANGE}* # Will expand to *[0-1]*, etc

        I=`expr $I + 1`
done

Code:
#!/bin/csh
# csh version

@ I = 1
while ($I <= 2)
        set VAR="[0-$I]"

        echo
        echo "Files in range $RANGE"
        echo *${RANGE}*

        @ i++
end

Incidentally -- neither of these program uses arrays, in either language.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to understand special character for line reading in bash shell?

I am still learning shell scripting. Recently I see a function for read configuration. But some of special character make me confused. I checked online to find answer. It was not successful. I post the code here to consult with expert or guru to get better understanding on these special characters... (3 Replies)
Discussion started by: duke0001
3 Replies

2. Shell Programming and Scripting

I am learning shell scripting and i would like to understand how sort -k3,3 -k 4,4 short* works

Please help me understand how sort -k3,3 -k 4,4 short* works , is it sorting 3 and 4 th field ? what is the use of specifying 3,3 and 4,4 in sort and what is actually does and i see sort -k3 short* and sort -k3,3 -k 4,4 short* giving the same output. Sort output attached Please help (2 Replies)
Discussion started by: Antony Ankrose
2 Replies

3. OS X (Apple)

Plotting A Sine Curve Inside A Bash Shell...

Hi all... After mentioning the generation of a sinewave sweep generator in a previous thread in this forum this is the method I decided upon. It plots a sinewave inside an 80 x 24 terminal window. Although the original used bc (and the line is in the code but commented out) it is now... (4 Replies)
Discussion started by: wisecracker
4 Replies

4. Shell Programming and Scripting

I am learning regular expression in sed,Please help me understand the use curly bracket in sed,

I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement, Question : 1 __________ /opt/oracle/work/antony>cat teledir.txt jai sharma 25853670 chanchal singhvi 9831545629 anil aggarwal 9830263298 shyam saksena 23217847 lalit... (7 Replies)
Discussion started by: Antony Ankrose
7 Replies

5. UNIX for Dummies Questions & Answers

Another Simple BASH command I don't understand. Help?

I have a text file called file1 which contains the text: "ls -l" When I enter this command: bash < file1 > file1 file1 gets erased. However if I enter this command: bash < file1 > newfile the output from "ls -l" is stored in newfile. My question is why doesn't file1's text ("ls -l") get... (3 Replies)
Discussion started by: phunkypants
3 Replies

6. Shell Programming and Scripting

Do not understand why my while loop fail (bash)

Hi again :) I still need your help now... #!/bin/bash SIZE=`ls -s text.txt` while do done My problem is that the line "while ..." still print the same value after the first loop. In one instruction change the size of text.txt I've run my bash script in debug mode, and the... (6 Replies)
Discussion started by: Link_
6 Replies

7. Shell Programming and Scripting

I want to understand bash better

Hi everyone, I have some questions regarding bash and my initializer files: when I look at my bashrc file, I see lots of "export".. export MAGICK_HOME="$HOME/bin..etc".. what does export really mean? in my bashrc, I have a line: "umask 0002" ... what does that do? I have a bash_profile... (6 Replies)
Discussion started by: patrick99e99
6 Replies

8. Shell Programming and Scripting

Understand Bash scripting encryption

Dear all, I have been working with Linux for quite few years but never required to learn Bash and Linux really dep doan. I want to learn bash I bought books and Have been reading on the Internet but I cannot find my answers, maybe I am blind. For example: What does this means? # for i in... (4 Replies)
Discussion started by: renpippa
4 Replies

9. Shell Programming and Scripting

bash shell: 'exec', 'eval', 'source' - looking for help to understand

Hi, experts. Whould anybody clear explay me difference and usage of these 3 commands (particulary in bash) : exec eval source I've tryed to read the manual pages but did not get much. Also could not get something useful from Google search - just so much and so not exactly, that is... (3 Replies)
Discussion started by: alex_5161
3 Replies

10. UNIX for Dummies Questions & Answers

learning curve's too steep

I tried PC-BSD a year or 2 ago, but the command-stuff wasn't clear to me, not knowing anything about UNIX. I decided to go from scratch to learn more and installed FreeBSD7. Now I find this is taking too much time (which I don't have; full time job + playing music + being over 50) to learn... (5 Replies)
Discussion started by: catch22
5 Replies
Login or Register to Ask a Question