Bash script not working

I would like to write a bash script capable of executing a SU command with the "user" like a parameter. Then copy a file to that user's home directory and exiting the session (and the script). This is because I do not have root access.

I wrote the following:

#! /bin/sh
su $1
cp .procmailrc /home/$1
exit
echo File copied

However it doesn't work. After typing the user's password I am returned to the prompt and no copying is performed. The current user after the operation is the one in question (so the exit clause is not executed). If I type exit at that point I get the following error:

cp: cannot stat '/home/user/.procmailrc' : Permission denied
Filed copied

I am just a newbie with bash scripts, how can I change it so it does the job ?

Thanks
agubairaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
#!/bin/sh
su $1 -c "cp .procmailrc /home/$1"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahoffmannCommented:
# a bit improved:
su $1 -c "cp /full/path/to/.procmailrc /home/$1" && echo File copied
0
agubairaAuthor Commented:
I tried both scripts and get the following message:

bash: /home/admin/.bashrc: Permission denied
File copied

The user I am trying to switch from to is "adele". After execution a "whoami" command returns "admin" (the user I logged on with).
 
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

agubairaAuthor Commented:
typo: The user I am trying to switch to is "adele"...
0
ahoffmannCommented:
> bash: /home/admin/.bashrc: Permission denied
did you try to write that file?
either you're not owner of the file, or it has no write permission for you, or it is located on NFS (with improper permissions)
0
TintinCommented:
whoami will show you the user you are logged into as the 'su' only lasts as long as the script runs.

0
agubairaAuthor Commented:
ahoffmann,

No I didn't try to write to that file. I just executed the script proposed by Tintin as user "admin". However I noticed the .procmailrc did get copied. Why the error though ?
0
NVRAMCommented:
> bash: /home/admin/.bashrc: Permission denied

Add "unset BASH_ENV" to eliminate this warning.

Also, if my memory is correct, the file needs to be unreadable to group/other (but the manual page doesn't state that explicitly).  
If that's true you might want to add a line "umask 077;" before the su/cp, or before the "cp" within  the quotes.

0
NVRAMCommented:
>Also, if my memory is correct, the file needs to be unreadable to group/other (but the manual page doesn't state that explicitly).
This refers to the ~/.procmailrc file
0
ahoffmannCommented:
> .. if my memory is correct, ..
NVRAM needs a new battery, sometimes ;-)
*SCNR*
0
NVRAMCommented:
Ah... indeed.  It's fetchmail that has that restriction, not procmail.
D'oh!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.