Is it possible to create a custom command line inside a BASH script?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is it possible to create a custom command line inside a BASH script?
I was wondering if it was possible to create a custom command-line in BASH script. I tried using the read command to take input and use IF/ELIF statements to compare the input and run functions according to them but I can only use real bash commands. For eg -
This is what i'm currently trying to work with:
Code:
read $command
if ( "$command" == "help" ) then
echo "Welcome to the Help Page"
elif ( "$command" == "clear" ) then
clear
elif ( "$command" == "exit" ) then
exit
fi
The clear and exit work properly but help starts the bash help command instead of echo-ing what i asked.
I was wondering if it was possible to create a custom command-line in BASH script. I tried using the read command to take input and use IF/ELIF statements to compare the input and run functions according to them but I can only use real bash commands. For eg -
This is what i'm currently trying to work with:
Code:
read $command
if ( "$command" == "help" ) then
echo "Welcome to the Help Page"
elif ( "$command" == "clear" ) then
clear
elif ( "$command" == "exit" ) then
exit
fi
The clear and exit work properly but help starts the bash help command instead of echo-ing what i asked.
Bash troubleshooting is an art, but not one difficult to learn.
I assume these lines are in a script. If they are not, put them in one.
1. Check the command
Code:
set -x
or use the #! line with options -xv to bash. You will want to revert these after you fix your issues. The output of the script when using this option needs to be captured to a file for later examination. The output will contain the commands, parameters, and output all together: lending itself to proper analysis.
2. Using commands that are bash internal makes your scripts more prone to issues. There are more conditions where internals are detected and executed, though there are ways to avoid this. Just a thought.
3. You may want to read about the
Code:
case
statement. It should be well covered in the bash documentation.
#!/bin/bash
read command
if [[ "$command" = "help" ]]; then
echo "Welcome to the Help Page"
elif [[ "$command" = "clear" ]]; then
clear
elif [[ "$command" = "exit" ]]; then
exit
fi
read $command
if ( "$command" == "help" ) then
echo "Welcome to the Help Page"
elif ( "$command" == "clear" ) then
clear
elif ( "$command" == "exit" ) then
exit
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.