gpt4 book ai didi

上次添加的 Linux 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 10:58:16 26 4
gpt4 key购买 nike

我是 Linux 脚本的新手,被这个练习困住了。我需要查明当前用户是否是最后添加的用户。如果没有,那么我需要显示最后添加的用户。

这是我的脚本:

#!/bin/bash --

lastUser ='tail -1 /etc/passwd | cut -d: -f1'

if [[ $lastUser -ne $USER ]]; then
echo "I am $USER and I am not the last added user."
echo "The last added user is $lastUser."
else
echo "I am $USER and I am the last added user."
fi

我得到的错误:第 3 行:lastUser:找不到命令。

我做错了什么?

最佳答案

这应该可以解决问题:

lastUser=$(tail -1 /etc/passwd | cut -d: -f1)

if [ "$lastUser" != "$USER" ]; then
echo "I am $USER and I am not the last added user."
echo "The last added user is $lastUser."
else
echo "I am $USER and I am the last added user."
fi

lastUser 是您提供的代码中的一个字符串,它不执行 cmd 。另外,不要忘记不要在 =

周围放置空格

关于上次添加的 Linux 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27985420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com