Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

shell script autocomplete path

Posted on 2013-05-21
17
Medium Priority
?
628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 220 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 220 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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 …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

636 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