2

My problem is I set an array value to the name of a server as follow:

$appserver[$i]=`grep $ip[2] /etc/hosts |awk '{print $2}'`;

Later I do a test to see if the server is up or not (using a wget). If the test fails I keep track of that server being down by creating a file with that name using touch:

$touch=`touch /home/steve/data/$appserver[$i];

The file gets created successfully. Later when I go to test whether or not the file exists it never evaluates to true.

if(file_exists('/home/steve/data/$appserver[$i])) {

I have tried several different things including creating a new variable for $appserver[$i] and testing against it, which also does not work:

I am running php 5.1.6 on RHEL 5.

Here is the full code:

  $appserver[$i]=`grep $ip[2] /etc/hosts |awk '{print $2}'`;
  echo "appserver[i] = $appserver[$i]\n";
   $get=`wget $key->url 2> /dev/null`;
   if(file_exists('/home/user/Start')) {
            $color[$i]="green";
            $rm=`rm -rf /home/user/Start`;
    }
    else {
            $color[$i]="red";
            if($hostname=="xxxxx" ) {
                    **if(file_exists('/home/user/data/$appserver[$i]))**  {**
                        echo "do nothing appserver file exists\n";
                        $touch=`touch /home/steve/data/notmailed`;
                        }
                    else {
                    echo "No app file mailed alert\n";
                    $touch=`touch /home/user/data/$appserver[$i]`;
                    }
            }
    }

Thank you.

1
  • 3
    '$appserver[$i]' != "$appserver[$i]" Commented Sep 19, 2012 at 14:16

3 Answers 3

1

It should be:

if(file_exists("/home/steve/data/$appserver[$i]")) {
2
  • Thank you. I have tried a very small program that shows how this is still failing. [root@dsmpp1 steve]# ls -l data total 4 -rw-r--r-- 1 root root 0 Sep 19 11:41 test_file [root@dsmpp1 steve]# cat test_file.php #!/usr/bin/php -q <?php $i=1; $testarray=array(); $testarray[$i]="test_file"; echo "testarray $testarray[$i]\n"; if(file_exists("/home/steve/data/testarray[$i]")) { echo "file exists\n"; } else { echo "file does not exist\n"; } [root@dsmpp1 steve]# php -q test_file.php testarray test_file file does not exist [root@dsmpp1 steve]# Commented Sep 19, 2012 at 15:42
  • You have an error in the code you pasted above, you have if(file_exists("/home/steve/data/testarray[$i]")) and the correct is if(file_exists("/home/steve/data/$testarray[$i]")) Commented Sep 19, 2012 at 16:16
1
if(file_exists('/home/steve/data/$appserver[$i]')) {

' quoted strings do not interpolate variable values, so you're looking for a file whose literal name is $appserver[$i]. Use " quotes instead:

if(file_exists("/home/steve/data/$appserver[$i]")) {
3
  • Thank you however someone suggested that before and it still does not work. Commented Sep 19, 2012 at 14:30
  • Thank you Marc. I have tried using the double quotes and it still oes not pass. I have hard coded the name of the server in place of $appserver[$i] just to make sure and it does pass when hard coded in. Commented Sep 19, 2012 at 14:50
  • Thank you. I have tried a very small program that shows how this is still failing. [root@dsmpp1 steve]# ls -l data total 4 -rw-r--r-- 1 root root 0 Sep 19 11:41 test_file [root@dsmpp1 steve]# cat test_file.php #!/usr/bin/php -q <?php $i=1; $testarray=array(); $testarray[$i]="test_file"; echo "testarray $testarray[$i]\n"; if(file_exists("/home/steve/data/testarray[$i]")) { echo "file exists\n"; } else { echo "file does not exist\n"; } [root@dsmpp1 steve]# php -q test_file.php testarray test_file file does not exist [root@dsmpp1 steve]# Commented Sep 19, 2012 at 15:49
0

Try this:

$appserver[$i] = "grep $ip[2] /etc/hosts |awk '{print $2}'";

//...

if(file_exists("/home/steve/data/$appserver[$i]"))
{
    //...
}

Ensure that you use double-qoutes (") whereever you want the content of variables parsed into the declared string!

1
  • Thank you but someone else also suggested that and it still does not work. I also tried it with the '/home/steve/data'.$appserver[$i] with single quotes and does not work. Commented Sep 19, 2012 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.