提到的语法错误出现在这个 while 循环中

提到的语法错误出现在这个 while 循环中

我通过 shellcheck myscript 运行了以下代码,但一直出现此错误。我该如何纠正它?

****Line 48:
 while [ $count - lt 3 ]

 ^-- SC1009: The mentioned syntax error was in this while loop.
       ^-- SC1073: Couldn't parse this test expression. Fix to allow more checks.**
                ^-- SC1076: Trying to do math? Use e.g. [ $((i/2+7)) -ge 18 ].
                 ^-- SC1072: Expected a test operator. Fix any mentioned problems and try again.**
 #!/bin/bash
 touch.bash_profile 
  mkdir login.sh 
  chmod + x login.sh 
  echo "Enter Your Username." 
  read user 
  echo "Enter Your Password." 
 read pass 
# to check blank inputs
  if [ -z "$user" ] ||
  [ -z "$pass" ] 
 then 
 echo 'Inputs cannot be blank. Please retry.' 
# Checking entered username and password with actual credentials
# if matched
    if [ "$Username" = "Alpha" ] ||
    [ "$Password" = "Beta" ] ;
then 
 pwd 
  else
# if not matched
      count = 1 
 while [ $count - lt 3 ]
      do
if["$Username" != "$user"]
      ||["$Password" != "$pass"];
then 
 left = 3 - $count 
 echo b

答案1

尝试一下这个我认为我的流程正确。

#!/bin/bash
# Create the files .bash_profile and login.sh in the current directory making the login.sh executable.

touch .bash_profile 
touch login.sh 
chmod +x login.sh 

# Tell them what needs to be done

echo "This script requires you to enter the Username and Password, you have three tries at it."

# set the count to 0
count=0
while [ $count -lt 3 ] ; do

    # Check for Username

    echo "Enter Your Username:" 
    read user 
    if [ -z "$user" ] ; then
        echo "You must enter a Username"
    fi
    
    # Check for Password

   echo "Enter Your Password:" 
    read pass 
    if [ -z "$pass" ] ; then
        echo "You must enter a Password"
    fi
    
    #checking entered username and password with actual credentials

    #if matched $Username and $Password

    if [ "$user" = "Alpha" ] && [ "$pass" = "Beta" ] ; then
         pwd 
         exit 0

    else

    #if not matched

        let "count+=1" 
        echo "This is try number $count "
    fi
done

运行时的输出以及之后目录的内容。

root@buster-raspi:~/test# ./test.sh
This script requires you to enter the Username and Password, you have three tries at it.
Enter Your Username
test
Enter Your Password
trtet
This is try number 1 
Enter Your Username
test
Enter Your Password
rsrseres
This is try number 2 
Enter Your Username
dsddsf
Enter Your Password
gfdggdf
This is try number 3 
root@buster-raspi:~/test# ./test.sh
This script requires you to enter the Username and Password, you have three tries at it.
Enter Your Username
test
Enter Your Password

You must enter a Password
This is try number 1 
Enter Your Username
root@buster-raspi:~/test# ./test.sh
This script requires you to enter the Username and Password, you have three tries at it.
Enter Your Username
Alpha
Enter Your Password
Beta
/root/test
root@buster-raspi:~/test# ls -l
total 4
-rwxr-xr-x 1 root root   0 Feb 26 22:06 login.sh
-rwxr-xr-x 1 root root 927 Feb 26 22:05 test.sh
root@buster-raspi:~/test# ls -l .bash_profile 
-rw-r--r-- 1 root root 0 Feb 26 22:11 .bash_profile
-rwxr-xr-x 1 root root 927 Feb 26 22:05 test.sh

答案2

我建议写代码的时候要整洁有条理。这里有一份指南,不是强制性的,但是是一个很好的起点。

谷歌搜索 gnu 手册,了解正确的语法等。搜索可以是“bash gnu 手册构造”,对我来说第一个结果这是

关于您的代码,请遵循我的建议:

#!/bin/bash
touch.bash_profile 
mkdir login.sh 
chmod + x login.sh 
echo "Enter Your Username" 
read user 
echo "Enter Your Password" 
read pass 
#to check blank inputs
if [ -z "$user" ] || [ -z "$pass" ]; then 
    echo 'Inputs cannot be blank. Please re-try.' 
    #checking entered username and password with actual credentials
    #if matched
    if [ "$Username" = "Alpha" ] || [ "$Password" = "Beta" ];then 
        pwd 
    else
        #if not matched
        count=1 
        while [ $count -lt 3 ]; do
            if [ "$Username" != "$user" ] || [ "$Password" != "$pass" ]; then
                left=$((3 - $count ))
                echo b
            fi
        done
    fi
fi

这已经通过了shellcheck语法。

相关内容