0
When I use Python on Linux, I usually transform extension files .py
executable using the chmod
. Example:
Filing cabinet user_sum_python.py
:
#!/usr/bin/env python
def user_sum(L):
if len(L)==1:
return L[0]
else:
return L[0]+user_sum(L[1:])
print(user_sum([2,4,8,23,56,21,20,4,87]))
On the command line:
chmod +x user_sum_python.py
And so I can run the program using only ./user_sum_python.py
.
On the other hand, if I write the same program in a compiler language, the computer already returns an executable. Example using Rust:
Filing cabinet user_sum_rust.rs
:
fn user_sum(lista: &[i32]) -> i32 {
if lista.len() == 1 {
return lista[0];
} else {
return lista[0] + user_sum(&lista[1..]);
}
}
fn main() {
let one_list: [i32; 9] = [2, 4, 8, 23, 56, 21, 20, 4, 87];
println!("{}", user_sum(&one_list));
}
Whirling rustc user_sum_rust.rs
in the terminal, the program generates a file that I can run using ./user_sum_rust
.
What is the difference between the two procedures? There are advantages and disadvantages associated with each of them?
Simply the Rust compiler ran chmod after the compilation. The question is mixing two things that have no direct relation (executable program with flag authorizing execution). the linux "+x" simply indicates that the user is allowed to ask for the environment to run that. You can give a chmod +x in a jpeg if you want. Only it is not good for much, because you authorized the user to execute something that is not executable.
– Bacco
Even without +x you can continue running . py using python directly. There is already a third thing, which is the linux shell using +x for another purpose, already described here https://answall.com/questions/57702/70
– Bacco
Rust only ran chmod? So you mean if I run
chmod +x user_sum_rust.rs
I will have the same result as runningrustc user_sum_rust.rs
?– Lucas
I didn’t say "just". He compiled it. Then he spun the chmod (so you don’t have to do 2 steps). The Rust is generating an executable. Yours. py is not an executable, it’s just a script (whoever runs it is Python, even if you call it directly - see the link I passed earlier - it’s not it alone), so it doesn’t need to be compiled.
– Bacco
so if I remove the Rust from my computer the command
./user_sum_rust
will continue working, but if I remove python the command./user_sum_python.py
will stop working?– Lucas
In theory, yes. Of course there are other N factors. In fact you don’t even need to remove Py, if you take it out of the path indicated in the first line of your script already prevents direct execution. So much so that when you run the rustc it generates another file (without the rs extension) for you to run (you don’t run the source). py no, you are calling the original file.
– Bacco
I think we can start here: What is a scripting language and by the answer links here: Scripting language is always built on another language? - in theory, would answer the central problem of the question.
– Bacco
Worth reading about Shebang: https://pt.wikipedia.org/wiki/Shebang
– Luiz Felipe