Упомянутая ошибка синтаксиса была в этом во время цикла [Закрыто]

Я провел код ниже через MyScript Shellcheck и продолжайте получать эту ошибку. Как я могу исправить это?

****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
-2
задан 27 February 2021 в 09:27

2 ответа

Попробуйте, я думаю, что поток правильный.

#!/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
1
ответ дан 18 March 2021 в 23:30

Я предлагаю быть аккуратным и организованным, чтобы писать код. Здесь у вас есть руководство , не обязательное, но хорошее место для начала.

Погуглите руководство gnu, чтобы узнать правильный синтаксис и все такое. Поиск может быть "bash gnu manual constructs", и для меня первый результат - это .

Что касается Вашего кода, и следуя моим предложениям:

#!/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.

0
ответ дан 18 March 2021 в 23:30

Другие вопросы по тегам:

Похожие вопросы: