Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试运行命令并将其存储在变量中。
length=`last | grep foouser | wc -l`
它工作正常,但是当我将变量添加到命令时它会中断。
value=$1 length=`last | grep $value | wc -l`
如何通过接受变量来使第二个示例工作?
你实际上并不需要wc:
wc
length=$(last | grep -c "$value")
您可以改进变量名称
num_logins=$(last | grep -c "$username")
你应该正确引用你的变量。如果它们包含空格,您的脚本可能会中断:
value="$1" length="$(last | grep "$value" | wc -l)"