linux - 我的 sh 脚本有问题不知道为什么?

标签 linux bash scripting shell

我在这里写了这个 sh 脚本。它应该做的是提示用户输入旧密码,然后使用“PASSWORD.txt”文件中的密码检查密码,如果不是,它将退出,否则,如果匹配,则要求用户输入输入新密码两次。然后它检查两个新密码是否相同,如果不相同,它将退出,否则我应该将用户输入的输入内容替换为“PASSWORD.txt”文件中的文本。

然后当我运行文件时,它要求我输入旧密码,我得到了这个错误:

Please Enter teh old passsword:
test
cat: .txt: No such file or directory
The password doesn't match![root@guzzy ~]#

问题是我输入的输入不匹配,即使我输入了正确的旧密码。

下面是脚本:

#!/bin/sh

clear

echo -e "Please Enter the old password:"

read old

if [ "$old" != "$(cat $PASSWORD.txt)" ]

    then

        echo -n "The password doesn't match!"

        exit

    else

        echo -n "The old password matches!"

        echo -n "Please Enter New password:"

        read new1

        echo -n "Please Enter New password again:"

        read new2

        if [ "$new1" != "$new2" ]

            then

                echo -n "The new passwords don't match!"

                exit

            else

                $new1 >> PASSWORD.txt

                echo -n "The new password has been saved!"

        fi
fi

请帮忙谢谢!

最佳答案

这一行:

$new1 >> PASSWORD.txt

应该是这样的:

echo "$new1" > PASSWORD.txt

您需要将值回显 到文件中。我假设您不想保留旧值。为了能够在同一个文件上再次运行您的脚本,您应该覆盖 (>>) 而不是追加 (>>)。

关于linux - 我的 sh 脚本有问题不知道为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3823087/

相关文章:

C++线程意外打印同一行多次

bash - 如何在文件中用一个替换 2 个新行

python - Xcode 中的 LLDB Python 脚本

regex - sed 命令替换大括号内的数据

bash - 如何重新定位每行的第三个字符串以从同一行的第 n 个字符开始

linux - 用户正在使用 bash 运行的进程数

c - fcntl : Turning disk buffering on and off

java - 从 java 程序运行我的 mongodb 命令

linux - vfork() 之后如何恢复父级

linux - 如何查找非官方安装的软件包?