Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
01-01-2017, 05:14 AM
|
#1
|
LQ Newbie
Registered: Jan 2017
Distribution: Kali Linux
Posts: 1
Rep:
|
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.
|
|
|
01-01-2017, 06:16 AM
|
#2
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,922
|
Quote:
Originally Posted by ShadyThGod
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 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 statement. It should be well covered in the bash documentation.
Let us see if that much gets you started.
|
|
|
01-01-2017, 06:49 AM
|
#3
|
Member
Registered: May 2016
Posts: 222
Rep:
|
There are a couple of things not doing what you expect them to do.
Look here, third example
http://mywiki.wooledge.org/BashGuide..._and_.5B.5B.29
Round the lines of this should be good:
Code:
#!/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
|
|
1 members found this post helpful.
|
01-01-2017, 09:43 AM
|
#4
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
Quote:
Originally Posted by ShadyThGod
Code:
read $command
if ( "$command" == "help" ) then
echo "Welcome to the Help Page"
elif ( "$command" == "clear" ) then
clear
elif ( "$command" == "exit" ) then
exit
fi
|
...in other words, this is not a bash script.
|
|
|
01-01-2017, 11:01 AM
|
#5
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,922
|
Quote:
Originally Posted by ondoho
...in other words, this is not a bash script.
|
In fact, if I remember correctly this line run in a bash script
Code:
if ( "$command" == "help" ) then
will execute the command
Code:
"$command" == "help"
in a subshell and return the exit code to if to determine what to do next. I strongly suspect that is not your intent.
I could be wrong, but I believe you need to spend a lot more time looking at the man pages and examples available using google!
|
|
|
01-03-2017, 04:49 AM
|
#6
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
Quote:
Originally Posted by wpeckham
Code:
if ( "$command" == "help" ) then
|
bash chokes on the missing ';' (or newline) before 'then'...
|
|
|
01-03-2017, 05:39 AM
|
#7
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,922
|
Quote:
Originally Posted by ondoho
bash chokes on the missing ';' (or newline) before 'then'...
|
I liked your comment
Quote:
...in other words, this is not a bash script.
|
better. There is nothing wrong there except the syntax and the logic. ;-)
|
|
|
All times are GMT -5. The time now is 07:23 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|