Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hey guys I am just trying a simple script to run a statement in shell.. what is wrong with this code ? Code: #!/bin/sh $count = 2 echo $1 if ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    6

    N00b help for scripting..

    Hey guys

    I am just trying a simple script to run a statement in shell.. what is wrong with this code ?

    Code:
    #!/bin/sh
    $count = 2
    echo $1
    if [ $count <= $1 ]; then
            runscript $count >> scriptout
            $count++
    fi
    the runscript is basically a script in the directory.. and i want the output to always be appended into a file called scriptout..

    please help

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Hi,

    You have some issues here.
    It looks like you came from a Perl world.
    1) you do not need the dollar sign when you declare count
    2) you have to remove the spaces around the equal sign when you assign
    3) you have to replace the less or equal sign as follows
    4) to increment count you have to put it in double parenthesis.

    After correction it would look like this
    Code:
    #!/bin/sh
    count=2
    echo $1
    if [ $count -le $1 ]; then
            runscript $count >> scriptout
            ((count++))
    fi
    Linux and me it's a love story

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    6
    Quote Originally Posted by khafa View Post
    Hi,

    You have some issues here.
    It looks like you came from a Perl world.
    1) you do not need the dollar sign when you declare count
    2) you have to remove the spaces around the equal sign when you assign
    3) you have to replace the less or equal sign as follows
    4) to increment count you have to put it in double parenthesis.

    After correction it would look like this
    Code:
    #!/bin/sh
    count=2
    echo $1
    if [ $count -le $1 ]; then
            runscript $count >> scriptout
            ((count++))
    fi

    Hey Thanks..

    so something wrong with this also then

    Code:
    #!/bin/sh
    echo 'Please Enter Number of Days:'
    read $numb
    
    echo 'Please Enter Email address:'
    read $email
    
    count=2
    
    if [ $count -le $numb ]; then
            script $count $email >> scriptout
            (($count++))
    fi

  4. #4
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Yes. A dollar sign problem.
    below would work
    Code:
    #!/bin/sh
    echo 'Please Enter Number of Days:'
    read numb
    
    echo 'Please Enter Email address:'
    read email
    
    count=2
    
    if [ $count -le $numb ]; then
            script $count $email >> scriptout
            ((count++))
    fi
    Linux and me it's a love story

  5. #5
    Just Joined!
    Join Date
    Sep 2007
    Posts
    6
    Quote Originally Posted by khafa View Post
    Yes. A dollar sign problem.
    below would work
    Code:
    #!/bin/sh
    echo 'Please Enter Number of Days:'
    read numb
    
    echo 'Please Enter Email address:'
    read email
    
    count=2
    
    if [ $count -le $numb ]; then
            script $count $email >> scriptout
            ((count++))
    fi

    thanks it runs

    but always comes with an error

    I had given input as 3 and an email address..

    Code:
    ./myscript: 2++: not found
    I m guessing its reaching end of loop error or something ??

  6. #6
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Did you replace
    Code:
    script $count $email >> scriptout
    by
    Code:
    ./myscript $count $email >> scriptout
    ?

    If that is the case then on the command line can you run and see what happens?
    Code:
    ./myscript $count $email >> scriptout
    Linux and me it's a love story

  7. #7
    Just Joined!
    Join Date
    Sep 2007
    Posts
    6
    Quote Originally Posted by khafa View Post
    Did you replace
    Code:
    script $count $email >> scriptout
    by
    Code:
    ./myscript $count $email >> scriptout
    ?

    If that is the case then on the command line can you run and see what happens?
    Code:
    ./myscript $count $email >> scriptout

    nono

    thats the output error i get

    nothing is changed the script is the same given on top..

    just that i relized it should be a loop so i changed

    if [ ]; then
    fi

    TO

    while [];
    do

    done

    rest all is same..

    the error i get is at the end of each loop

    /myscript: 2++: not found

    myscript is the name of the above script

    the script is

    Code:
    #!/bin/sh
    echo 'Please Enter Number of Days:'
    read numb
    echo 'Please Enter Email address:'
    read email
    
    count=2
    
    while [ $count -le $numb ]; 
    do
            echo "DAY $count" >> scriptout
            mailscript $count $email >> scriptout
            
            (($count++))
    done
    also the count++ is not increasing.. so the echo output is always DAY 2 and looping into that..

  8. #8
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Can you post the full new script(after you changed if-then to while-do)?
    When I saw your script I thought that you wanted to give it as an example and replace
    Code:
    script $count $email >> output
    with
    Code:
    your_own_script $count $email >> output
    because script is a program that already exists in Linux. You can check it as follows
    Code:
    man script
    Linux and me it's a love story

  9. #9
    Just Joined!
    Join Date
    Sep 2007
    Posts
    6
    I have posted the full script above

    i think the problem is that it is not doing (($count++))

    so it spits out error 2++ as 2 is the initial value

    so therefore loop is infinite as count is not moving ahead..

  10. #10
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Sorry I did not see the full script.

    Yes the problem is
    Code:
    (($count++))
    Replace it with
    Code:
    ((count++))
    .
    If you look closely I did not use the dollar sign when incrementing in my above posts
    Linux and me it's a love story

Page 1 of 2 1 2 LastLast

Posting Permissions

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