Solved

shell script autocomplete path

Posted on 2013-05-21
17
586 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
Comment Utility
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
Comment Utility
Hi woolmilkporc

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

Expert Comment

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

How did you manage to make the TAB key work in ksh?
0
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
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
Comment Utility
@skullnobrains: Good points, but "cd" is not allowed in rksh.
0
 
LVL 26

Expert Comment

by:skullnobrains
Comment Utility
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
Comment Utility
Hi, apologies for the late response. I am currently using rksh whic restricts cd. See below the script
sysadmin-v1
0
 
LVL 26

Assisted Solution

by:skullnobrains
skullnobrains earned 55 total points
Comment Utility
why don't you list the files that match your criteria, and ask the user which one he wants to work on ?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
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
Comment Utility
@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
Comment Utility
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 26

Expert Comment

by:skullnobrains
Comment Utility
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
Comment Utility
@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 26

Expert Comment

by:skullnobrains
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now