相关疑难解决方法(0)

为什么 printf 比 echo 好?

我听说printfecho. 根据我的经验,我只能回忆起一个我不得不使用的实例,printf因为它echo无法将一些文本输入到 RHEL 5.8 上的某个程序中,但printf确实如此。但显然,还有其他差异,我想询问它们是什么以及是否有特定情况下何时使用一种与另一种。

echo text-processing printf

635
推荐指数
4
解决办法
27万
查看次数

如何在grep中使用多行作为组分隔符?

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"也不起作用。

shell grep quoting

12
推荐指数
1
解决办法
1万
查看次数

为什么 $1 在 $'...' 中使用时不起作用?

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”作为文本行。为什么不打印“测试”,我怎样才能做到这一点?

bash

3
推荐指数
1
解决办法
507
查看次数

if 语句与 grep

我想做的是获取用户输入并搜索文件,如果匹配,它将显示结果,否则它将回显 $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)

linux scripting bash shell-script

2
推荐指数
1
解决办法
4万
查看次数

当 INSIDE mysql shell 时变量不会被扩展

在创建新的 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 usertest1` 手动提供了密码(不是来自变量),这次我可以正常登录。

问题

为什么当我在 mysql shell 中时,没有变量扩展,我怎样才能防止这种行为或至少获得接近这种行为的行为?

也许这是一个错误?

shell bash mysql database variable-substitution

-1
推荐指数
1
解决办法
1826
查看次数