LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-16-2011, 01:24 AM   #1
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Question bash script with own command line interpreter


update: i'm changing this post to a question, i really need help to properly implement TAB completion to make this work (it's part of a bigger framework). If anyone has info on how to get readline+complete/compgen to work when using "read -e", please post it!!!

original post:

Hello, this is a working template for an embedded command interpreter in a bash script. Given the scarce material i've seen on this i'm posting as a time saver for everyone.

It needs bash 4 with readline to work properly. Has command line edition, history and tab completion (provided by readline).

It also has automatic listing and validation of user commands based on the functions defined. If you want a function to NOT be accesible to the user, just use a "_" as the first character of its name (of course, this can be altered as needed).

i invite you to use/expand/comment etc.

Code:
#!/bin/bash

# funcs ------------------------------------------------------------------------
cmd1() {
    echo $FUNCNAME: "$@"
}

cmd2() {
    echo $FUNCNAME: "$@"
}

cmd3() {
    echo $FUNCNAME: "$@"
}

_complete() {
    echo $allowed_commands
}

# start ------------------------------------------------------------------------
shopt -qs extglob

set -o emacs

bind 'set show-all-if-ambiguous on'
bind 'set completion-ignore-case on'
bind 'TAB:dynamic-complete-history'

unset -v HISTFILE
allowed_commands="$(declare -f | sed -ne '/^[^_].* () /s/ ().*//p' | tr '\n' ' ')"
for command in $allowed_commands ; do
    history -s $command
done

echo "waiting for commands"
while read -ep"-> "; do
    history -s $REPLY
    case "$REPLY" in
        @(${allowed_commands// /|})?(+([[:space:]])*)) $REPLY ;;
        \?) _complete ;;
        *) echo "invalid command: $REPLY" ;;
    esac
done

Last edited by Juako; 01-17-2011 at 05:01 PM. Reason: code reorder
 
Old 01-16-2011, 01:39 AM   #2
Juako
Member
 
Registered: Mar 2010
Posts: 202

Original Poster
Rep: Reputation: 84
Forgot to mention that the technique to get TAB completion is pulled from another LQ thread, thank you!

Anyway, i was hoping to get completion to work with the standard complete/compgen builtins instead of this history hack, but i couldn't for the life of me get those to work in the script whatsoever. I'd very,very much like to hear something about this working, as AFAICS it's truly an ugly part.

In this other LQ thread "complete" is reported as working, though it didn't worked for me.

The other bit i'd like to get some help about is this command substitution:

Code:
allowed_commands="$(declare -f | sed -ne '/^[^_].* () /s/ ().*//p' | tr '\n' ' ')"
The last pipe (that of "tr") seems redundant. I know sed has the ability to transform the newlines to spaces...hopefully some sed warrior sheds some light here.

Last edited by Juako; 01-18-2011 at 12:23 PM.
 
Old 01-19-2011, 04:47 PM   #3
Juako
Member
 
Registered: Mar 2010
Posts: 202

Original Poster
Rep: Reputation: 84
Thumbs down

As detailed in this sister thread, programmable completion in read is disabled in the bash code by the maintainer.

I have contacted the maintainer of Bash, and been toold that this use case (presenting commands to the end user for completion) is the only one presented for programmable completion in the context of read, and it isn't considered compelling enough to worth the effort of implementing it.

This answer left me highly unsatisfied. I do not concord at all with that view, in fact i think that since you have a functionality that is clearly oriented to provide CLI implementations inside bash scripts, it well worths the effort to have it complete.

Last edited by Juako; 01-20-2011 at 09:45 PM.
 
  


Reply

Tags
bash scripting, interpreter, readline


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
Embed expect in bash script prpednek Linux - General 3 05-15-2010 06:36 AM
works on command line but not in bash script tara Linux - General 7 02-09-2009 03:57 AM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
[SOLVED] [bash]difference between script on command line and in file Wim Sturkenboom Programming 4 09-08-2008 02:20 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM


All times are GMT -5. The time now is 03:28 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration