Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I use a batch script to create other scripts?

I'm creating a script that I want to be dual-purpose:

  1. The script will use user-input options to execute commands to change some settings. This part is nearly done.

    • The script is collecting the user's choices into local environment variables via SETLOCAL/ENDLOCAL and SET /P, and calling the variables later in the script for command execution.

    • After collecting the user options, there is a verification step that displays those options and asks the user for confirmation before proceeding to command execution.

  2. The script will give the user an option to automatically create a new script with their options built-in for future convenience.

Here's my current plan concept to achieve objective #2, though I'm not exactly sure how to write it:

  1. Build a template script that can be copied to start off creation of the customized scripts.

    • At the very start of the template script, SETLOCAL will isolate environment variables created by the finalized script so they do not persist after the script is finished running.

    • Immediately after SETLOCAL will be a GOTO command which points to a label at the very end of the script. The label will be the start of the variable definitions section in completed scripts.

    • After the GOTO command will be a label called STARTEXEC that will be jumped to after variable definitions are complete.

    • Just before the variables section label will be a labeled section with some command(s) to exit the script.

  2. Into the current script:

    • Add a CHOICE during the verification step that asks the user if they'd like to create a static script. If yes, prompt the user for a filename via SET /P then include the script building commands in this script's execution flow via GOTO.
       
  3. Use some commands similar to these, to build the static script's variables section:

    COPY ScriptTemplate.bat %UserFileName%.bat
    ECHO SET VAR1=%UserVar1% >> %UserFileName%.bat
    ECHO SET VAR2=%UserVar2% >> %UserFileName%.bat

  4. Add one more line to make sure the static script jumps to command execution.

    ECHO GOTO STARTEXEC >> %UserFileName%.bat

My questions here:

  1. Are my methods fairly sound, or is there a better way to do this, still via batch script?

  2. What should I use to exit the static script, before it comes back around to the variables section? I know I'll need an ENDLOCAL. Will EXIT work here, or is there something else more appropriate for this?

like image 541
Iszi Avatar asked Nov 14 '22 09:11

Iszi


1 Answers

The template idea seems to be the easy method (not sure about short, but this is probably how I would've approached it.) Create a separate template based on the original, removing all of the SET /P commands.

EDIT:

Removed the old answer.

At the end of the template, write:

GOTO:EOF

:User_Defined_Variables

At the beginning of the file, write:

CALL:User_Defined_Variables

In your original script, after the user confirms creating a custom script, make a copy of the template file COPY /y template.bat userscript.bat send all of the user's variables to it.

(ECHO var1 = %uservar1% 
 ECHO var2 = %uservar2%
 Echo var3 = %uservar4%)>> userscript.bat
GOTO:EOF

So when the user runs the script, the CALL:User_Defined_Variables will set the user's script parameters and continue on with the rest of the script.

like image 190
Anthony Miller Avatar answered Dec 25 '22 09:12

Anthony Miller