• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

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
0
agubaira
Asked:
agubaira
  • 3
  • 3
  • 3
  • +1
2 Solutions
 
TintinCommented:
#!/bin/sh
su $1 -c "cp .procmailrc /home/$1"
0
 
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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
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

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now