Caniko
Caniko

Reputation: 872

How to exit shell mode in system()

I have a php script which should use some preset system aliases. I.e. "alias ll=ls -l"

In terminal "ll" works but from php system("ll") outputs

sh: ll: command not found

How do I exit "sh" and execute my terminal commands?

P.S.: May be I missunderstood the basic linux components shell and bash. In this case, please correct me/the post

Upvotes: 1

Views: 277

Answers (1)

kojiro
kojiro

Reputation: 77127

The PHP docs aren't clear about this, but presumably PHP's system is a reflection of Standard C's system(3), which hands the argument command to the command interpreter sh(1). If you want to avoid the shell, you'll need to use another feature of PHP besides system (like explicit fork/exec). That's context, but it won't help you solve your problem.

In your case it seems you just want the aliases in an rcfile. Scripts invoked by system calls aren't going to read your rcfile unless you take extraordinary steps to make that happen, and even if you do, it's going to be non-obvious to the maintenance programmer. I'd strongly advise you to put the alias in the script or command argument itself, or simply call the command explicitly (ls -al).

You can also manually source the rcfile from your script, or call system(csh -i 'yourcommands') to force csh to be invoked as an interactive shell (which should cause your rcfile to be read). I think this is a bad idea because it is effectively forcing the shell to behave inconsistently with its environment, but I'm including it here for completeness.

Most of the above I got from a quick read through the Startup and shutdown section of the csh manual on my Mac (Mavericks). There are other potential solutions there that I haven't laid out, as well.

Upvotes: 1

Related Questions