我听说printf
比echo
. 根据我的经验,我只能回忆起一个我不得不使用的实例,printf
因为它echo
无法将一些文本输入到 RHEL 5.8 上的某个程序中,但printf
确实如此。但显然,还有其他差异,我想询问它们是什么以及是否有特定情况下何时使用一种与另一种。
在grep
您可以使用--group-separator
在组比赛之间写一些东西。
这有助于清楚我们有哪些块,尤其是在使用-C X
选项获取上下文行时。
$ cat a
hello
this is me
and this is
something else
hello hello
bye
i am done
$ grep -C1 --group-separator="+++++++++" 'hello' a
hello
this is me
+++++++++
something else
hello hello
bye
Run Code Online (Sandbox Code Playgroud)
我在使用空行作为 grep 的上下文“组分隔符”中学到了如何只拥有一个空行,说--group-separator=""
.
但是,如果我想有两个空行怎么办?我试着说,--group-separator="\n\n"
但我得到了字面意思\n
:
$ grep -C1 --group-separator="\n\n" 'hello' a
hello
this is me
\n\n
something else
hello hello
bye
Run Code Online (Sandbox Code Playgroud)
其他类似的东西--group-separator="\nhello\n"
也不起作用。
input() {
read -p $'\e[31m\e[1m $1 [Y/n] \e[0m' -n 1 -r
}
input "test"
exit
Run Code Online (Sandbox Code Playgroud)
这只是打印“$1”作为文本行。为什么不打印“测试”,我怎样才能做到这一点?
我想做的是获取用户输入并搜索文件,如果匹配,它将显示结果,否则它将回显 $keyword not found。
但我的票据总是返回错误。
read -p "Enter keyword: " keyword
if search="$(cat ./records | grep '$keyword')"
then
echo "$search"
else
echo "$keyword not found!"
fi ;;
Run Code Online (Sandbox Code Playgroud) 在创建新的 mysql 用户和数据库之前,我做了:
read sps -s
# Inputing and saving the sps (password) value.
echo ${sps}
# sps password value echoed.
mysql -u root -p[PASSWORD]
mysql>
CREATE user "test"@"localhost" IDENTIFIED BY "${sps}";
Run Code Online (Sandbox Code Playgroud)
当我开始登录时,mysql -u test -p[SPS_PASSWORD_VALUE]
出现访问被拒绝的错误。
密码不是我给出的值,${sps}
而是它${sps}
本身,作为一个 string。
为了“正式”证明问题是由于${sps} variable not being expanded **inside** mysql shell, and therefore acts as a regular string, I created another user
test1` 手动提供了密码(不是来自变量),这次我可以正常登录。
为什么当我在 mysql shell 中时,没有变量扩展,我怎样才能防止这种行为或至少获得接近这种行为的行为?
也许这是一个错误?