编辑Bash脚本以显示输出

时间:2021-11-27 13:58:16

I have a script that cycles through several servers and if a particular user is found it will output their name. I'd like to modify the individual statements so that if a user isn't found 'NO USER IDENTIFIED' is output.

我有一个循环遍历多个服务器的脚本,如果找到特定用户,它将输出他们的名字。我想修改单个语句,以便在未找到用户时输出“NO USER IDENTIFIED”。

Here is an individual code snippet:

这是一个单独的代码段:

echo "environment1" sshpass -p $ldappw ssh $ldapuser@12.34.567.65 'mysql -h website.com -u topuser -ppassword dbname-e "select concat(FIRST,LAST) from users;"' | grep -i ${username}

I would appreciate the assistance of the community in modifying the statement to give me the desired output.

我希望社区的帮助可以修改声明,以便为我提供所需的输出。

1 个解决方案

#1


0  

One simple solution is to just write this:

一个简单的解决方案就是写下这个:

echo "environment1" sshpass -p $ldappw ssh $ldapuser@12.34.567.65 'mysql -h website.com -u topuser -ppassword dbname-e "select concat(FIRST,LAST) from users;"' | (grep -i ${username} || echo "NO USER IDENTIFIED")

Explanation

As Etan points it out, grep returns failure if no lines are selected. This is what the grep manual says on that:

正如Etan指出的那样,如果没有选择行,grep会返回失败。这就是grep手册中所说的:

The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2.

如果找到选定的行,则退出状态为0,如果未找到,则退出状态为1。如果发生错误,则退出状态为2。

Bash evaluates the right side of || only if the left side was false. As the bash manual says:

Bash评估了||的右侧只有左侧是假的。正如bash手册所说:

The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.

&&和||如果expression1的值足以确定整个条件表达式的返回值,则运算符不会计算expression2。

So if grep cannot select a row which matches ${username}, echo will do the job.

因此,如果grep无法选择与$ {username}匹配的行,echo将完成这项工作。

Notes

  • Consider selecting only the desired user select concat(FIRST,LAST) from users where concat(FIRST,LAST) like '%${username}%;'. With this trick you can do an exact match before grep (just remove % characters from the like).
  • 考虑只从用户那里选择所需的用户选择concat(FIRST,LAST),其中concat(FIRST,LAST)就像'%$ {username}%;'。有了这个技巧,你可以在grep之前做一个完全匹配(只需从中移除%字符)。

  • Consider using the -N (Do not write column names in results.) and the -s (Silent mode.) parameters of mysql, so it produces less output.
  • 考虑使用-N(不写结果中的列名)和mysql的-s(静默模式。)参数,因此它产生的输出较少。

#1


0  

One simple solution is to just write this:

一个简单的解决方案就是写下这个:

echo "environment1" sshpass -p $ldappw ssh $ldapuser@12.34.567.65 'mysql -h website.com -u topuser -ppassword dbname-e "select concat(FIRST,LAST) from users;"' | (grep -i ${username} || echo "NO USER IDENTIFIED")

Explanation

As Etan points it out, grep returns failure if no lines are selected. This is what the grep manual says on that:

正如Etan指出的那样,如果没有选择行,grep会返回失败。这就是grep手册中所说的:

The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2.

如果找到选定的行,则退出状态为0,如果未找到,则退出状态为1。如果发生错误,则退出状态为2。

Bash evaluates the right side of || only if the left side was false. As the bash manual says:

Bash评估了||的右侧只有左侧是假的。正如bash手册所说:

The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.

&&和||如果expression1的值足以确定整个条件表达式的返回值,则运算符不会计算expression2。

So if grep cannot select a row which matches ${username}, echo will do the job.

因此,如果grep无法选择与$ {username}匹配的行,echo将完成这项工作。

Notes

  • Consider selecting only the desired user select concat(FIRST,LAST) from users where concat(FIRST,LAST) like '%${username}%;'. With this trick you can do an exact match before grep (just remove % characters from the like).
  • 考虑只从用户那里选择所需的用户选择concat(FIRST,LAST),其中concat(FIRST,LAST)就像'%$ {username}%;'。有了这个技巧,你可以在grep之前做一个完全匹配(只需从中移除%字符)。

  • Consider using the -N (Do not write column names in results.) and the -s (Silent mode.) parameters of mysql, so it produces less output.
  • 考虑使用-N(不写结果中的列名)和mysql的-s(静默模式。)参数,因此它产生的输出较少。