I need a shell script with function calling that tells the user questions and provides…
- User
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=USER 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.
- Assistant
Here's a shell script that meets your requirements. It includes a menu-driven interface with colored output, default values for each question, and the ability to save the answers to a file named
setupparams:#!/bin/bash # Configuration file CONFIG_FILE="setupparams" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Default values DEFAULT_HOME="$HOME" DEFAULT_USER="$USER" DEFAULT_WORK="$HOME" # Current values (will be loaded from config or set to defaults) CURRENT_HOME="" CURRENT_USER="" CURRENT_WORK="" # Load configuration if exists load_config() { if [ -f "$CONFIG_FILE" ]; then source "$CONFIG_FILE" # Set current values from config or use defaults CURRENT_HOME=${HOME_DIR:-$DEFAULT_HOME} CURRENT_USER=${USERNAME:-$DEFAULT_USER} CURRENT_WORK=${WORK_DIR:-$DEFAULT_WORK} else # If no config file, use defaults CURRENT_HOME=$DEFAULT_HOME CURRENT_USER=$DEFAULT_USER CURRENT_WORK=$DEFAULT_WORK fi } # Save configuration save_config() { echo "HOME_DIR=\"$CURRENT_HOME\"" > "$CONFIG_FILE" echo "USERNAME=\"$CURRENT_USER\"" >> "$CONFIG_FILE" echo "WORK_DIR=\"$CURRENT_WORK\"" >> "$CONFIG_FILE" chmod 600 "$CONFIG_FILE" echo -e "${GREEN}Configuration saved to $CONFIG_FILE${NC}" } # Ask question with default value ask_question() { local question="$1" local default="$2" local var_name="$3" local current_value="${!var_name}" echo -e "${BLUE}$question${NC}" read -p " [${YELLOW}$current_value${NC}]: " answer if [ -z "$answer" ]; then answer="$current_value" fi # Update the variable declare -g "$var_name=$answer" } # Change settings change_settings() { echo -e "\n${GREEN}=== Change Settings ===${NC}" ask_question "1. Home Directory?" "$DEFAULT_HOME" "CURRENT_HOME" ask_question "2. Username to run?" "$DEFAULT_USER" "CURRENT_USER" ask_question "3. Work Directory?" "$DEFAULT_WORK" "CURRENT_WORK" save_config } # Show current settings show_settings() { echo -e "\n${GREEN}=== Current Settings ===${NC}" echo -e "Home Directory: ${YELLOW}$CURRENT_HOME${NC}" echo -e "Username to run: ${YELLOW}$CURRENT_USER${NC}" echo -e "Work Directory: ${YELLOW}$CURRENT_WORK${NC}" echo -e "Config file: ${YELLOW}$(pwd)/$CONFIG_FILE${NC}" } # Main menu main_menu() { while true; do echo -e "\n${BLUE}=== Main Menu ===${NC}" echo -e "1. Change settings" echo -e "2. Show current settings" echo -e "3. Save settings" echo -e "${RED}4. Exit${NC}" read -p "Choose an option [1-4]: " choice case $choice in 1) change_settings ;; 2) show_settings ;; 3) save_config ;; 4) echo -e "${RED}Exiting...${NC}"; exit 0 ;; *) echo -e "${RED}Invalid option!${NC}" ;; esac done } # Initialize load_config # Start the menu main_menuFeatures:
- Default Values: Each question has a default value (HOME, USER, and current work directory)
- Configuration File: Saves answers to
setupparamsfile - Menu System:
- Change settings (update any of the three values)
- Show current settings
- Save settings
- Exit (colored red)
- Color Coding:
- Blue for questions and menu titles
- Yellow for default/current values
- Green for success messages
- Red for exit option and error messages
Usage:
- Save the script as
setup.sh - Make it executable:
chmod +x setup.sh - Run it:
./setup.sh - Follow the menu options to configure your settings
The script will create the
setupparamsfile in the same directory where you run the script. The file will contain the configuration in a format that can be sourced by other scripts if needed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.