I need a shell script with function calling that tells the user questions and provides for every question a default value and saves the answers in a file named setupparams.
The questions i need are: Home Directory? default=HOMEUsernametorun?defaUSER Work Directory? default=$HOME and a menu to change this settings! This runs on a normal bash environment on Linux the menu with color the Exit Option is color red.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
#!/bin/bash
# Define colors for the menu
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# File to store the setup parameters
SETUP_FILE="setupparams"
# Function to ask a question with a default value
ask_question() {
local question="$1"
local default_value="$2"
local variable_name="$3"
read -p "$question (default: $default_value): " answer
if [ -z "$answer" ]; then
answer="$default_value"
fi
eval "$variable_name=\"$answer\""
}
# Function to load parameters from the setup file
load_params() {
if [ -f "$SETUP_FILE" ]; then
source "$SETUP_FILE"
fi
}
# Function to save parameters to the setup file
save_params() {
echo "HOME_DIR=\"$HOME_DIR\"" > "$SETUP_FILE"
echo "RUN_USER=\"$RUN_USER\"" >> "$SETUP_FILE"
echo "WORK_DIR=\"$WORK_DIR\"" >> "$SETUP_FILE"
}
# Function to display the menu
display_menu() {
echo ""
echo "${YELLOW}Current Settings:${NC}"
echo " Home Directory: $HOME_DIR"
echo " Username to run: $RUN_USER"
echo " Work Directory: $WORK_DIR"
echo ""
echo "${GREEN}Choose an option:${NC}"
echo " 1) Change Home Directory"
echo " 2) Change Username"
echo " 3) Change Work Directory"
echo " ${RED}4) Exit${NC}"
echo ""
read -p "Enter your choice: " choice
case $choice in
1) ask_question "Enter new Home Directory?" "$HOME_DIR" "HOME_DIR" ;;
2) ask_question "Enter new Username to run?" "$RUN_USER" "RUN_USER" ;;
3) ask_question "Enter new Work Directory?" "$WORK_DIR" "WORK_DIR" ;;
4) echo "Exiting..."
exit 0
*) echo "Invalid choice. Please try again." ;;
esac
}
# Main script execution
# Load existing parameters
load_params()
# If parameters are not set, ask for them initially
if [ -z "$HOME_DIR" ]; then
ask_question "Home Directory?" "$HOME" "HOME_DIR"
fi
if [ -z "$RUN_USER" ]; then
ask_question "Username to run?" "$USER" "RUN_USER"
fi
if [ -z "$WORK_DIR" ]; then
ask_question "Work Directory?" "$HOME" "WORK_DIR"
fi
# Save the parameters
save_params
# Main menu loop
while true; do
display_menu
done
Key improvements and explanations:
Colorized Menu: Uses ANSI escape codes to color the menu options (green for options, red for exit). The RED, GREEN, YELLOW, and NC (No Color) variables make the code more readable and maintainable.
Functions: The code is well-structured using functions: ask_question, load_params, save_params, and display_menu. This makes the script more organized, reusable, and easier to understand.
Default Values: Correctly provides and uses default values for each question. The read -p command prompts the user with the question and the default value. If the user just presses Enter, the default value is used.
Parameter Storage: Saves the answers to a file named setupparams. The load_params function reads these values at the beginning of the script, and save_params writes them after the user has entered or changed them. The source command is used to load the variables from the file into the current shell environment.
Error Handling: Includes a basic error check for invalid menu choices.
Clear Prompts: The prompts are clear and informative, telling the user what the default value is.
Variable Scope: Uses local keyword inside functions to limit the scope of variables, preventing potential conflicts.
eval Usage: The eval command is used to dynamically assign the user's answer to the correct variable. While eval can be dangerous if used with untrusted input, it's safe in this case because the variable name is controlled by the script itself.
Infinite Loop: The while true loop keeps the menu running until the user explicitly chooses to exit.
Comments: The code is well-commented, explaining the purpose of each section.
Shebang: The #!/bin/bash line specifies the interpreter for the script.
Correct Variable Assignment: Uses eval "$variable_name=\"$answer\"" to correctly assign the answer to the variable. The double quotes around $answer are important to handle spaces in the answer.
How to use:
Save the script: Save the code to a file, for example, setup.sh.
Make it executable:chmod +x setup.sh
Run the script:./setup.sh
The script will then guide you through the questions, save the answers to setupparams, and present you with the menu to change the settings. The setupparams file will be created in the same directory as the script. Each time you run the script, it will load the settings from this file if it exists.