1

I have a small foreach loop in php but the results will not pass to a shell script. see below:

$contents = file("$target_dir/$user.temp.txt");

foreach($contents as $line) {
    echo $line . "<br />";
    exec("sh read.sh $line >> tempfile");
}

the echo statement works just fine and displays the data to screen as it should. however the result of $line does not make it to the shell script, but when I replace $line with a random string it does. here is the shell script:

#!/bin/bash
#test script
echo "test output: $1"

when trying to call the shell script with $line in place, the tempfile will get created but is blank. all my permissions are set to 777 and the group calling the script is the same owner as the folder. I've reviewed other posts about php loops but dont seem to find anything that matches exactly what my issue is.

Thanks in advance for any insight.

6
  • why don't you write directly into the file ? Commented Jun 15, 2015 at 20:22
  • 1
    so what's in $line? Commented Jun 15, 2015 at 20:22
  • $line contains one line of $user.temp.txt. my goal is to take one line of the $user.temp.txt file, put it into a shell script, then move onto the next line in $user.temp.txt. this is to log into devices and run commands on them if i can get the loop working. Commented Jun 15, 2015 at 20:27
  • 2
    Try quoting line as it may have spaces. exec("sh read.sh '$line' >> tempfile"); Commented Jun 15, 2015 at 20:34
  • That got it! i didn't think to use single quotes since it usually is a literal interpret, but this is also the first time 'ive tried something like this. thanks so much for the help. Commented Jun 15, 2015 at 20:41

1 Answer 1

1

Needed to add single quotes to $line.

exec("sh read.sh '$line' >> tempfile");
Sign up to request clarification or add additional context in comments.

Comments

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.