通过替换 PAM 文件中的文本来更改密码策略的 Shell 脚本

标签 shell scripting newline

我正在编写一个 shell 脚本,它将使用 Ubuntu 14.04 中的 PAM 文件完全更改密码策略。我这样做的方式是

sudo sh -c 'echo "I want to change this text" >/etc/pam.d/example.txt'

我遇到的问题是我无法创建新行。我研究过这个,有人说 HTML 标签 <br>有效,但我已经尝试过

sudo sh -c 'echo "I want to change this text. <br> This should be a new line." >/etc/pam.d/example.txt 

(和)

sudo sh -c 'echo "I want to change this text. <br /> This should be a new line." >/etc/pam.d/example.txt'

但它只打印 <br><br />文本文件上的标签。我该如何解决这个问题?

最佳答案

基本上,您应该停止使用 echo 来编写您正在编写的任何新内容。

虽然 POSIX 描述了 echo 的行为,但实际上它并不像应有的那样可移植。 echo 的 POSIX 文档甚至建议“鼓励新应用程序使用 printf 而不是 echo。

您还可以找到对此 here on the S.E. network 的引用。和the bash hackers' wiki touches on this tooone of the bash pitfalls 中提到了它.

sudo sh -c 'printf "First line.\nSecond line.\n" >/etc/pam.d/example.txt'

关于通过替换 PAM 文件中的文本来更改密码策略的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838970/

相关文章:

php - 使用来自 PHP/CGI 的 shell 命令是不好的做法吗?

linux - 用于从另一个文件的 block 组成文件的 shell 命令

scripting - 测试人员使用的脚本语言

SAS:输入文本文件时指定换行符

Redis 在 SET 中回车或换行

python - 换行符/代码\r\n\r\n 显示在文本之前

linux - 嵌入式 Linux shell (BusyBox) 检查文件列表是否存在并运行命令

linux - 在 linux shell 脚本中向 csv 文件中的列添加/Sub/Mul/Div 常量

bash - bash shell中的find命令和-name选项疑惑

ios - 覆盖 .xcassets 中的文件