Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In a small bash script I'm running I am attempting to chown a new directory that is created. I've added:

sudo chown $USER:$USER /var/www/$sitename
sudo chmod 775 /var/www/$sitename

after the line where I mkdir (sudo mkdir /var/www/$sitename).

For some reason the chown is not executing. I can execute it manually but when written in the file it doesn't work. I have noticed that "chown" is not highlighted in the same color as "mkdir" and "chmod" but I can't figure out my problem.

Why doesn't chown work here?

Is it an issue with $USER:$USER?

EDIT Here is the full script. How would I chown the file to whichever non root user executed the script?

#!/bin/sh
#!/bin/bash
# New Site

cd /etc/apache2/sites-available/
echo "New site name (test.my):"
read sitename
echo "<VirtualHost *:80>

        ServerAdmin admin@$sitename

    ServerName $sitename

        ServerAlias $sitename

    DocumentRoot /var/www/$sitename

        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/$sitename>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined


</VirtualHost>" > $sitename.conf
sudo mkdir /var/www/$sitename
sudo chown $USER:$USER /var/www/$sitename
echo USER is $USER
sudo chmod 775 /var/www/$sitename
sudo a2ensite $sitename.conf
sudo apachectl restart
echo "New site created"
share|improve this question
    
Is there a group named $USER? getent group $USER –  Cyrus Feb 5 at 20:20
1  
$USER variable is set during interactive login. How do you run your script - from login session or using cron or from daemon? –  myaut Feb 5 at 20:31
1  
Check if the USER variable is even seen by the script. If you add a line to your script that says echo USER is $USER, what does it print out? –  Mark Plotnick Feb 5 at 22:12
    
@MarkPlotnick Evidently, USER is root. With the edit I've made do you think you can explain how to chown the file to whichever non-root user executes the script? –  BrassApparatus Feb 7 at 9:41
    
@myaut I run it manually from the terminal. –  BrassApparatus Feb 7 at 9:44

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.