Solved

shell script autocomplete path

Posted on 2013-05-21
17
601 Views
Last Modified: 2013-05-27
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.
0
Comment
Question by:jw124210
  • 7
  • 5
  • 5
17 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39183934
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
 

Author Comment

by:jw124210
ID: 39183941
Hi woolmilkporc

Thank you for your quick response I am not using bash, but rksh.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39183960
There is no programmable autocompletion feature in (r)ksh, unfortunately.

How did you manage to make the TAB key work in ksh?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:skullnobrains
ID: 39187027
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39187044
@skullnobrains: Good points, but "cd" is not allowed in rksh.
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 39187157
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
 

Author Comment

by:jw124210
ID: 39188254
Hi, apologies for the late response. I am currently using rksh whic restricts cd. See below the script
sysadmin-v1
0
 
LVL 27

Assisted Solution

by:skullnobrains
skullnobrains earned 55 total points
ID: 39188404
why don't you list the files that match your criteria, and ask the user which one he wants to work on ?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39188428
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
 

Author Comment

by:jw124210
ID: 39188456
@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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39188472
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
 
LVL 27

Expert Comment

by:skullnobrains
ID: 39190204
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39190232
@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
 
LVL 27

Expert Comment

by:skullnobrains
ID: 39190339
@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
 

Author Comment

by:jw124210
ID: 39193559
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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 55 total points
ID: 39193626
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
 

Author Closing Comment

by:jw124210
ID: 39199331
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linux SFTP 8 76
linux 13 99
HOw To Install Docker on VMware Workstation 19 309
Disabling security updates Ubuntu 3 43
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question