I am trying to write a very simple shell script in Linux. The Scenario I am trying to achieve is:
Creating an user account and giving it a password internally (No interaction with user allowed for this). Once successful, Just display a success message to the user. None of the rest should be diplayed.
On RHEL this turned out to be pretty simple as passwd command accepts the --stdin option which helped me pass the input via Pipe. And I redirected the standard output in a log file. [Not sharing that code as it's tad simple and explained multiple times here for different question on StackExcahnge.]
However, Ubuntu is not accepting the --stdin option for passwd hence I had to use the below method:
#!/bin/bash
useradd -m demo &>path/to/logfile.log
passwd demo &>>path/to/logfile.log << EOF
myPasswd
myPasswd
EOF
echo "User demo successfully added to the system."
Now The problem here is my output does not print the echo statement and also displays a warning:
root@ubuntu:~# ./usercreate.sh
./usercreate.sh: line 8: warning: here-document at line 3 delimited by end-of-file (wanted `EOF')
root@ubuntu:~#
instead of my expected one:
root@ubuntu:~# ./usercreate.sh
User demo successfully added to the system.
root@ubuntu:~#
Please help me with these two things:
- Is there a way to suppress that warning for this script only?
- Why is it not printing my echo statement and how can I print it?
PS: Another way or idea to write this script is also welcome
Thanks
When using HEREDOC notation, you must write the closing word (
EOF
in your case) in a line by itself, without any leading or trailing whitespace.But in this case I would use another approach:
You can use
chpasswd
. This command is shipped in all linux distros (that I know of) and I think in UNIX variants too.The usage is pretty simple, you just pass via the STDIN a
username:password
combination and that's all, one combo per line. To change the password for a single user, just do a simple echo: