LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 01-01-2017, 05:14 AM   #1
ShadyThGod
LQ Newbie
 
Registered: Jan 2017
Distribution: Kali Linux
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 01-01-2017, 06:16 AM   #2
wpeckham
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

Rep: Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816
Quote:
Originally Posted by ShadyThGod View Post
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.

Let us see if that much gets you started.
 
Old 01-01-2017, 06:49 AM   #3
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
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.
Old 01-01-2017, 09:43 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by ShadyThGod View Post
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.
 
Old 01-01-2017, 11:01 AM   #5
wpeckham
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

Rep: Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816
Quote:
Originally Posted by ondoho View Post
...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!
 
Old 01-03-2017, 04:49 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by wpeckham View Post
Code:
if ( "$command" == "help" ) then
bash chokes on the missing ';' (or newline) before 'then'...
 
Old 01-03-2017, 05:39 AM   #7
wpeckham
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

Rep: Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816
Quote:
Originally Posted by ondoho View Post
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. ;-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script - command works directly in command line but not in script raoulcousins Linux - Newbie 6 08-21-2013 07:43 PM
[SOLVED] ls command inside bash script error sopier Programming 11 12-17-2011 10:19 PM
[SOLVED] Command line works, won't work from inside bash script w6lsn Programming 4 02-17-2011 05:45 AM
create a global associative array variable inside a function of a bash script konsolebox Programming 3 07-14-2009 06:08 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration