Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
I see that $1 means first argument. Why do we need the ./ ? –  MEM Apr 22 '11 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 '11 at 17:34
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
    
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. –  Scott Severance Apr 22 '11 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 '11 at 17:26
    
djeikyb: Thanks for the references. ;) –  MEM Apr 22 '11 at 18:20
    
@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? –  Scott Severance Apr 23 '11 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 '11 at 4:25
show 3 more comments

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!

share|improve this answer
    
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 '11 at 16:44
    
Why, in order to run it, we need to do ./ ? –  MEM Apr 22 '11 at 17:25
    
I've done chmod +x generateDodc.sh but I still need sudo :( Why? –  MEM Apr 22 '11 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) –  Fido Apr 22 '11 at 17:42
    
You need to quote your ${args[0]} –  enzotib Apr 23 '11 at 7:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.