Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

i'm trying to run this script at start up but this is constantly giving me error that /etc/init.d/put_ubuntu_users: 14: /etc/init.d/put_ubuntu_users: cannot open ~/data.txt: No such file

This is my script

#!/bin/sh

### BEGIN INIT INFO
# Provides:          Updates Password at Login
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: script for updating password
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

                while IFS=: read f1 f2
                        do
                                user=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 1);
                                pwd=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 2);

                                        if [ -n "$user" -a "$f2" != "$pwd" ]; then
                                                #search for password, and repalce it with new one
                                                sudo sed -i 's@'$pwd'@'$f2'@' /etc/shadow
                                        fi

                done < ~/data.txt

i had run these set of commands

sudo chmod 755 put_ubuntu_users
sudo update-rc.d put_ubuntu_users defaults
sudo service put_ubuntu_users start

BTW, this script is working normally if i manually run it by typing ./put_ubuntu_users

Please tell me ehat i'm doing wrong

share|improve this question

4 Answers 4

up vote 1 down vote accepted

The error is telling you it cannot find the file ~/data.txt. The reason is simple: /etc/init.d scripts are run as root, and I am pretty sure there is no file data.txt in /root, root's home. You can fix that by using absolute paths, not relative ones.

On the other hand, I find it difficult to understand what a redirection is doing after a done statement. What exactly are you meaning to do?

share|improve this answer
    
Thanks its work! :) actually i'm replacing password of user in /etc/shadow –  Fatima Zohra Nov 28 '13 at 2:18

Chances are that there is no home folder set up for the init process user (which is normal).

At the start of your script, add setuid myuser, which will run the script as that user. Currently the init user doesn't have a home folder set up (a guess, otherwise it'd write to the home folder location). A surprisingly useful Wikipedia page about this.

share|improve this answer

Services don't have home directories. So ~/data.txt doesn't do what you think it does in that context. You need to put the file somewhere you reference with a static file name.

share|improve this answer

The error is saying the file data.txt (for the users home directory) can't be found/accessed. The easiest way to fix this is to edit the last line of the script and change the ~/data.txt to /home/{username_here}/data.txt

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.