?
Solved

Passing arguments to a shell script in PHP

Posted on 2009-07-11
10
Medium Priority
?
3,396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
LVL 4

Accepted Solution

by:
khyer123 earned 2000 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …
Suggested Courses

762 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