4

In recall for nostalgic feelings, I've remembered the .bat files on DOS. :)

Here's the thing: Each time I need to generate a documentation for a given web application I have to:

sudo phpdoc -o HTML:frames:earthli -d public_html/yoursite.dev/application/ -t public_html/yoursite.dev/docs/

is there a way to make a shortcut to the above, so that, we can type only:

sudo generateDoc.sh -a yoursite.dev

or even better:

sudo generateDoc.sh -theVariableThatNeedsTobeChangedOnBashHere

So that, if we have yoursite.dev passed on the command line, the bash will have yoursite.dev, if we write yoursite2.dev then, the bash would have yoursite2.dev and so on...

Any clue?

Thanks a lot.

0

4 Answers 4

4

Try this:

#!/bin/bash
phpdoc -o HTML:frames:earthli -d public_html/"$1"/application/ \
    -t public_html/"$1"/docs/

Call it as sudo ./script.sh yoursite.

2
  • I see that $1 means first argument. Why do we need the ./ ?
    – MEM
    Apr 22, 2011 at 17:21
  • 1
    @MEM its so bash knows that the script your trying to run is in this directory for a little explanation look here linfo.org/dot_slash.html
    – myusuf3
    Apr 22, 2011 at 17:34
2

You can also place this function in your .bashrc:

generateDocs() {
    sudo phpdoc -o HTML:frames:earthli \
                -d "public_html/$1/application/" \
                -t "public_html/$1/docs/"
}

then

generateDocs yoursite.dev
1

Here's an example script I wrote. The $* means "all the arguments passed". Importantly, they're in quotes because otherwise white space would break the command. Quodlibet is a sweet music library/player, btw.

jake@daedalus:~$ cat bin/play
#!/bin/bash
quodlibet --query "$*"
quodlibet --next
jake@daedalus:~$ play garrison keillor rhubarb pie

Like Fido said, don't forget to set the file as executable with chmod +x programName.

Something you might consider is making a bin directory in your home folder and adding it to your $PATH. Then you can call the command no matter what folder you're in. I have this line in my .bashrc: PATH=$HOME/bin:$PATH. If you don't do this, you have use an absolute or relative path when you call your script, ie:

jake@daedalus:/usr/local/share/docs$ /home/jake/bin/play foo fighters everlong
jake@daedalus:~/bin$ ./play placebo where is my mind

I highly recommend Grymoire's guide to SH scripting. What you're needing to do is use the shell's Positional Parameters to take arguments and insert them into your command. There's also TLDP's more in depth treatment of BASH's Positional Parameters. I'd start with Grymoire's, and then read through TLDP when you're ready for a more advanced treatment of the subject.

7
  • Actually, you should use "$@", not "$*". With "$*", all arguments will be returned as a single string. With "$@", all arguments will be returned as they were originally given. Apr 22, 2011 at 17:03
  • @mssever Good distinction to make, but at least in my case, I do want the argument returned as a single string. Grymoire points out the difference.
    – djeikyb
    Apr 22, 2011 at 17:26
  • @djeikyb: Since you know the difference between these two variables, I'm curious why you prefer "$*" in your case. The reason is that in all the shell scripting I've done, I've never encountered a situation where I've wanted "$*". I always use "$@" so that I'm protected no matter what the arguments are. What's a situation where "$*" is better? Apr 23, 2011 at 0:30
  • @mssever $* treats play adele rumour has it like one string: "adele rumour has it". $@ treats it as multiple arguments: "adele", "rumour", "has", "it". If I were to use $@, quodlibet will select all "adele" songs, then anything with "rumour" in the title, then anything with "has", etc.. when what I want is to select "adele rumour has it" all at once.
    – djeikyb
    Apr 23, 2011 at 4:25
  • I would prefer to call play "adele rumour has it" and use "$@" in the script.
    – enzotib
    Apr 23, 2011 at 7:22
0

Open your favorite text editor, paste the following:

args=("$@")
sudo phpdoc -o HTML:frames:earthli -d public_html/${args[0]}/application/ -t public_html/${args[0]}/docs/

and save it as (in this case) 'generateDoc.sh'. The next thing to do is to give execution rights to your file, so open a terminal, and run: $ chmod +x generateDoc.sh
Now all you need to do is call the file: $ ./generateDoc.sh yoursite.dev there's no need for sudo here, because it's already inside the sh file.

Hope this helps!

5
  • Thanks a lot Fido, I've thought about what I wanted to achieve and I've changed my question accordingly, if you happen to know, do you care to update your question as well?
    – MEM
    Apr 22, 2011 at 16:44
  • Why, in order to run it, we need to do ./ ?
    – MEM
    Apr 22, 2011 at 17:25
  • I've done chmod +x generateDodc.sh but I still need sudo :( Why?
    – MEM
    Apr 22, 2011 at 17:34
  • 1
    You need the ./ beacuse that's the syntax to call a sh file, you can also run it with sh or bash.The fact that the file is executable doesn't mean that you don't need special privileges to run some things. (Although I'm not sure if special rights are needed to run phpdoc) Apr 22, 2011 at 17:42
  • You need to quote your ${args[0]}
    – enzotib
    Apr 23, 2011 at 7:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .