Changing python file permission with chmod +x - python-3.x

I'm a definite Python newbie, so the answer here may be obvious. But I'm trying to change a simple .py file's permissions so it can be run directly from the Terminal window (I'm running Python3 from a Macbook). I've entered the shebang line #! /usr/bin/env python3 at the top of my script, but according to the tutorial I'm following, I next need to run the following command:
chmod +x pythonScript.py
But every time I try running this either from Terminal or the Python shell, I get the following syntax error:
>>> chmod +x pythonScript.py
File "<stdin>", line 1
chmod +x pythonScript.py
^
SyntaxError: invalid syntax
Any ideas on what I'm missing? How do I change the file permission so I can execute a file directly from the Terminal window?

You probably already figured it out but just in case there are others with a similar question.
Your .py file should be saved to your home folder. Say my file is name.py.
When you open the terminal (with current working directory of your home folder) type the command chmod +x name.py.
Now you will be able to run the file in the terminal by typing ./name.py
only use pythonScript.py if that is the name of your .py file

You should run chmod +x filename and ./filename not in python env(>>>). But in directory

Related

Script Can't Find Python3

I'm trying to learn a bit of Python3, so, first things first: Hello World. But as simple as it is, I'm having a terrible time with line 1, the Shebang reference. I create the following script on my laptop running Mac Os 10.15.3
#!/usr/bin/env python3
print('Hello World')
I save it as python_test-01.py, make the script executable, and try to run it
Emonda:Scripts paul$ chmod 755 python_test-01.py
Emonda:Scripts paul$ ls -l
-rwxr-xr-x# 1 paul staff 47 Mar 5 13:07 python_test-01.py
Emonda:Scripts paul$ ./python_test-01.py
./python_test-01.py: line 1: #!/usr/bin/env: No such file or directory
./python_test-01.py: line 2: syntax error near unexpected token `'Hello World''
./python_test-01.py: line 2: `print('Hello World')'
Hmm. Can't find env. OK, I'll use a direct path in the shebang line instead of env. I edit the script to read
#!/usr/bin/python3
print('Hello World')
and run it
Emonda:Scripts paul$ ./python_test-01.py
./python_test-01.py: line 1: #!/usr/bin/python3: No such file or directory
./python_test-01.py: line 2: syntax error near unexpected token `'Hello World''
./python_test-01.py: line 2: `print('Hello World')'
Now it can't find Python3 either. So I look around to make sure I actually have env and python3 installed and where they are.
Emonda:Scripts paul$ cd /usr/bin/
Emonda:bin paul$ pwd
/usr/bin
Emonda:bin paul$ whereis env
/usr/bin/env
Emonda:bin paul$ whereis python3
/usr/bin/python3
Yep, there they are, right where I said they would be in the Shebang line. /usr/bin/
A Second Chapter
After kicking this over again and again, I decide to try something different. I ssh over to my Raspberry Pi running Debian GNU/Linux. I note that it too has a version of Python3 installed in its /usr/bin/ directory. I use vim to write the same two line script starting with the same shebang: #!/usr/bin/python3 . Chmod 755. "Hello World". It works the first try.
What is so different between these two systems? Everything worked as expected in the Raspberry Pi. What's up with the Mac? Where should I be looking on the mac to find the reason the interpreter can't find Python3?
Thanks aging for hanging in there with me,
Paul
After all this and all of your great input, it turns out that the script file on the MacOs system was saved UTF-8 with BOM. That was enough to make first line of the script, the Shebang, illegible to the interpreter. After saving the file the the UTF-8 no BOM encoding, it worked as expected.

"cannot execute binary file" when trying to run a shell script on linux

I am very new to linux and shell scriprting.
I am trying to run a shellscript from secure shell (ssh) on linux using following commands:
chmod +x path/to/mynewshell.sh
sh path/to/mynewshell.sh
I get this error:
path/to/mynewshell.sh: path/to/mynewshell.sh: cannot execute binary file.
Tried using this command:
bash path/to/mynewshell.sh
I get the same error.
Tried with this command: su - myusername sh path/to/mynewshell.sh
It is asking for my password and giving me this error: no such file or directory.
1.The result of cat -v path/to/mynewshell.sh is:
^#^#^#^#^#^#^#^#Rscript "$dir"/diver_script.R
done
2.When tried 'less path/to/mynewshell.sh' i got this on my terminal:
#!/bin/bash/Rscript^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
for dir in /path/to/* ; do
^#^#^#^#^#^#^#^#Rscript "$dir"/myRscript.R
done
3.When i ran file path/to/mynewshell.sh : i got this "Bourne-Again shell script text executable"
Please give any advice on how I can try executing the shellscript.
chmod -x removes execution permission from a file. Do this:
chmod +x path/to/mynewshell.sh
And run it with
/path/to/mynewshell.sh
As the error report says, you script is not actually a script, it's a binary file.
I was getting the same error running my shell script through a bash interpreter in PowerShell. I ran dos2unix myscript.sh on the shell script, and now it runs ok.
From a proposed duplicate:
run_me.sh.xz: run_me.sh.xz: cannot execute binary file
This is because the file is compressed, as indicated by the .xz extension. You need to remove the compression before the file can be used.
xz -d ./run_me.sh.xz
chmod +x ./run_me.sh # probably not necessary if you already did that before
./run_me.sh
Other compression schemes like gzip (.gz extension), bzip2 (.bz2 extension) etc behave similarly; you just have to know the name of the command to uncompress it, which is of course usually easy to google.
To anyone else having the problem i had.
i was trying to run a 16 bit unicode text file converted to a shell script, this doesn't work as all 16 bit unicode text files have a 0xFFFE marker at the start making mac os not like the file and this gives the “cannot execute binary file” error.
open the text file click on "Format" at the top, go down to "Make Plain Text" click it.
open your terminal type chmod 777 /path/to/file.sh
put in terminal: /path/to/file.sh to run it
That script is simply not a shell script.
A shell script is usually readable and contains shell code.
The output your cat command shows looks indeed like it's a binary of some sort.
As some note, it might be because of a file conversion issue when copying but it looks more like an actual binary to me.
You can check what it is identified as with the file command so:
file path/to/mynewshell.sh
Just start with a clean script and rewrite the code, it looks like you just want to run some R scripts in a directory?
Make sure the R scripts point to the right R script executioner.
In my case I had a bash script that would not execute. The file was originally generated from a find ... -print0 command. Leaving a \0 character the script, removing that character solved my problem.

Error running shell script using bash and node.js

I get the following error (error.message) when I run the following shell script (myscript.sh).
myscript.sh
#!/bin/bash
cd /path/to/ && node app.js
error.message
/path/to/myscript.sh: line 1: #!/bin/bash: No such file or directory
/path/to/myscript.sh: line 2: node: command not found
I have already run the following command line instructions.
command-line
chmod u+x /path/to/myscript.sh
chmod u+x /path/to/app.js
Also, I know I have node installed because when I run:
node -v
I get back:
v5.5.1
I execute myscript.sh via the following AppleScript:
MyApp.applescript
do shell script "bash /path/to/myscript.sh"
Also: which bash returns /bin/bash
What could be causing this error and how can I fix it?
I fixed the first error:
/path/to/myscript.sh: line 1: #!/bin/bash: No such file or directory
By copying a working .sh file I had on my machine and copy/pasting the code from the old file to the new file.
I'm guessing somehow there was a filetype issue or discrepancy despite the fact that I used a .sh extension in the file name.
In the future, I will double check the file type in my Finder utility (Max OS X v10.10.1).
However, I am still seeing the second error:
/path/to/myscript.sh: line 2: node: command not found
Credit goes to #HeadCode and #mh-cbon for helping me figure this out with their comments.
I solved the second problem by running:
myshell.sh
#!/bin/bash
path/to/node path/to/app.js
where path/to/node was found by running
command-line
which node
and path/to/app.js is the actual file tree path to app.js. (In other words, different from path/to/node.)

Can't run a script

I tried to create a script in linux, on a Synology server over SSH
so I wrote a file test.sh
#!/bin/bash
echo "this is a test"
I saved the file.
after that I did
chmod 755 test.sh
the I did
./test.sh
then i got this error
-ash "./test.sh" is not found
the file was created in
/root
I don't understand
Your shell (ash?) is trying to execute your script and is getting an ENOENT (no such file or directory) error code back. This can refer to the script itself, but in this case it refers to the interpreter named in the #! line.
That is, /bin/bash does not exist and that's why the script couldn't be started.
Workaround: Install bash or (if you don't need any bash specific features) change the first line to #!/bin/sh.
This is one of the quirks with hash bang programs. If the interpreter is not found (i.e. the program interpreting the script), you don't get a completely useful error like /bin/bash: no such file, but a completely useless and misleading test.sh: not found.
If this isn't in the Unix Hater's Handbook, it should be. :-)
You can either use #!/bin/sh or #!/path/to/bash or #!/usr/bin/env bash (which searches PATH for bash).

How to set a program to run in Linux terminal only with program name

I'm new to Linux and I wonder there are many programs we can use only program name to start it in Linux terminal, like gedit,vi,firefox instead of providing the all program's path,I like to run my own programs like this in terminal only typing program name, programs I like to run are written in Java and Python (.jar, .pyc, .py and .class)
I like to know how to do it with step by step
You can write whatever program/script you have to behave as a command. Let's say your executable script/program is named as my_script and is placed in /path/to/my_script.
Be sure that the script is executable. If not,then please do
chmod +x /path/to/my_script
Then, place a symlink to this location in /usr/local/bin as
sudo ln -s /path/to/my_script /usr/local/bin
You can add the symlink to any of the paths mentioned in $PATH.
That's it and enjoy your program.
The other answers all involve creating a symlink in a directory that is already listed in the system PATH, but I think it is more unixy to add needed directories to your PATH.
If your script is located at $HOME/bin/myscript and you have already made sure that it is executable then you can run
export PATH=$HOME/bin:$PATH
to run it without giving the full path. And you can add that same line to your .bashrc file in your home directory to have it preloaded whenever you start your shell. This approach does not require that the user has permission to create symlinks in system directories.
If you have an executable binary file in your home folder (let's say for example sublime_text) you must give it execute permision and call it with its relative path
chmod +x sublime_text
./sublime_text
If you made a symlink to it in /usr/bin (or other folders included in your PATH), you would be able to call it by its name
sudo ln -s ~/sublime_text /usr/bin/sublime_text
sublime_text
In your case, you aren't dealing with binary files, but with scripts meant to be interpreted. For this you must prepend a shebang telling linux what's the binary meant to execute the script. If it was, for example, a python script ~/hello.py, these could be the contents of the script:
#!/usr/bin/python
print "Hello, World!"
Where the first line tells linux to use the python binary to execute the script.
From then on, you can do:
chmod +x hello.py
sudo ln -s ~/hello.py /usr/bin/hello
hello
And it will echo "Hello World" to the console.

Resources