Here’s a bash script that meets your requirements:
```bash #!/bin/bash
Function to check if a program has a password
has_password() { program=$1 password_file=”.${program}_password”
if [ -f "$password_file" ]; then
return 0
else
return 1
fi }
Function to get password from user
get_password() { program=$1 password_file=”.${program}_password”
echo "Enter password for $program:"
read -s password
echo "$password" > "$password_file"
echo "Password set successfully!" }
Function to validate password
validate_password() { program=$1 password_file=”.${program}_password”
echo "Enter password for $program:"
read -s password
stored_password=$(cat "$password_file")
if [ "$password" == "$stored_password" ]; then
return 0
else
return 1
fi }
Function to change password
change_password() { program=$1 password_file=”.${program}_password”
echo "Enter new password for $program:"
read -s new_password
echo "$new_password" > "$password_file"
echo "Password changed successfully!" }
Main script
program=”firefox”
if has_password “$program”; then if validate_password “$program”; then echo “Password is correct.” echo “1. Change password” echo “2. Run $program”
read -p "Enter your choice: " choice
case $choice in
1)
change_password "$program"
;;
2)
echo "Running $program..."
# Add command to run the program here
;;
*)
echo "Invalid choice!"
;;
esac
else
echo "Incorrect password. Exiting..."
exit 1
fi else
echo "No password set for $program."
echo "1. Run $program"
echo "2. Set password"
read -p "Enter your choice: " choice
case $choice in
1)
echo "Running $program..."
# Add command to run the program here
;;
2)
get_password "$program"
;;
*)
echo "Invalid choice!"
;;
esac fi ```
To use this script, save it to a file (e.g., program_runner.sh), make it executable (chmod +x program_runner.sh), and then run it (./program_runner.sh). Replace firefox with the name of the program you want to run.
Note: This script assumes that the program’s password is stored in a hidden file named .program_password in the same directory as the script. You can modify the script to change the password file location if needed.
Loading...