在~/目录下修改.bash_profile文件,添加./a.sh,运行source .bash_profile可以自动执行我自己写的a.sh脚本。
但是在注销登录后并不能自动执行,这是为什么呢?要怎么修改才能登录后就自动执行我自己写的脚本呢?
对了,我使用的系统是ubuntu14.04

解决方案 »

  1.   

    有的是运行.bash_profile,如RH系列,有的是.xprofile, 有的是.profile。debian系用的可能不是.bash_profile。你可以到/etc/skel目录下看下,这里会存放当前Linux发型版使用的默认配置,如Redhat系列的在此目录下就会有.bash_profile
      

  2.   

    chmod u+x a.sh
      

  3.   

    我已经添加过权限了,并且我在终端里面执行source .bash_profile可以执行我自己写的脚本,但是在开机登录后自己就不会运行,.bash_profile里面的东西不是在登录后都会默认自动执行的吗?
      

  4.   


    # ~/.profile: executed by the command interpreter for login shells.
    # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
    # exists.
    这是unbuntu /etc/skel的.profile里面的注释,是.bash_profile,我试了一下在~目录下面添加.bash_login,也是不行
      

  5.   

    不知道理解的是否正确,注销/登陆是指X界面的操作么?
    这个当然不会触发bash的profile/rc,打开终端才会起新shell吧。
      

  6.   

    PS,关于.bash_profile和.bashrc,参考man bash的以下内容。       When  bash  is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and exe-
           cutes commands 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  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.
      

  7.   

    我已经添加过权限了,并且我在终端里面执行source .bash_profile可以执行我自己写的脚本,但是在开机登录后自己就不会运行,.bash_profile里面的东西不是在登录后都会默认自动执行的吗?source表示在当前bash环境下执行命令
    建议把脚本添加至/etc/rc.local
      

  8.   


    你使用的是虚拟机吗?如果是的话,你重启之后要是从桌面的虚拟终端进入的话,.bash_profile并不会生效,因为现在是非登录shell,要想使.bash_profile生效只能以登录shell进行操作才可以,而登录shell必须执行su - 用户名 命令才可以,即使这样,当你关闭登录shell时,下次重启机器还是会遇到同样的问题,所以我建议你把./a.sh 配置到 .bashrc里面,这个配置文件不管是否登录式shell都会生效,每次重启机器时就不用重复操作了!