Everyone,
I'm creating a command line script based on Python 3.5 on Ubuntu 16.04.
This script accepts some options that I'm treating with argparse, but the most important one is just a URL which I don't want to treat with an argument.
This is the way that I want it to work:
<command> <url> --<optional_argument_1> <value_1> ... --<optional_argument_N> <value_N>
And not like this one:
<command> --<url_argument> <url> ....
Is there a way to this with argparse?
Turns out that there are a kind of argument called "positional arguments" and it solved my problem! :)
This is the tutorial that I followed to achieve what I wanted.
Related
I'm in python 3 and on debian
I would like to have a function that uses os.system. For simplicity's sake something along the lines of:
def notxt():
command = "rm *.txt"
os.system(command)
notxt()
but when I run the script, it hangs without carrying out the command. Is there a way around this or am I approaching it incorrectly?
I would use subproccess here then use run. Like this
import subprocess
subprocess.run(["rm", "1.txt"])
You might to find another bash command to delete all txt files
Here is the docs:
Python3 Docs for Subproccess
I am trying to write a bash script that utilizes the command mkvirtualenv.
I can use it in the console without a problem but as soons I as try to use it in a bash script I get ./run: line 1: mkvirtualenv: command not found
I am not aware of anything that would create such a situation.
Does anyone know why the bash script behaves like that?
The reason emerged form the comments below the question: mkvirtualenv is a function.
If you want the function to exist in the script, you can export it from your shell by
export -f mkvirtualenv
Do you think it's possible in Ubuntu instead of typing python3 test.py every time I want to run a script, to use a shorthand equivalent such like this: py test.py? In other words, what I want is to make a shorthand command for python3 that should look like this: py. Can I do that?
Sure. You can use the bash alias command to do something like this:
alias py=python3
Put it somewhere like .bashrc and IIRC it will be set up for each (interactive) session.
Why not ensure the top line of your python code has
#!/bin/python3
Then make sure the file is executable
chmod my_python_code.py +x
Now just run it
./my_python_code.py
The previous alias command will also work - even if you do the above steps.
./bye.py
I have been looking for this answer for a quite while but not exactly getting the right example or answer, that how can I run a script using a file as a parameter in linux I want to come up with something that if a user type for example this: # ./script.sh check1.txt in the bash so how it can automatically display the content of the file or make the appropriate script process. I am not sure whether I am explaining, right or wrong but this is the best I could. Give me any suggestions, examples or links. cheers.
this is pretty straight-forward, your script needs to use the command line arguments like this
# script.sh looks e.g. like this
infile=$1
second_param=$2
cat $infile > /tmp/$second_param
or whatever you like. then you call it like you expect:
./script.sh check1.txt
or
./script.sh check1.txt 123
if you need a second parameter
On Windows, the following registry setting configures the script interpreter to be used by Apache:
HKEY_CLASSES_ROOT\.cgi\Shell\ExecCGI\Command=C:\Perl\bin\perl.exe
How is this done on Linux?
To add a bit more information to #Mohit's good answer:
Unix uses many interpreters for many languages. Some of them are called "shells", but most are just another computer language to the system. In fact, every file is written in some language, even if it's compiled assembly of Java bytecodes.
The first few bytes of a file are "magic": they tell the OS how to execute the file. If the first two bytes are '#!', the OS knows that the file needs an interpreter. The rest of the first line up to newline is then used as a command to execute. The first "word" (space-separated group of non-spaces) of the line is interpreted as absolute file name to run, and all the other words are passed to it as command line arguments. Last parameter is the file name of the file you're running.
So, for example, if you have the first line as
#!/bin/tclsh
in a file /home/user/aaa.tcl
the OS will execute /bin/tclsh with /home/user/aaa.tcl as command line argument:
/bin/tclsh /home/user/aaa.tcl
For a more advanced example, try this:
#! /bin/env perl
in /home/user/myperlscript
This executes the following command:
/bin/env perl /home/user/myperlscript
/bin/env is a utility program that looks up its first argument using PATH environment variable, and then executes the program it finds, passing the rest of its arguments on to the program. With the help of env, you can use PATH to find your interpreters.
If you are talking about CGI script handlers.
It is set on the first line of each CGI script, I frequently use TCL as my script handler in Apache and hence add:
#!/bin/tclsh
Add this line on top of your script, eg. test.cgi and it will be executed by TCL shell whenever it is requested by someone.
Similary you can set it as
for BASH -- #!/bin/sh
or
for PERL -- #!/usr/bin/perl
Note: The path for the shell binary executable can be different, from above, on your machine. Use the following command to find it:
#which perl
Also, as Max has suggested, do check if Apache is configured to allow CGI scripts
Find detailed description of the same here at this Apache Tutorial Link
ScriptInterpreterSource is an Apache configuration setting and is only supported on Windows. I'm not really experienced at configuring Apache on Linux but I reckon you should check out the Script directive.
There is no registry under Linux. Also, I doubt you will get Perl.exe running under Linux.