添加脚本到/etc/profile.d后无法登录

Pat*_*eau 5 bash scripts login .profile 12.04

我创建了一个小脚本来在 Nautilus 中添加书签,以将用户重定向到网络共享。如果我手动运行该脚本,它运行得非常好,但是一旦我尝试将其添加到 /etc/profile.d 中,我就无法连接到计算机。似乎只要我执行一个循环或任何类似的“花哨”的事情,脚本就会出错。这是我当前正在工作的脚本的示例:

#!/bin/bash

# Creating gtk-bookmarks if it doesn't exists
if [[ ! -f ~/.gtk-bookmarks ]]
then
        touch ~/.gtk-bookmarks
fi

# Adding bookmarks if not present
if ! grep -Fxq "smb://example.com/R03C01 Software:" ~/.gtk-bookmarks
then
    echo "smb://example.com/R03C01 Software:" >> ~/.gtk-bookmarks
fi
Run Code Online (Sandbox Code Playgroud)

一旦我有了这样的功能:

####
# Function find_server_by_ip()
# Desc : This function find the server to use for the current host IP Address
# Parameters : None
# Return : echo Server to use
####
function find_server_by_ip()
{
        hostname_ip=$(hostname -I)
        IFS="."
        set -- $hostname_ip

        if [[ $3 -eq 78 ]]
        then
                echo "Server A"
        else
                echo "Server B"
        fi
}
Run Code Online (Sandbox Code Playgroud)

...登录停止工作。令我困惑的是,使用 *bash add_shortcut.sh* 或 *./add_shortcut.sh* 运行此脚本效果很好......

我们是否有可能无法在 /etc/profile 调用的脚本中创建函数、解析数组或类似的内容?在这种情况下,我在哪里或如何为连接到计算机的所有用户执行登录脚本?哦,顺便说一句,我正在运行 Ubuntu 12.04 LTS!

预先感谢并抱歉我的英语!

编辑:有一个脚本的粘贴箱。我没有完全添加它,因为脚本注释是法语,所以......好吧,它就在那里:http://pastebin.com/71XzaBk6

gle*_*man 2

/etc/profile.d 中的内容可能是您登录时由窗口管理器获取的,并且可能使用 /bin/sh,因此针对要在其中添加脚本的目标是 POSIX shell,而不是 bash。[[请注意,现在的脚本中找不到此内容。

所以,

  • 选择[ ... ][[ ... ]]
  • funcname() { ... }不带function关键字使用

dash 是 POSIX(仅)shell,因此您可能会发现dash 手册页很方便


http://pastebin.com/71XzaBk6上的评论