Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have created a little script to add bookmarks in Nautilus to redirect users to network share. If I run the script manually, it works brillantly but as soon as I try to add it in /etc/profile.d, I can't connect to the computer. It seems that as soon as I do a loop or anything "fancy" like that, the script give an error. Here is an example if my currently working script :

#!/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

As soon as I had a function like this one :

####
# 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
}

...login stop working. What baffles me is that running this script with *bash add_shortcut.sh* or *./add_shortcut.sh* works fine...

Is it possible that we can't create functions, parse arrays or anything like that in a script called by /etc/profile? And in that case, where or how can I execute a login script for all the users connecting to the computer? Oh, by the way I am running Ubuntu 12.04 LTS!

Thanks in advance and sorry for my english!

EDIT : There is a pastebin of the script. I don't add it compeletly since the script comments are in french so...well, there it is : http://pastebin.com/71XzaBk6.

share|improve this question

1 Answer 1

Stuff in /etc/profile.d is probably sourced by your window manager when you log in, and that probably uses /bin/sh, so target a POSIX shell, not bash, for scripts to be added there. Note that [[ is not found in the scripts that are in there now.

So,

  • choose [ ... ] over [[ ... ]]
  • use funcname() { ... } without the function keyword

dash is a POSIX (only) shell, so you might find the dash man page handy


Comments on http://pastebin.com/71XzaBk6

  • This is the primary error: Change

    if [[ ! -f ~/.gtk-bookmarks ]]
    

    to

     if [ ! -f ~/.gtk-bookmarks ]
    
  • the find_server_by_ip function can be simplified:

    findserverbyip() {
        case "$(hostname -I)" in 
            *[0-9].33.9.[0-9]*) share_server=serverB ;;
            *)                  share_server=serverA ;;
        esac
    }
    

    Also, this comment is misleading: the function returns nothing, it has a side effect.

    # Return : echo Server to use
    
  • might as well remove the shebang line: this script doesn't get executed, it gets sourced.

share|improve this answer
    
Note that goes for your ~/.profile too. –  glenn jackman Mar 19 at 20:37
    
Thanks for the fast reply! Unfortunately I tried to remove everything not "sh"-compliant and still the same problem. I'm starting to think that the problem is caused by /etc/profile that "source" the script, not execute them alongside himself...?? –  Patrick Pruneau Mar 19 at 20:51
    
show (or link to) the whole script. –  glenn jackman Mar 19 at 20:57
    
I added a link to pastebin with the (almost) whole script in the question. –  Patrick Pruneau Mar 20 at 12:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.