简体   繁体   中英

How to write a unix shell script for

Program shall ask the secret key to run the program; a user should program this secret key in advance. If the user enters correct secret key it should move to next step (3), else it should prompt to enter correct key for five times and then exit the program

You may be trying to implement something like this

#!/bin/bash

secretKey="qwerty1234"
failcount=5
success=0

while [ $failcount -gt 0 ]
do
    echo "please enter secretKey"
    read inp
    if [ $inp = $secretKey ]
    then
        success=1
        break
    else
        ((failcount--))
        echo $failcount" tries remaining"
    fi
done

if [ $success = 0 ]
then
   exit 1
fi

echo "code runs here"

The above code has a preset secret key written into the script (in this case qwerty1234)

The program loops 5 times as determined by the failcount variable. If the password is entered correctly, the success variable is set to 1 and the code executes.

If the password is incorrect 5 times, the loop ends with the success variable as 0.

This causes the program to exit with error code 1

Hope this helped, however please try to provide a clearer example, possibly with some basic implementation in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM