如何在终端直接运行自己的程序?

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
Daniel23
帖子: 70
注册时间: 2007-12-26 15:56
来自: MIT

如何在终端直接运行自己的程序?

#1

帖子 Daniel23 » 2008-02-28 10:05

我自己编了一个程序,生成了可执行文件,在/home/XX/test/test1。我想在终端里敲test1直接运行,应该怎样做?请高手赐教!
头像
Daphne
帖子: 39
注册时间: 2008-02-25 14:30

#2

帖子 Daphne » 2008-02-28 10:12

cd /home/xxx/test
./test1

或者直接在终端/home/xx/test/test1
或者 cd ~xx/test && test1
.
.
.
前提是test1文件有可执行权限。即 ls -l时,文件对应你的用户有x权限
Daniel23
帖子: 70
注册时间: 2007-12-26 15:56
来自: MIT

#3

帖子 Daniel23 » 2008-02-28 10:15

Daphne 写了:cd /home/xxx/test
./test1

或者直接在终端/home/xx/test/test1
或者 cd ~xx/test && test1
.
.
.
前提是test1文件有可执行权限。即 ls -l时,文件对应你的用户有x权限
我明白这种方法,我想直接敲程序名,而不用路径,应该怎样做呢?
头像
Daphne
帖子: 39
注册时间: 2008-02-25 14:30

#4

帖子 Daphne » 2008-02-28 10:20

环境变量内添加path
比如你使用bash
进入自己的主目录,即输入cd后面不加东西,回车
vi .bashrc (有个点哦)
在后面添加 export PATH=/home/xxx/test:$PATH
然后保存退出
然后. .bashrc (两个点中间有空格哦)
然后再输入test1试下?

如果你只是要临时的,在终端下输入
PATH=/home/xxx/test:$PATH 回车
再输入test1试下呢?
不过这样你把当前终端关了变量就失效了哦
Daniel23
帖子: 70
注册时间: 2007-12-26 15:56
来自: MIT

#5

帖子 Daniel23 » 2008-02-28 10:30

Daphne 写了:环境变量内添加path
比如你使用bash
进入自己的主目录,即输入cd后面不加东西,回车
vi .bashrc (有个点哦)
在后面添加 export PATH=/home/xxx/test:$PATH
然后保存退出
然后. .bashrc (两个点中间有空格哦)
然后再输入test1试下?

如果你只是要临时的,在终端下输入
PATH=/home/xxx/test:$PATH 回车
再输入test1试下呢?
不过这样你把当前终端关了变量就失效了哦
我按你说的第一种方法没有成功啊,按你说的是不是在test下面不过多少个可执行文件都可以执行了?
那系统自带的可执行文件是怎么设置在终端里敲名字就能直接运行的阿?
头像
Daphne
帖子: 39
注册时间: 2008-02-25 14:30

#6

帖子 Daphne » 2008-02-28 10:48

请确认自己使用的是bash,而且是修改当前用户主目录下的.bashrc,并且两个路径中间是使用了英文的:分割,并且确定已经保存修改
最后. .bashrc了。

系统中的环境变量为:vi /etc/environment
如果你用的不是ubuntu,比如red hat系列,而且你使用的是bash,请查看/etc/profile
不建议把你那个学习用的PATH加入。最多就是象第一种方法那样修改个人用户的环境变量就好
建议先学下linux基础 - -||
环境变量配置的章节。
上次由 Daphne 在 2008-02-28 10:54,总共编辑 1 次。
Daniel23
帖子: 70
注册时间: 2007-12-26 15:56
来自: MIT

#7

帖子 Daniel23 » 2008-02-28 10:54

Daphne 写了:请确认自己使用的是bash,而且是修改当前用户主目录下的.bashrc,并且确定已经保存修改
最后. .bashrc了。

系统中的环境变量为:vi /etc/environment
如果你用的不是ubuntu,比如red hat系列,而且你使用的是bash,请查看/etc/profile
不建议把你那个学习用的PATH加入。最多就是象第一种方法那样修改个人用户的环境变量就好
建议先学下linux基础 - -||
环境变量配置的章节。
谢谢!已经搞好了,发现要退出下当前编辑.bashrc的终端,再在新终端运行就好了!
非常感谢您的帮助!
Daniel23
帖子: 70
注册时间: 2007-12-26 15:56
来自: MIT

#8

帖子 Daniel23 » 2008-02-28 10:55

Daphne

Can I ask u some question? I'm in Nanjing too! working on ubuntu.
can u tell me your email?
头像
Daphne
帖子: 39
注册时间: 2008-02-25 14:30

#9

帖子 Daphne » 2008-02-28 10:58

Daniel23 写了:Daphne

Can I ask u some question? I'm in Nanjing too! working on ubuntu.
can u tell me your email?
http://irc.ubuntu.org.cn/irc.cgi
这里的大牛很多
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

#10

帖子 xhy » 2008-02-28 11:14

: /etc/profile
//全局初始化文件,作用于登录式shell

: /etc/bashrc
//标准bash没有定义这个配置文件

: ~/.bashrc
//每一个交互式shell的初始化文件


: ~/.bash_profile
//个人初始化文件,在执行完/etc/profile后执行,作用于登录式shell



如果你从TTY登录,输入用户名和秘密,
那么首先执行/etc/profile然后执行~/.bash_profile
(从TTY登录的shell是以-开头的)


如果在X中打开终端,那么执行~/.bashrc


一些概念: 登录式Shell,交互式Shell
INVOCATION
A login shell is one whose first character of argument zero is a -, or
one started with the --login option.

An interactive shell is one started without non-option arguments and
without the -c option whose standard input and error are both connected
to terminals (as determined by isatty(3)), or one started with the -i
option. PS1 is set and $- includes i if bash is interactive, allowing
a shell script or a startup file to test this state.

The following paragraphs describe how bash executes its startup files.
If any of the files exist but cannot be read, bash reports an error.
Tildes are expanded in file names as described below under Tilde Expan-
sion in the EXPANSION section.

When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the
file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.

When bash is started non-interactively, to run a shell script, for
example, it looks for the variable BASH_ENV in the environment, expands
its value if it appears there, and uses the expanded value as the name
of a file to read and execute. Bash behaves as if the following com-
mand were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file
name.
目前负债150多万
xiangsiaiwen
帖子: 133
注册时间: 2007-12-26 8:02

#11

帖子 xiangsiaiwen » 2008-02-28 11:18

在/bin里建立链接试试
(我没用过)
头像
gaint_star
帖子: 413
注册时间: 2008-02-20 0:09
来自: 深圳
联系:

#12

帖子 gaint_star » 2008-02-28 12:30

xiangsiaiwen 写了:在/bin里建立链接试试
(我没用过)
这样最简单啦
凡是在/bin目录里面的程序都可以在终端内调用
我也新手,以前这样做过!
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#13

帖子 BigSnake.NET » 2008-02-28 12:33

Daniel23 写了:
Daphne 写了:cd /home/xxx/test
./test1

或者直接在终端/home/xx/test/test1
或者 cd ~xx/test && test1
.
.
.
前提是test1文件有可执行权限。即 ls -l时,文件对应你的用户有x权限
我明白这种方法,我想直接敲程序名,而不用路径,应该怎样做呢?
放到 ~/bin 目录
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

#14

帖子 xhy » 2008-02-28 12:47

做一个符号连接过去就ok了
目前负债150多万
头像
yaoms
帖子: 4952
注册时间: 2007-10-19 14:51
来自: 深圳

#15

帖子 yaoms » 2008-02-29 10:16

放到 ~/bin里面,没有这个目录就mkdir一个
Nothing 有事请发邮件到 yms541 AT gmail.com
alias 爱慕颇雷尔='mplayer'
回复