/    Sign up×
Community /Pin to ProfileBookmark

exec and shell_exec not working with command line arguments to shell scripts

Hi all,

I am trying to run a shell script when a user enters a submit button (after validation of the form data). Below is my code

<code>
echo “<pre>”;
//exec(“sh /var/www/html/generate.sh”, $output);
exec(“sh /var/www/html/generate.sh $Path $ID $domain”, $output);
//$output=exec(dirname(__FILE__) . ‘/var/www/html/CERTDIR/certAuto4ChainNarenRnD_GenOnlyFemtoCert.sh’);
echo var_dump($output);
//echo $output;
echo “</pre>”;

</code>
But surprisingly when i type only <code> exec(“sh /var/www/html/generate.sh </code> (I have written the shell script if < 3 command line args it pops out a usage message) am able to see the output.

Output:

array(1) {
[0]=>
string(118) “Usage: /var/www/html/generate.sh <………..>”
}

Also the shell script output is very huge its almost equvalent to a pages content. around 10 paragraphs.

So how do i run a shell script which takes 3 or more command line arguments (these command line arguments are the form data which am taking from $_post from the user input) from a php script ?

Do i need to increase the size of the var_dump() ?? or is there anything am missing here.

Even with shell_exec() no much luck i can run only a script without command line args (i dont know how to run shell_exec() which reads command line arguments and outputs it to a page)

This works (but no command line args)

$output=exec(dirname(__FILE__) . ‘/var/www/html/generate.sh

Anybody knows how to accomplish this taks of running a Shell script with 3 command line arguments which outputs a page of data to the user at the browser ?

Thanks and Regards,
kan

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 30.2014 — If you want to capture the output, I would normally use shell_exec() (assigning the result to the desired result variable). I would recommend applying escapeshellcmd() to each of the arguments, just in case.
Copy linkTweet thisAlerts:
@kan303authorOct 01.2014 — I just was experimenting with escapeshellcmd()

$dir = ".";
$ou=escapeshellcmd('ls $dir');
echo $ou;


But the error_log in /var/log/httpd/ says ls command not found... Same thing happening for mkdir function of php and copy function of php. It says permission denied. Am not able to use shell commands such as ls and mkdir from php. I think its the problem in apache. Even i changed the user permission and owners to apache and did chmod 755 to all the files and also added

apache ALL=(ALL) NOPASSWD: /home/kan/

php ALL=(ALL) NOPASSWD: /bin/cp

apache ALL=(ALL) NOPASSWD: /bin/cp

php ALL=(ALL) NOPASSWD: /bin/mkdir

apache ALL=(ALL) NOPASSWD: /bin/mkdir

in the /etc/sudoers. But nothing seems to work.

Regards,

kan
Copy linkTweet thisAlerts:
@NogDogOct 01.2014 — Could it be that the apache user account does not have a usable path setting? (E.g., you might have to specify "/bin/ls" instead of just "ls"?)
Copy linkTweet thisAlerts:
@kan303authorOct 06.2014 — Even i tried /bin/ls. And gave html directory 777 access Still no luck.
×

Success!

Help @kan303 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 12.1,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @aldoushuxley,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,
)...