Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 798  / 1 Year ago, mon, december 26, 2022, 1:52:01

I ran the below code through shellcheck myscript and keep getting this error. How can I correct it?


****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

More From » bash

 Answers
2

Try this I think I have the flow correct.


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

The output when you run it and the contents of the directory afterwards.


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

[#1881] Monday, December 26, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
polcomposte

Total Points: 421
Total Questions: 92
Total Answers: 109

Location: Uzbekistan
Member since Mon, Jul 20, 2020
4 Years ago
polcomposte questions
;