Link to home
Start Free TrialLog in
Avatar of Paul Wills
Paul WillsFlag for South Africa

asked on

shell script autocomplete path

Hello experts


I am currently working on a script that tails a log file.
I use read -e -p "somesomething.." DIR_PATH

when I hit tab it start listing from the directory the script is executing. I want to specify the starting point of autocomplete to /opt/ and not the script directory. The script will be used by mulitiple users and some are notorious, and known for their ways.  Alternatively if they is a ways of excluding certain directory from being listed during autocomplete will be a bonus.
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

If  (and only if) you're using bash you can add something like this to your shell initialization file (.bashrc or /etc/profile for a system-wide setting):

_comp () {
    START_DIR=/opt
    IFS=$'\n' tmp=( $(compgen -W "$(ls "$START_DIR")" -- "${COMP_WORDS[$COMP_CWORD]}" ))
    COMPREPLY=( "$START_DIR/${tmp[@]// /\ }" )
}
complete -F _comp
myscript

Change "myscript" to the actual name of your script.
Avatar of Paul Wills

ASKER

Hi woolmilkporc

Thank you for your quick response I am not using bash, but rksh.
There is no programmable autocompletion feature in (r)ksh, unfortunately.

How did you manage to make the TAB key work in ksh?
Avatar of skullnobrains
skullnobrains

regardless the script, if you do have a working autocomplete in whatever directory, you can simply change directory and autocompletion will occur wherever you need.

excluding files from an autocomplete is a pain when feasible. it might be easier to list the files in your script and prompt the user for the file he wants to use ?

btw i understand little of the problem without seeing the script. where does autocompletion take place ? can we see th script ? you tail files using read ? in a loop with a prompt ?????
@skullnobrains: Good points, but "cd" is not allowed in rksh.
sorry, i should have read the man page before i posted

maybe sticking this in the shebang would help ?

#!sh -c "cd /opt ; ksh -r"

Open in new window


deb@deb:~$ sh -c "cd /opt ; ksh -r"
$ ksh[1]: /dev/null: restricted
pwd
/opt

Open in new window

Hi, apologies for the late response. I am currently using rksh whic restricts cd. See below the script
sysadmin-v1
SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your script runs under bash, not rksh.

If you can start it from your shell (be it rksh or something else) you can also run "cd" inside.
@skullnobrains - I will also try that option, The files I intend to list are log files on quite a number of solaris zones. Not all log files are in the same location, as this is based on the application server install path.

@woolmilkporc - The script is executed by users with rksh the script is located in mounted directory made available to all users. The Path of the directory containing the script is set inside the user's .profile. I add commands which the user can execute using sim links. I have two directories one for users who are admin (these can execute most commands), and then I have users who are readonly and they can only view log files. The users are not able to cd as they are restricted because of the shell rksh.
I meant to say that you can add "cd $GF_DIR" to your script. The user does not have to enter it manually!

Once the script is started it runs under bash (hence the TAB autocompletion which is not implemented in (r)ksh) and "cd" will work.

If it's not always the same directory you can pass it to the script as a parameter (referenced by $1).

/bin is in the PATH of the restricted users, otherwise the script would throw an error.
going the menu way, you may consider using something like this

file=$(dialog --stdout --menu "please choose log file" 100 100 100 `find LOG_FILE_LOCATIONS -printf '"%p" "%p"'`)

Open in new window


not sure wether rksh will allow it as there seem to be quite a few differences depending on versions. woolmilkporc's aparently does not do autocompletion, yours does, and mine attemps to but it is completely unusable
@skullnobrains:

>> yours does ... << 

Please note the shebang line in the author's script - the script runs under bash, and as long as the script can be started at all it doesn't matter which interactive shell the user comes from.

We have the problem here that programmable autocompletion is not supported for readline(), so one of your solutions ("cd" or "menu") is probably the way to go.
@wool :
i did miss the shebang, thanks, and i assume it is actually used in the author's setup. if that is the case cd would work, but since the files are in many directories, that will likely not help much. i believe this illustrates why restricted shells are both a pain for the average user/admin, and not actually very usefull security-wise

if dialog is unavailable or not something the author wants to use, it is pretty easy to code a file list that gets filtered as the user types the beginning of the item, but implementing actual autocompletion for the shell in shell is definitely a pain

it should also be rather easy to update dynamically a directory of links or symlinks to the available log files, cd to that directory and use autocompletion. if the directory structure reflects something meaningfull like ./service_type/app_name/log_name.date.log, autocompletion could bring a menu-like user experience
Hi

I will go with a menu listing. I save my results in a text file and read from the text file.
The file test.txt contains the string to be read.

test.txt
===============
-Dcom.sun.aas.defaultLogFile=/opt/instances/nodeagents/sivz1312_NA01/sivz1312_NA01_AS01/logs/server.log
argv[24]: -Dcom.sun.aas.defaultLogFile=/opt/instances/nodeagents/sivz1312_NA01/sivz1312_NA01_AS02/logs/server.log
argv[3]: -Dcom.sun.aas.defaultLogFile=/opt/instances/nodeagents/sivz1312_NA01/agent/logs/server.log
argv[24]: -Dcom.sun.aas.defaultLogFile=/opt/instances/nodeagents/sivz1312_NA01/sivz1312_NA01_AS03/logs/server.log

I have added the following to the script

for i in `ps -ef |grep instanceRoot | awk '{ print $2 }'`; do echo $i | grep opt; pargs $i | grep defaultLogFile; done > instancelog_`hostname`.txt

outputs the server instance log path

for GF_LOG in `cat instancelog_"hostname".txt | sed -e "s/.*defaultLogFile=//"`; do echo $GF_LOG; done

The above outputs the content of instancelog_hostname.

My question is - I want to list the contents of this file as a menu option, like select.

How would I achieve this?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all, I believe you all played an important role in cracking this one. I will split the points. I have taken the route of displaying a menu to select from and it works well.

Once again thank you for all your help