Results 1 to 7 of 7

Thread: My almost first script. What's wrong?

  1. #1
    Join Date
    Jun 2006
    Beans
    123
    Distro
    Kubuntu

    My almost first script. What's wrong?

    Code:
    #!/bin/bash
    
    cd $HOME
    cd Desktop
    
    for (( i = 1; i >= 3; i++ ))
    do
    
    cp 'job.temp' "job_$i.qsub"
    
    
    echo "./foo $i 5. 0. 133" > "job_$i.qsub"
    
    
    done
    Expected behaviour: 3 files that contain job.temp with an extra string at the end.
    Observed: No new files. No error message. Absolutely nothing.
    Question: What's wrong and what is the good way to do it?
    Ubuntu Linux install+configuration+software installation: 3 hoours.
    WindowsXP install+configuration+software installation: 5 hours

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: My almost first script. What's wrong?

    Code:
    #!/bin/bash
    
    cd $HOME
    cd Desktop
    
    for (( i = 1; i <= 3; i++ ))
    do
    
    cp 'job.temp' "job_$i.qsub"
    
    
    echo "./foo $i 5. 0. 133" > "job_$i.qsub"
    
    
    done

  3. #3
    Join Date
    Jan 2007
    Beans
    Hidden!

    Re: My almost first script. What's wrong?

    Code:
    for (( i = 1; i >= 3; i++ ))
    Look carefully at that.

    You're saying let i be 1, then as long as i is greater or equal to 3, increment i...

    Try changing it to:
    Code:
    for (( i = 1; i <= 3; i++ ))

  4. #4
    Join Date
    Feb 2008
    Beans
    367

    Re: My almost first script. What's wrong?

    Just as an explanation of what sisco posted. In your original script, your loop variable, i never is "greater than" 3, you were basically stating that you want the loop to execute while i >= 3, which never occurs because i is initialized as 1.

    EDIT: I also reported this to be moved to Programming talk.

  5. #5
    Join Date
    Jun 2006
    Beans
    123
    Distro
    Kubuntu

    Re: My almost first script. What's wrong?

    Thanks. It's just a typo. I expected the problem to be in misunderstanding how shell scripts work and did not check the condition.

    How do you debug shell scripts? In C I would just put a breakpoint in the middle of the loop to see what happened, what about this?
    Last edited by muxecoid; March 17th, 2009 at 08:59 AM.
    Ubuntu Linux install+configuration+software installation: 3 hoours.
    WindowsXP install+configuration+software installation: 5 hours

  6. #6
    Join Date
    Jan 2007
    Beans
    Hidden!

    Re: My almost first script. What's wrong?

    Quote Originally Posted by muxecoid View Post
    Thanks. It's just a typo. I expected the problem to be in misunderstanding how shell scripts work and did not check the condition.

    How do you debug shell scripts? In C I would just put a breakpoint in the middle of the loop to see what happened, what about this?
    Try the -x option to bash. In other words, run your script like:
    Code:
    bash -x ./script.sh
    Or change your
    Code:
    #!/bin/bash
    to
    Code:
    #!/bin/bash -x
    Or add a line like:
    Code:
    set -x
    at the beginning (just after #!/bin/bash)

  7. #7
    Join Date
    Aug 2006
    Beans
    198

    Re: My almost first script. What's wrong?

    Quote Originally Posted by muxecoid View Post
    Expected behaviour: 3 files that contain job.temp with an extra string at the end.
    I think there is another, somewhat subtler bug on this line:
    Code:
    echo "./foo $i 5. 0. 133" > "job_$i.qsub"
    The redirecting operator > means replace the contents of the target file with the output of the previous.

    To append text, use >>

    example:
    Code:
    $ echo "123" >test
    $ cat test
    123
    $ echo "456" >test
    $ cat test
    456
    $ echo "123" >>test
    $ cat test
    456
    123
    $

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •