Author Topic: How can a package automate individual users' .bashrc?  (Read 870 times)

Offline Archie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8821
  • Aurum nostrum non est aurum vulgi.
How can a package automate individual users' .bashrc?
« on: November 23, 2011, 01:30:54 AM »
Hey all,

I got this app already built and packaged ... but it seems that my specfile is still missing something (maybe) on %post.

I need to somehow tell the installation to echo [a_script_that_will_add_a_couple_of_lines] >> .bashrc for each user on a system, and notify the user that the .bashrc needed to be source.

How do I do that? Do we have any packages with a similar event or something that I can take a look at? Sorry if my explanation is a bit unclear

Thanks for any help.
Since 2006 | LiCo 401868 | Bare Metal | What is necessary is never unwise. --Sarek, 2258.42


Offline Archie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8821
  • Aurum nostrum non est aurum vulgi.
Re: How can a package automate individual users' .bashrc?
« Reply #1 on: November 23, 2011, 01:33:13 AM »
The following is part of install.sh (which I did not use on the spec file)

Code: [Select]
if [ `grep -c "^[[:space:]]*\(source\|\.\) /etc/profile\(\.d/autojump\.bash\)[[:space:]]*$" ${bashrc_file}` -eq 0 ]; then
        echo "Your .bashrc doesn't seem to source /etc/profile or /etc/profile.d/autojump.bash"
        echo "Adding the /etc/profile.d/autojump.bash to your .bashrc"
        echo "" >> ${bashrc_file}
        echo "# Added by autojump install.sh" >> ${bashrc_file}
        echo "source /etc/profile.d/autojump.bash" >> ${bashrc_file}
    fi
    echo "Done!"
    echo
    echo "You need to source your ~/.bashrc (source ~/.bashrc) before you can start using autojump."
else
    echo "Your distribution does not have a /etc/profile.d directory, the default that we install one of the scripts to. Would you like us to copy it into your ~/.bashrc file to make it work? (If you have done this once before, delete the old version before doing it again.) [y/n]"
    read ans
    if [ ${#ans} -gt 0 ]; then
     if [ $ans = "y" -o $ans = "Y" -o $ans = "yes" -o $ans = "Yes" ]; then

                # Answered yes. Go ahead and add the autojump code
        echo "" >> ${bashrc_file}
        echo "#autojump" >> ${bashrc_file}
        cat autojump.bash | grep -v "^#" >> ${bashrc_file}

                # Since OSX uses .bash_profile, we need to make sure that .bashrc is properly sourced.
                # Makes the assumption that if they have a line: source ~/.bashrc or . ~/.bashrc, that
                # .bashrc has been properly sourced and you don't need to add it.
                OS=`uname`
                if [ $OS == 'Darwin' -a x`grep -c "^[[:space:]]*\(source\|\.\) ~/\.bashrc[[:space:]]*$" ~/.bash_profile` == x0 ]; then
                    echo "You are using OSX and your .bash_profile doesn't seem to be sourcing .bashrc"
                    echo "Adding source ~/.bashrc to your bashrc"
                    echo -e "\n# Get the aliases and functions" >> ~/.bash_profile
                    echo -e "if [ -f ${bashrc_file} ]; then\n  . ${bashrc_file}\nfi" >> ~/.bash_profile
                fi
                echo "You need to source your ~/.bashrc (source ~/.bashrc) before you can start using autojump."
     else
         echo "Then you need to put autojump.sh, or the code from it, somewhere where it will get read. Good luck!"
     fi
    else
        echo "Then you need to put autojump.sh, or the code from it, somewhere where it will get read. Good luck!"
    fi
fi
Since 2006 | LiCo 401868 | Bare Metal | What is necessary is never unwise. --Sarek, 2258.42


Offline Archie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8821
  • Aurum nostrum non est aurum vulgi.
Re: How can a package automate individual users' .bashrc?
« Reply #2 on: November 23, 2011, 01:34:01 AM »
And this is my specfile:

Code: [Select]
%define fname joelthelion
%define name autojump
%define version v18
%define lname 054edc5
%define release %mkrel 1

Name: %{name}
Version: %{version}
Release: %{release}
Summary: autojump is a faster way to navigate your filesystem.
URL: https://github.com/joelthelion/autojump
Group: Utilities
License: GPL 2+
Source: %{fname}-%{name}-release-%{version}-1-g%{lname}.tar.xz
BuildRequires: bash
BuildRoot: %{_tmppath}/%{name}-buildroot

%description
One of the most used shell commands is cd. Between 10 and 20 percent
of all commands they type are actually cd commands! Unfortunately,
jumping from one part of your system to another with cd requires to
enter almost the full path, which isn't very practical and requires a lot
of keystrokes.

autojump is a faster way to navigate your filesystem. It works by maintaining
a database of the directories you use the most from the command line. The
jumpstat command shows you the current contents of the database. You need
to work a little bit before the database becomes usable.

%prep
%setup -q -n %{name}

%build

%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT%_datadir/autojump/
mkdir -p $RPM_BUILD_ROOT%_usr/bin/
mkdir -p $RPM_BUILD_ROOT%_datadir/man/man1/
cp icon.png $RPM_BUILD_ROOT%_datadir/autojump/
cp jumpapplet $RPM_BUILD_ROOT%_usr/bin/
cp autojump $RPM_BUILD_ROOT%_usr/bin/
cp autojump.1 $RPM_BUILD_ROOT%_datadir/man/man1/

mkdir -p $RPM_BUILD_ROOT%_sysconfdir/profile.d/
cp autojump.bash $RPM_BUILD_ROOT%_sysconfdir/profile.d/
cp autojump.sh $RPM_BUILD_ROOT%_sysconfdir/profile.d/

%post
echo "You need to source your ~/.bashrc (source ~/.bashrc) before you can start using autojump."

%postun

%files
%defattr(-,root,root,-)
%_bindir/autojump
%_bindir/jumpapplet
%_sysconfdir/profile.d/autojump.sh
%_sysconfdir/profile.d/autojump.bash
%_datadir/autojump/icon.png
%_datadir/autojump
%_datadir/man/man1/autojump.1.bz2

%clean
rm -rf $RPM_BUILD_ROOT

%changelog


Since 2006 | LiCo 401868 | Bare Metal | What is necessary is never unwise. --Sarek, 2258.42


Offline AS

  • Hero Member
  • *****
  • Posts: 4098
  • Have a nice ... night!
Re: How can a package automate individual users' .bashrc?
« Reply #3 on: November 23, 2011, 03:56:38 AM »

Hi Archie,
I think you don't need to do anything, all needed work is actually done by placing the file /etc/profile.d/autojump.bash:

PCLinuxOS default setup actually source in the files in /etc/profile.d directory, for each user, therefore the install script actually perform only:
"echo Done!"

see in red the execution flow:
Quote

The following is part of install.sh (which I did not use on the spec file)

Quote
if [ `grep -c "^[[:space:]]*\(source\|\.\) /etc/profile\(\.d/autojump\.bash\)[[:space:]]*$" ${bashrc_file}` -eq 0 ]; then
        echo "Your .bashrc doesn't seem to source /etc/profile or /etc/profile.d/autojump.bash"
        echo "Adding the /etc/profile.d/autojump.bash to your .bashrc"
        echo "" >> ${bashrc_file}
        echo "# Added by autojump install.sh" >> ${bashrc_file}
        echo "source /etc/profile.d/autojump.bash" >> ${bashrc_file}
    fi
    echo "Done!"
    echo
    echo "You need to source your ~/.bashrc (source ~/.bashrc) before you can start using autojump."

else
    echo "Your distribution does not have a /etc/profile.d directory, the default that we install one of the scripts to. Would you like us to copy it into your ~/.bashrc file to make it work? (If you have done this once before, delete the old version before doing it again.) [y/n]"
    read ans
    if [ ${#ans} -gt 0 ]; then
        if [ $ans = "y" -o $ans = "Y" -o $ans = "yes" -o $ans = "Yes" ]; then

                # Answered yes. Go ahead and add the autojump code
           echo "" >> ${bashrc_file}
           echo "#autojump" >> ${bashrc_file}
           cat autojump.bash | grep -v "^#" >> ${bashrc_file}

                # Since OSX uses .bash_profile, we need to make sure that .bashrc is properly sourced.
                # Makes the assumption that if they have a line: source ~/.bashrc or . ~/.bashrc, that
                # .bashrc has been properly sourced and you don't need to add it.
                OS=`uname`
                if [ $OS == 'Darwin' -a x`grep -c "^[[:space:]]*\(source\|\.\) ~/\.bashrc[[:space:]]*$" ~/.bash_profile` == x0 ]; then
                    echo "You are using OSX and your .bash_profile doesn't seem to be sourcing .bashrc"
                    echo "Adding source ~/.bashrc to your bashrc"
                    echo -e "\n# Get the aliases and functions" >> ~/.bash_profile
                    echo -e "if [ -f ${bashrc_file} ]; then\n  . ${bashrc_file}\nfi" >> ~/.bash_profile
                fi
                echo "You need to source your ~/.bashrc (source ~/.bashrc) before you can start using autojump."
        else
            echo "Then you need to put autojump.sh, or the code from it, somewhere where it will get read. Good luck!"
        fi
    else
        echo "Then you need to put autojump.sh, or the code from it, somewhere where it will get read. Good luck!"
    fi
fi

AS

Offline kjpetrie

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 4037
Re: How can a package automate individual users' .bashrc?
« Reply #4 on: November 23, 2011, 05:12:59 AM »
Except many people either don't read the "Additional Output" or regard it as an error message and panic.

A loop which parses /etc/passwd looking for uids of 500 and over and then editing each /home/<username>/.bashrc would be how I'd do it.
-----------
KJP
-----------------------------------------------------------
PClos64 RC1 on Intel D945GCLF2 motherboard (Atom 330), 2GB DDR2 RAM, Maxtor STM325031, HL-DT-ST DVDRAM GSA-H42N, Amilo LSL 3220T monitor. Also Acer 5810TG (with custom kernel) and Asus eeePC 2G surf

Offline Archie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8821
  • Aurum nostrum non est aurum vulgi.
Re: How can a package automate individual users' .bashrc?
« Reply #5 on: November 23, 2011, 05:31:32 AM »
@as, if you look at my specfile, you will note that I did not add that script as I think several sections are not necessary.

I had a look at my .bashrc after running the install.sh and noted that there are two additional lines added - a comment and the command source.

So what you are saying is that those added lines are not necessary because autojump.bash is already in place at /etc/profile.d/?

I will need to test this on another install and see if that is the case.

To be perfectly honest, I was very confused by the grep line and all the open/close brackets ... and had no clue what it was although I would have guess that it was searching for a matching file, autojump.bash.

If I need to trim the script down to what is basically writing the last two echo before echo "Done!", I could probably get away without the grep line. I just want to be sure that it doesn't get skipped.

But I'll do some test first. Thank you for your reply.

Except many people either don't read the "Additional Output" or regard it as an error message and panic.

A loop which parses /etc/passwd looking for uids of 500 and over and then editing each /home/<username>/.bashrc would be how I'd do it.


@kjpetrie, and how would that be done if you were to do it?

I think this is just a case of ensuring that if there are three or four users in the same computer, all of them would have their .bashrc sourced.
Since 2006 | LiCo 401868 | Bare Metal | What is necessary is never unwise. --Sarek, 2258.42


Offline AS

  • Hero Member
  • *****
  • Posts: 4098
  • Have a nice ... night!
Re: How can a package automate individual users' .bashrc?
« Reply #6 on: November 23, 2011, 06:04:07 AM »
@as, if you look at my specfile, you will note that I did not add that script as I think several sections are not necessary.

I had a look at my .bashrc after running the install.sh and noted that there are two additional lines added - a comment and the command source.

So what you are saying is that those added lines are not necessary because autojump.bash is already in place at /etc/profile.d/?


Yeah, you got it!  ;)

actually (in PCLinuxOS), /home/user/.bashrc will source in /etc/bashrc that in turn will source in /etc/profile.d/*

Quote
I will need to test this on another install and see if that is the case.

To be perfectly honest, I was very confused by the grep line and all the open/close brackets ... and had no clue what it was although I would have guess that it was searching for a matching file, autojump.bash.


Although the install script is meant to discriminate the target environment, actually is based on the wrong assumption that  /etc/profile.d/* files are sourced in from /home/user/.bashrc, which is not the case in PCLinuxOS, because of the intermediate step /etc/bashrc.

Quote

If I need to trim the script down to what is basically writing the last two echo before echo "Done!", I could probably get away without the grep line. I just want to be sure that it doesn't get skipped.

But I'll do some test first. Thank you for your reply.


Always welcome!

AS

Offline Archie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8821
  • Aurum nostrum non est aurum vulgi.
Re: How can a package automate individual users' .bashrc?
« Reply #7 on: November 23, 2011, 06:38:13 AM »
Hah! You're right as ever, as. There's nothing wrong with it. I tested it on 32-bit and the user did not have the two lines on the .bashrc but was still able to use autojump. Thank you again for your valuable advice and for putting up with me.
Since 2006 | LiCo 401868 | Bare Metal | What is necessary is never unwise. --Sarek, 2258.42