简体   繁体   中英

Create a command-line in Ubuntu

In Ubuntu, I'd like to create a command-line called pycharm_help which will open firefox or another browser with the website https://docs.python.org/2.7/py-modindex.html . I know for doing this, I need to create a script with #!/usr/bin at the beginning. As I'm starting doing programming, I'd like that someone could help me to create this script in python. Could anyone be able to tell me how to do it? And help me create this little program?

Thanks in advance!

There is standard module webbrowser to open page in default browser

#!/usr/bin/env python

import webbrowser

webbrowser.open("https://docs.python.org/2.7/py-modindex.html")

If you have to open in firefox then maybe you will have to use

#!/usr/bin/env python

import webbrowser

browser = webbrowser.get('firefox')

browser.open("https://docs.python.org/2.7/py-modindex.html")

BTW: Ubuntu will treat script as command-line command only if

  • it has in first line #! with program which it has to use to execute this script
    (so called "shebang" or "hashbang" - # = she/hash, ! = bang)
    ie. #!/usr/bin/env python or #!/usr/bin/python
    ( #!/usr/bin/perl , #!/usr/bin/php , etc.)
  • it has "eXecution" privilage:

     chmod +x script.py 

If you want to use a python script , you can follow the answer of @furas at comments. But you can do it even in pure command line / bash script like this:

#!/bin/bash
xdg-open "https://docs.python.org/2.7/py-modindex.html" &

xdg-open calls the default web browser in your system.

Save the file (ie charmhelp) under /usr/bin/ directory to be accesible from everywhere, then make it executable using chmod +x /usr/bin/charmhelp and you can run it when you need it as charmhelp

PS: If you save the file in other directory and you want to run it (ie /name/home) you need to call it either by full path like /name/home/charmhelp or if you are already in name/home you have to run it as ./charmhelp (mind the dot in the beginning).

You could also use links (terminal web browser) directly from terminal like

links -dump "https://docs.python.org/2.7/py-modindex.html" |less

With links the web page will be displayed in terminal.

Much more simpler make an alias:

alias charmhelp='xdg-open https://docs.python.org/2.7/py-modindex.html &'

runit by charmhelp . To make the alias permanent you have to put it in name/home/.bashrc file .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM