![]() |
|
[Shell] [Bash Script] Go back to menu, when an option is selected - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Shell] [Bash Script] Go back to menu, when an option is selected (/showthread.php?tid=6022986) |
[Bash Script] Go back to menu, when an option is selected - friiz1337 - Nov 7, 2019 Hello guys Down here I have a code that lets a user login into a system, as well as create an account. My question is: How can I loop this same code every time the user creates an account (chooses option 2, specifies username and password, and then shows the same menu(in case the user wants to create another account)). I know bash doesn't have a goto option so I tried wrapping this whole piece of code into a function, but without luck (I miss something) . :/ Thanks for the help. Code echoRE: [Bash Script] Go back to menu, when an option is selected - m3rkill - Nov 8, 2019 how about making the script start itself again instead of quit? RE: [Bash Script] Go back to menu, when an option is selected - netWalker - Nov 12, 2019 Here's how I'd do it. Code #!/bin/bashOf course you could also just make the entire thing a while loop. Something like this: Code #!/bin/bashA few side notes unrelated to your question. Do what you want with this, just my opinion. 1) The "echo" commands There's an unnecessary number of them. It makes it not very pretty to read. I'm not judging, I used to do the same thing. Instead you should use something like this: Code echo -e "Make account\n\n"Code echo "Make account"2) Make a prompt for "read." You can give the read command a prompt by using this: read -p "Select an option: " option. Just a cosmetic thing really. Purely optional. 3) Might as well make that "case" statement an if-else. You really only have 3 possible options. Login, make account, and wtf ever else the retarded user could put in. This is again a personal preference thing, but it just seems a bit unnecessary. You could just make it a if-else. Code #!/bin/bashRE: [Bash Script] Go back to menu, when an option is selected - friiz1337 - Nov 13, 2019 (Nov 12, 2019 02:52 PM)netWalker Wrote: Here's how I'd do it. Wow, thanks for the great answer, I will modify my code and get back to this thread. As for the tips, thank you, I'm sort of new to programming so anything is helpful. RE: [Bash Script] Go back to menu, when an option is selected - netWalker - Nov 14, 2019 (Nov 13, 2019 10:26 AM)friiz1337 Wrote: Wow, thanks for the great answer, I will modify my code and get back to this thread. As for the tips, thank you, I'm sort of new to programming so anything is helpful. Glad I could help. Good luck with your script. RE: [Bash Script] Go back to menu, when an option is selected - friiz1337 - Jan 11, 2020 So, it's been a while since my last reply, however I managed to solve my problem. I just used the exec "$0" command right after my "register" option loop. This allows the program to restart without creating additional processes, after the user has created an account. I also changed the case with if-elif's. Another option that was pretty close to solving my problem was using break 2/3 (depending on your loops). Thank you for the help. RE: [Bash Script] Go back to menu, when an option is selected - FantaŠec - Feb 3, 2020 If you’re still looking to improve this, can you post an updated version? You shouldn’t have to use exec at all to leverage this functionality (exec isn’t usually ideal for a case like this too). RE: [Bash Script] Go back to menu, when an option is selected - AnonMouse - Feb 6, 2020 this would be cool to use if you could make it smaller RE: [Bash Script] Go back to menu, when an option is selected - friiz1337 - Feb 6, 2020 (Jan 11, 2020 03:34 AM)friiz1337 Wrote: So, it's been a while since my last reply, however I managed to solve my problem. I just used the exec "$0" command right after my "register" option loop. This allows the program to restart without creating additional processes, after the user has created an account. I also changed the case with if-elif's. I actually updated this by using functions lol. Calling a function works like a charm |