使用高级 .profile 登录 X-Windows

使用高级 .profile 登录 X-Windows

我刚刚将我的一个系统升级到 Precise Pangolin (Ubuntu 12.04)。(Lucid 早就该升级了。哎呀!)

当我尝试登录 X Windows 时,我得到一个包含以下内容的 ~/.xsession-errors 文件:

/etc/gdm/Xsession: Beginning session setup...
/etc/gdm/Xsession: 11: /home/username/.profile: function: not found
/etc/gdm/Xsession: 19: /home/username/.profile: RC: not found
Warning: unknown mime-type for "0" -- using "application/octet-stream"
Error: no such file "0"
/etc/gdm/Xsession: 23: /home/username/.profile: Syntax error: "}" unexpected

我的正常登录 shell 是 ksh,在 .profile 中我定义了一个函数 - 该函数在脚本中使用了几次。以上所有消息都与该函数有关。定义如下:

function checkFile {
#<stuff here>
print "${RC}"
}

我似乎找不到解决这个问题的方法;没有找到让 X-windows 登录识别高级 .profile 文件的方法,也没有找到让它忽略 .profile 或其中任何行的方法。我尝试创建 .bash_profile 和 .bash_rc,但登录时仍然会读取 .profile。我还尝试将函数(及其调用者)包装在交互式 shell 的测试中。这只会将错误消息更改为“{found - expecting fi”或类似内容。

我不喜欢被任何软件强行塞进 20 世纪 70 年代的盒子里....除了不使用 .profile 中的这些功能和不使用 X-windows 之外,我还有什么选择?

下面是我使用更可移植的语法时发生的情况的日志:

username@hpmicro1:/home/username> cat .xsession\-errors
/etc/gdm/Xsession: Beginning session setup...
/etc/gdm/Xsession: 12: /home/username/.profile: Syntax error: "(" unexpected (expecting "fi")

username@hpmicro1:/home/username> more .profile
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

if [ "${SHELL}" = "/bin/ksh" ]; then
function checkFile() {

答案1

您可以尝试一种更便携的语法:

checkFile() {
    #<stuff here>
    echo "${RC}"
}

答案2

听起来,虽然您的登录 shell 是 ksh,但 X 服务器正在使用更标准的 Bourne shell 来获取您的 .profile。以 bash 为例,您可以在 .profile 中的任何特定于 bash 的命令之前使用如下逻辑:

if [ "$BASH" != "" ]; then
  bash stuff
fi

我确信 ksh 环境中会有一些东西可以用于类似的检查。

相关内容