Bash call another script depending on “read” input

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Bash call another script depending on “read” input



I am trying to learn about making simple bash scripts to do things on my computer just because I want to learn because I think it is interesting (and I can also think of uses down the track).



I am trying to write a script that assigns variables that will call another script depending on what I type. I have managed to call another script from a variable using the below:


#!/bin/bash
Echo hello please choose you next step
VBA="/Users/zap/VBA.sh"
$VBA



but now I want to be able to call one script or another depending on user input, and I have tried to make the script below, so that if when I type VBA in the "read" section it runs one script and if I type in VBB in it runs a different script. But it seems that this does not work how do I need to change the syntax to make the out put run with the script VBA or VBB?


#!/bin/bash
Echo hello please choose you next step
VBA="/Users/zap/VBA.sh"
VBB="/Users/zap/VBB.sh"
read IPT
NXT="$"$IPT""
echo $NXT



If I can make this work I will turn this into a simple script that runs sudo shutdown and then asks me if I want to shut down immediately (I think -h) or restart (I think -r).


sudo shutdown


-h


-r




1 Answer
1



There are a few options.



1) You could use an if statement and explicitly run the command you want:


echo Do you want to reboot (Y/N)?
read IPT
if [ "$IPT" = "Y" ] ; then
shutdown -h now
else
shutdown -r now
fi



2) you can try to force the user to type the command. It could be dangerous if you sudo
since the user could type something you don't want to run.


echo do you want to run VBA or VBB?
read IPT
"/Users/zap/$IPT.sh"





Thank you, I did both of these to learn - and have ultimately used the "if" version also putting in a variable to change the time of when it happens.
– Mangus
Aug 7 at 23:43






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS