Passing arguments to a shell script in PHP

I'm trying to follow this tutorial on creating a shell script in PHP. http://www.phpbuilder.com/columns/darrell20000319.php3?page=2
I ran into a problem on Page 2. The tutorial says: "So in the above script we're printing out the first two arguments to the script which would be called like this:
[dbrogdon@artemis dbrogdon]$ scriptname.ph Darrell Brogdon"

But it doesn't tell me anything about the file from which it is called! I need to know
1) what type of a file it should be, and 2) what the entire contents of that file should be. (i.e., what else is needed other then "[dbrogdon@artemis dbrogdon]$ scriptname.ph Darrell Brogdon"

Thanks,

John
Commonly with a shell script you need to pass arguments to the script. This is easily done using the built-in '$argv' array as show in the following example:
#!/usr/local/bin/php -q
<?php
    $first_name = $argv[1];
    $last_name = $argv[2];
 
    print("Hello, $first_name $last_name!  How are you today?\n");
 
?>
So in the above script we're printing out the first two arguments to the script which would be called like this:
[dbrogdon@artemis dbrogdon]$ scriptname.ph Darrell Brogdon
Which would print out:
Hello, Darrell Brogdon!  How are you today?
[dbrogdon@artemis dbrogdon]$

Open in new window

LVL 1
John CarneyReliability Business Tools Analyst IIAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

khyer123Commented:
I'm not sure I understand your question...

How this work is... when you run the php script (usually from some console), you can feed it in arguments. The example you have will assume that you will take in two strings. When you pass in values, they always come in as whatever the default data type is (string, int, etc... php is dynamic in that sense). So, if you run your program like this:  php script.php bob barker      Then when the script runs, $argv[1] will have the value of "bob" and $argv[2] will have the value of 'barker'. That's all you need.
0
khyer123Commented:
Oh, i see what might be going on as well.

Make sure that scriptname.ph is executable (chmod +x scriptname.ph).

Otherwise, just run this: php scriptname.ph Darrell Brogdon

If you don't chmod it to be executable, it's just a text file.
0
John CarneyReliability Business Tools Analyst IIAuthor Commented:
Hi khyer123, I'm afraid I don't understand. What do you mean by "console"? Console means a piece of hardware to me. What exactly is it here?  And what is scriptname.ph? and how and where do I make it executable?  The phrase you put in parentheses (chmod +x scriptname.ph)

Could you please give me an example of a file that would pass the arguments to my script in the snippet above. I have that script in a file named "aaShell.php"   (If that's an unacceptable name, please let me know what the name should be).

So what should the name and type of my other file be? The one that contains the line: "php scriptname.ph Darrell Brogdon"

Or does that line go in my "aaShell.php"? I'm afraid I need to have it spelled out in great detail. Please accept my apologies for my stupidity at this point.

Thanks,
John
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

khyer123Commented:
Oh, this is the Windows PHP section. Are you doing this in windows?

By console, I meant the command line (in linux anyway). In what environment are you running this? You need a command line to pass arguments in.
0
John CarneyReliability Business Tools Analyst IIAuthor Commented:
I have a site on a Windows server to which I need to add some password protection to several of the pages. My immediate goal is to learn enough php to accomplish that goal as soon as possible. However I've also copied some of the files to my Apache Linux server, so please educate me in either or both.

<<< By console, I meant the command line (in linux anyway) >>> So where do I find the "command line" on my Linux server?  I assume that must be some kind of file residing on my server?

Thanks,
John
0
khyer123Commented:
Is the linux server something you have desktop access to (ie you have the box in front of you) or is this something that is hosted? If it's hosted, you would need shell/SSH access to it to get to the command line. If you're sitting at the machine, and you're running Gnome or KDE, you need Console (or Konsole for KDE) which will bring up the command prompt.

In either case, you need to do this:

php /path/to/file/aaShell.php Argument1 Argument2

And that has to be done from a command line
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John CarneyReliability Business Tools Analyst IIAuthor Commented:
It's hosted. I'll call them to see if they can tell me how to get access to shell/SSH.
0
John CarneyReliability Business Tools Analyst IIAuthor Commented:
It's going to take 12-24 hours for my shell/SSH to be active. I'll get back here as soon as it's activated.

Thanks,
John
0
khyer123Commented:
Alright, so as soon as it's active, you need to use some ssh client (putty works fine).

Navigate to where you have your php file, and run the commands I talked about above to see what happens.
0
John CarneyReliability Business Tools Analyst IIAuthor Commented:
Thanks, I'm starting to get it. Sorry I took so long to get back to you.

- John
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.