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.
jw124210Asked:
Who is Participating?
 
woolmilkporcConnect With a Mentor Commented:
This can be done with select.

export PS3="Please enter your choice: "
select choice in $(cat instancelog_$(hostname).txt | sed -e "s/.*defaultLogFile=//")
  do
    echo "selected: " $choice
    # your processing here
    break
  done
0
 
woolmilkporcCommented:
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.
0
 
jw124210Author Commented:
Hi woolmilkporc

Thank you for your quick response I am not using bash, but rksh.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
woolmilkporcCommented:
There is no programmable autocompletion feature in (r)ksh, unfortunately.

How did you manage to make the TAB key work in ksh?
0
 
skullnobrainsCommented:
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 ?????
0
 
woolmilkporcCommented:
@skullnobrains: Good points, but "cd" is not allowed in rksh.
0
 
skullnobrainsCommented:
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

0
 
jw124210Author Commented:
Hi, apologies for the late response. I am currently using rksh whic restricts cd. See below the script
sysadmin-v1
0
 
skullnobrainsConnect With a Mentor Commented:
why don't you list the files that match your criteria, and ask the user which one he wants to work on ?
0
 
woolmilkporcCommented:
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.
0
 
jw124210Author Commented:
@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.
0
 
woolmilkporcCommented:
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.
0
 
skullnobrainsCommented:
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
0
 
woolmilkporcCommented:
@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.
0
 
skullnobrainsCommented:
@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
0
 
jw124210Author Commented:
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?
0
 
jw124210Author Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.