Find the answer to your Linux question:
Results 1 to 5 of 5
Hello everyone, The following code: Code: #!/bin/bash x=1 echo $x while [ $x -eq "1" ] do echo $x x=2 done echo "reached the end" outputs "1" from line 3, ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    3

    well now this is embarrasing - simple bash script doesn't work

    Hello everyone,

    The following code:

    Code:
    #!/bin/bash
    x=1
    echo $x
    while [ $x -eq "1" ]
    do
      echo $x
      x=2
    done
    echo "reached the end"
    outputs "1" from line 3, but nothing else.

    I don't have access to a shell on the server and I'm calling it from php using

    Code:
    echo passthru('/bin/bash -x ./process.sh');
    I'd say it's a problem with how the script is being called, but the script obviously starts and does not continue so I'm stumped!

    Paul

  2. #2
    Just Joined!
    Join Date
    Jun 2010
    Posts
    4
    I get this output.
    Code:
    ./foo.sh 
    1
    1
    reached the end
    what exactly is it you want to do?

  3. #3
    Just Joined!
    Join Date
    Jun 2010
    Posts
    3
    Well that's not the script I want to use of course I've tried to narrow down to find where the problem lies. What I'm doing is sending out a lot of emails (not spam!); once a day a cron job calls php which sets up a table of emails needing to be sent and then calls a bash script to initiate sending. In order to not send them out too quickly (and get blocked for spam) it sends out 4 per second. In order for php to not timeout, it is called repeatedly from inside the bash loop.

    So the bash scripting I need is very simple, but seems to not be working on my server, or when being called from php. Even the following doesn't work (it outputs a but not a+1).

    Code:
    #!/bin/bash
    a=1
    echo $a
    echo $((a+1))
    Any idea why?

  4. #4
    Just Joined!
    Join Date
    Jun 2010
    Posts
    4
    Okey, the problem is that you are using passthru() its ment to be used if the bash data is binary and are to be passed directly to the browser.

    Use system() or exec() and catch the output in stead.

    Hope this helps.

  5. #5
    Just Joined!
    Join Date
    Jun 2010
    Posts
    3
    Thanks for the suggestion. I tried with exec first, and then was trying passthru and shell_exec to capture the output when exec wasn't working.

    I've just realised I don't need to exec from php at all, but call bash straight from crontab and do the initial setup from there. Here's the actual script....

    Code:
    #!/bin/bash
    php setup_alerts.php
    code=2
    while [ $code -eq 2 ]
    do
    php send_alerts.php
    code=$?
    done
    Still not working... I even tested with "echo hello > file" in the first line, but no file was produced. I guess it's time to get on to the server admin.

    *****UPDATE: Turns out I was using a DOS file with CR+LFs. There goes most of a day.

Posting Permissions

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