Solved

Passing arguments to a shell script in PHP

Posted on 2009-07-11
10
3,321 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:gabrielPennyback
  • 5
  • 5
10 Comments
 
LVL 4

Expert Comment

by:khyer123
ID: 24832544
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
 
LVL 4

Expert Comment

by:khyer123
ID: 24832549
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
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 24832593
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
 
LVL 4

Expert Comment

by:khyer123
ID: 24832615
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
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 24832639
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Accepted Solution

by:
khyer123 earned 500 total points
ID: 24832660
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
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 24832758
It's hosted. I'll call them to see if they can tell me how to get access to shell/SSH.
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 24832795
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
 
LVL 4

Expert Comment

by:khyer123
ID: 24832806
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
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 31602487
Thanks, I'm starting to get it. Sorry I took so long to get back to you.

- John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now