Go Back   openSUSE Forums > Archives > SLS Archives > ARCHIVES - SuSE Linux > ARCHIVES - General Questions
Forums FAQ Members List Search Today's Posts Mark Forums Read


ARCHIVES - General Questions If your question doesn't fit in any other category below ask in here.

 
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-Jun-2008, 08:46
beli0135
Guest
 
Posts: n/a
Thumbs up

Hi.

I have a problem that for executing python script in bash, I need to type:
> python myfile.py
because in no other way it starts
Also for the shell scripts, I must do > sh myscript.sh

How to make it run by just typting the file name? (both shell and python)

I always put in python script as a first line:
#!/usr/bin/env python

and for shell:
#!/bin/sh

Where is the catch?

Thanks!
  #2 (permalink)  
Old 05-Jun-2008, 08:52
init7
Guest
 
Posts: n/a
Default

You mean just typing the file directly, regardless the current position?
Then you need to place the file in your PATH variable,
Code:
echo $PATH
When you are executing application, the shell actually find for that file from the PATH variable.
The most simple way is to place your script in the ~/bin directory. You can also symbolic link it. It has already added in PATH variable.
Don't forget to give the execute permission.

Oh yeah, just some tips. You may consider to give your script different naming convention, for example all caps or just the first character of file are caps, to avoid conflict with the system command which usually using small character.
  #3 (permalink)  
Old 05-Jun-2008, 08:58
beli0135
Guest
 
Posts: n/a
Default

Thanks.
Yes, I did put it in my script directory and put it in path. I found, just now, that for shell scripts, I should put #! /bin/bash and add execute permissions.
This is OK now.

However, I cannot resolve python thing
  #4 (permalink)  
Old 05-Jun-2008, 09:09
init7
Guest
 
Posts: n/a
Default

Try to change it to:
Code:
#!/usr/bin/python
if it's there python located.

It's called preprocessor directive, if I'm not mistaken.
  #5 (permalink)  
Old 05-Jun-2008, 09:51
waveclaw
Guest
 
Posts: n/a
Default

Quote:
Hi.

I have a problem that for executing python script in bash, I need to type:
> python myfile.py
because in no other way it starts
[/b]
What are the permissions on the script?

i.e. ls -l myfile.py

Are the execute permissions set?
  #6 (permalink)  
Old 05-Jun-2008, 11:38
hcvv
Guest
 
Posts: n/a
Default

Quote:
Try to change it to:
Code:
#!/usr/bin/python
if it's there python located.

It's called preprocessor directive, if I'm not mistaken.
[/b]
In openSUSE 10.3 it is /usr/bin/python.
The #!/.... construction has nothing to do with any preprocessing. It is the way to tell the shell (where you type the command to start the script) which interpreter to start. Somewhere the system must know if this is a bash, python, php or whatever script. The #!/... is sometimes called the shebang (there are other nicknames). Read Wikipedia.
  #7 (permalink)  
Old 05-Jun-2008, 12:09
beli0135
Guest
 
Posts: n/a
Default

I found that for some reason, gedit puts some strange character when you press enter. Something like windows' #13#10

now works.
Thank you all
  #8 (permalink)  
Old 05-Jun-2008, 12:25
FeatherMonkey
Guest
 
Posts: n/a
Default

Use bash -x when trouble shooting how I found out last time you did this

So if ./file doesn't work execute it as
bash -x ./file then you'll get hopefully some useful feedback.
  #9 (permalink)  
Old 05-Jun-2008, 20:43
init7
Guest
 
Posts: n/a
Default

Quote:
In openSUSE 10.3 it is /usr/bin/python.
The #!/.... construction has nothing to do with any preprocessing. It is the way to tell the shell (where you type the command to start the script) which interpreter to start. Somewhere the system must know if this is a bash, python, php or whatever script. The #!/... is sometimes called the shebang (there are other nicknames). Read Wikipedia.
[/b]
Ah thanks, now I know what it is meaning.
 

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2