Rarst
Rarst

Reputation: 2395

How to run shell written in PHP on Windows?

I am looking at wpshell - shell, written in PHP (for PHP and WordPress).

It seems to be run by following code in bash:

#!/bin/bash

shell=$(dirname $(readlink -f $0))"/wpshell.php"

if [ -t 0 ]; then
    while [ true ]; do
        /usr/local/bin/php $shell
        if [ $? -eq 0 ]; then break; fi
    done
else
    set -f
    read -s input
    echo $input | /usr/local/bin/php $shell stdin
fi

How to adapt this for Windows environment? If at all possible.

Upvotes: 2

Views: 886

Answers (1)

Jarek
Jarek

Reputation: 1330

Lets look at this piece by piece

  • shell=$(dirname $(readlink -f $0))"/wpshell.php"

    • $0 is the script being called. $(cmd) means take the output of cmd.
    • readlink -f resolves symlinks, so $(readlink -f $0) finds the full path of the script being run, after evaluating any symlinks.
    • dirname /path/to/foo.sh returns the path to the argument (in this case /path/to). So, $(dirname $(readlink -f $0)) is the full path to the directory this script was being run from, after resolving all links.

    • shell=$(dirname $(readlink -f $0))"/wpshell.php" Takes the output of shell=$(dirname $(readlink -f $0)) and appends "/wpshell.php". So, this whole line just gets the full path to wpshell.php

  • if [ -t 0 ]; then check if STDIN is open and refers to a terminal, and enters the if block if it does

    • while [ true ]; do loops forever until we break. This is so if wpshell crashes or exits with an error, it will automatically restart.
      • /usr/local/bin/php $shell just calls php and passes it the path to wpshell that we found earlier.
      • if [ $? -eq 0 ]; then break; fi check if the last command (/usr/local/bin/php $shell) completed successfully. ?$ is the return code of the last command, and we check if that equals 0, which means no error. If there was no error, wpshell exited cleanly, so we are done, and can break out of our infinite loop and exit.
  • else this block is executed if the STDIN does not refer to a terminal

    • set -f disables glob completion, so things like * are not expanded.
    • read -s input reads a string from the user and stores it in input
    • echo $input | /usr/local/bin/php $shell stdin sends what we just read as the input to wpshell, and tells it to read from that

Most of this functionality isn't available on Windows. You would most likely be fine just calling php on wpshell.php. Create wpshell.bat, and put "C:\path\to\php" "C:\path\to\wpshell.php" in it. You can then run wpshell.bat to run wpshell.

Upvotes: 3

Related Questions