agubaira
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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).
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).
ASKER
typo: The user I am trying to switch to is "adele"...
> 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)
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)
whoami will show you the user you are logged into as the 'su' only lasts as long as the script runs.
ASKER
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 ?
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 ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>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
This refers to the ~/.procmailrc file
> .. if my memory is correct, ..
NVRAM needs a new battery, sometimes ;-)
*SCNR*
NVRAM needs a new battery, sometimes ;-)
*SCNR*
Ah... indeed. It's fetchmail that has that restriction, not procmail.
D'oh!
D'oh!
su $1 -c "cp /full/path/to/.procmailrc /home/$1" && echo File copied