[PHP] How to write file containing shell script?

2002-03-27 Thread Dalton Hunter

Hi, I am trying to write a file that contains a shell script. The file
writes to the server but when I try to execute it it fails with a command
not found error. But if I copy and paste the files contents into a new text
file using Textpad on my PC and upload it to the same directory it works?!
It is not related to permissions as both files are 777 and I am root? The
only difference I could tell is when I compare the files in Textpad although
the contents are identical, the failed file is slightly larger and has a
file type of Netscape in the File Properties while the other has a file
type of PC? Any ideas?



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.343 / Virus Database: 190 - Release Date: 3/22/02



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How to write file containing shell script?

2002-03-27 Thread Miguel Cruz

On Wed, 27 Mar 2002, Dalton Hunter wrote:
 Hi, I am trying to write a file that contains a shell script. The file
 writes to the server but when I try to execute it it fails with a
 command not found error. But if I copy and paste the files contents into
 a new text file using Textpad on my PC and upload it to the same
 directory it works?! It is not related to permissions as both files are
 777 and I am root? The only difference I could tell is when I compare
 the files in Textpad although the contents are identical, the failed
 file is slightly larger and has a file type of Netscape in the File
 Properties while the other has a file type of PC? Any ideas?

Make sure you use unix-style line endings (just \n and no \r).

Textpad sort of transparently opens files with different line ending 
styles, though it does tell you in one of those little status boxes at the 
bottom - if it's showing DOS or Windows or whatever when you open it, then 
the shell probably won't execute it.

An alternative would be to use text/ASCII (not binary) FTP to transfer it 
to the server. This will automatically fix the line endings.

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How to write file containing shell script?

2002-03-27 Thread Dalton Hunter

Miguel Cruz [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 On Wed, 27 Mar 2002, Dalton Hunter wrote:
  Hi, I am trying to write a file that contains a shell script. The file
  writes to the server but when I try to execute it it fails with a
  command not found error. But if I copy and paste the files contents into
  a new text file using Textpad on my PC and upload it to the same
  directory it works?! It is not related to permissions as both files are
  777 and I am root? The only difference I could tell is when I compare
  the files in Textpad although the contents are identical, the failed
  file is slightly larger and has a file type of Netscape in the File
  Properties while the other has a file type of PC? Any ideas?

 Make sure you use unix-style line endings (just \n and no \r).

 Textpad sort of transparently opens files with different line ending
 styles, though it does tell you in one of those little status boxes at the
 bottom - if it's showing DOS or Windows or whatever when you open it, then
 the shell probably won't execute it.

 An alternative would be to use text/ASCII (not binary) FTP to transfer it
 to the server. This will automatically fix the line endings.

 miguel


Hi, thanks for your reply. I should have been a little clearer. The file is
created as a result of a PHP script not in TextPad. I am only comparing them
in Textpad afterward. For example the script contains ...

?php

// BEGIN PHP SCRIPT THAT GENERATES SHELL SCRIPT

$sbackup = #!/bin/sh
... multiple line shell script in here ...
;

$backupfile = fopen(/path/to/shell_script,w);
fwrite($backupfile,$sbackup);
fclose($backupfile);
chmod(/path/to/shell_script, 0777);

// END PHP SCRIPT THAT GENERATES SHELL SCRIPT

?

... and it creates the file fine but when I go to execute the shell script
it returns ...

sh: ./shell_script: No such file or directory

eventhough it is there. If I copy the files contents into a new Texpad
document on my PC and upload to the server it works fine. I tried to add a
\n at the end of each line in the PHP script but no luck. I think it might
have something to do with the line endings.


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.343 / Virus Database: 190 - Release Date: 3/22/02



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How to write file containing shell script?

2002-03-27 Thread Miguel Cruz

On Wed, 27 Mar 2002, Dalton Hunter wrote:
 Miguel Cruz [EMAIL PROTECTED] wrote:
 Make sure you use unix-style line endings (just \n and no \r).
 
 Hi, thanks for your reply. I should have been a little clearer. The file is
 created as a result of a PHP script not in TextPad. I am only comparing them
 in Textpad afterward. For example the script contains ...
 
 ?php
 
 // BEGIN PHP SCRIPT THAT GENERATES SHELL SCRIPT
 
 $sbackup = #!/bin/sh
 ... multiple line shell script in here ...
 ;

 ... and it creates the file fine but when I go to execute the shell script
 it returns ...
 
 sh: ./shell_script: No such file or directory

Right. I think the No such file or directory message is because it's 
failing to find an interpreter called /bin/bash\r.

Assuming you're creating the PHP file in Textpad, save it with Unix line
endings rather than Windows line endings. This is a Textpad option (I'm
sitting at the OSX machine so I can't check exactly where it is at the
moment, but trust me, I've used it earlier today).

I think that when your string literal breaks onto the next line, PHP is 
obediently including your Windows-style line endings in $sbackup and thus 
the shell_script file.

 I tried to add a \n at the end of each line in the PHP script but no
 luck.

Windows line ending = \r\n
Unix line ending = \n

So adding a \n won't help. You need to get rid of the invisible \r, using 
the method described above.

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How to write file containing shell script?

2002-03-27 Thread Jason Wong

On Thursday 28 March 2002 15:03, Dalton Hunter wrote:

 them in Textpad afterward. For example the script contains ...

 ?php

 // BEGIN PHP SCRIPT THAT GENERATES SHELL SCRIPT

 $sbackup = #!/bin/sh

Do you actually terminate each line of your shell script with \n?

ie shouldn't that be:

$sbackup = #!/bin/sh\n ... 

??


 sh: ./shell_script: No such file or directory

Have you tried

  cat shell_script

to see whether it 'looks' ok?


-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk

/*
Of course a platonic relationship is possible -- but only between
husband and wife.
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How to write file containing shell script?

2002-03-27 Thread Miguel Cruz

On Thu, 28 Mar 2002, Miguel Cruz wrote:
 Right. I think the No such file or directory message is because it's 
 failing to find an interpreter called /bin/bash\r.

Sorry, I mean /bin/sh\r. Habit.

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How to write file containing shell script?

2002-03-27 Thread Dalton Hunter

Miguel Cruz [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 On Thu, 28 Mar 2002, Miguel Cruz wrote:
  Right. I think the No such file or directory message is because it's
  failing to find an interpreter called /bin/bash\r.

 Sorry, I mean /bin/sh\r. Habit.

 miguel


Thanks miguel, that was the problem. The original PHP file was indeed being
saved in TextPad as PC thus adding that \r. I resaved it as Unix and the
shell script runs fine. :)


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.343 / Virus Database: 190 - Release Date: 3/22/02



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php