shell脚本将无法识别符定界符 [英] Shell script won't recognize heredoc delimiter

查看:236
本文介绍了shell脚本将无法识别符定界符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写在Linux的一个非常简单的shell脚本。我想实现的方案是:

I am trying to write a very simple shell script in Linux. The Scenario I am trying to achieve is:

创建用户帐户,并给它一个内部密码(与用户允许对这种无交互)。一旦成功,仅仅显示一条成功信息给用户。其余的都不应该是diplayed。

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.

在RHEL这竟然是pretty简单passwd命令接受--stdin选项,它帮助我通过管道传递输入。而且我重定向到日志文件的标准输出。 [不共享该code,因为它是稍微简单,这里就StackExcahnge不同的问题解释多次。]

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.]

不过,Ubuntu是不接受passwd中--stdin选项,因此我不得不使用下面的方法:

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."

现在这里的问题是我输出不打印echo语句,并显示警告:

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. 有没有办法用晚餐preSS的警告只有这个脚本?

  2. 为什么不打印我的回声声明,我怎么能打印呢?

PS:另一种方法或思路来写这个剧本也是欢迎的。

PS: Another way or idea to write this script is also welcome

感谢

推荐答案

在使用定界符记号,必须在一行写(在你的情况 EOF )的收盘字本身没有任何开头或结尾空白。

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:

您可以使用 chpasswd的。此命令运在所有的Linux发行版(我知道的),我想在UNIX变体了。

You can use chpasswd. This command is shipped in all linux distros (that I know of) and I think in UNIX variants too.

用法是pretty简单,您只需通过STDIN传递用户名:密码组合,这一切,每行一个组合。要为单个用户更改密码,只是做了一个简单的回声:

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

这篇关于shell脚本将无法识别符定界符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆