bash - 理解 bash 代码中的输出的问题
问题描述
我在 bash 中有以下代码:
#!/bin/sh
w=`who | grep $1`
if [ -z "$w" ]; then
echo "$1 ... ";
fi
你能帮我理解这段代码的输出吗?我可以看到它将显示命令行中的给定参数($1,$2...)
但我真的不明白这两行:
w=`who | grep $1`
if [ -z "$w" ]; then
你能给我一些帮助吗,因为我找不到太多信息
解决方案
Looks to me a script to check if the username you pass as parameter to the script is logged on the system.
For example if you call it with ./check.sh nonexistinguser
it will print nonexistinguser ...
.
Explanation:
w=`who | grep $1`
Execute who
, which displays who is logged in, then grep
the result for whatever parameter you passed to the script ($1
), store the result in a variable called w
; have a look here for the docs on -z
if [ -z "$w" ]; then
If "$w"
has length zero, execute the body of the if.
推荐阅读
- handlebars.js - 如何使用 registerHelper 在 handlebars.js 中使用 if 条件
- firebase - Flutter Firebase Auth Exception 的错误代码是什么?
- azure-service-fabric - IAsyncEnumerator 项目变为空
- java - 在 spock 中交换号码测试用例
- laravel - Laravel 类别、子类别和模型绑定
- java - Spring Kafka:kafkaTemplate executeInTransaction 方法如何发挥 Consumer 的 read_committed 隔离级别
- git - 如何处理未提交的更改
- javascript - 通过自动更改所有函数和变量名称来掩盖生产中的反应代码
- excel - IF 函数 Excel 中的多个 OR 语句
- vue.js - 如何以编程方式打开/关闭锁定纵横比