1

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:

  1. Is there a way to suppress that warning for this script only?
  2. 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

tripleee
  • 175,061
  • 34
  • 275
  • 318
theHeman
  • 505
  • 1
  • 6
  • 26
  • Like the error message says, you have used `EOD` where you think you had `EOF`. You seem to have corrected this error when you transcribed the code -- maybe next time, use copy+paste to make sure you really show us your exact code. – tripleee Feb 27 '15 at 06:42
  • Swap `&>>path/to/logfile.log` and `<< EOF`. – Cyrus Feb 27 '15 at 06:43
  • But I don't think your workaround attempt will work -- a here document is also received on standard input. – tripleee Feb 27 '15 at 06:43
  • @Cyrus no, redirections can occur in any order. – tripleee Feb 27 '15 at 06:44
  • @tripleee, sorry my bad. I have two almost identical (Only EOD and EOF changed where I though EOF might me causing issues) scripts running. Pasted the warning of second script. Will correct that immediately. – theHeman Feb 27 '15 at 06:46
  • @Cyrus yes, I tried that. Redirection order does not matter. I got the identical output. – theHeman Feb 27 '15 at 06:49
  • @tripleee so any idea how I can make this work on Ubuntu? – theHeman Feb 27 '15 at 06:50
  • 1
    Do you have any space after the closing `EOF`?. It should be the only word in the line, with no leading or trailing spaces. – Carlos Campderrós Feb 27 '15 at 07:26
  • Damn!! That's awesome @CarlosCampderrós !! That did the trick!! Thanks man!! If you just write that as an answer, I can accept it as correct one :) I am using the Vim editor hence I missed that space. – theHeman Feb 27 '15 at 07:30
  • @ashhem done, with a suggestion on how to solve this problem easily too :) – Carlos Campderrós Feb 27 '15 at 07:33
  • 1
    @tripleee thank you for the more apt title :) – theHeman Feb 27 '15 at 07:41
  • See also http://stackoverflow.com/questions/18660798/here-document-delimited-by-end-of-file – tripleee Apr 04 '16 at 18:05

1 Answers1

1

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:

echo "demo:myPasswd" | chpasswd
Carlos Campderrós
  • 22,354
  • 11
  • 51
  • 57
  • Thanks! Did the trick. Actually the chpasswd proved more helpful as I need to run my script across various distros and need to have a uniform one!! – theHeman Feb 27 '15 at 07:38