Today, I was required to write a shell script which will detect a directory which has a random name-ending (Eg. version-YQEG$1200 etc), enter it and run some operations inside that directory. When we work manually in commandline, this is easy because since we know that the directory begins with 'version' we type version and hit 'tab' key which autocompletes it. But how to do that in a shell script ?
That is when this command compgen came to my rescue.
So lets have a look at a command that I did not know hitherto: compgen
The compgen builtin command expands a list of arguments to generate completion matches, so it allows us to display the commands, functions, directories and aliases available for the current user. In short compgen is a command that displays commands.
So compgen is a command builtin in the bash shell so if you use this shell you have this command available in the terminal,
Let’s see how we can use it directly from the command line, as first thing let’s say that in general compgen has different options that you can use to specify which kind of thing you are looking for, so the basic usage is:
compgen [option] [word]
This generate possible completion matches for word according to the options, which may be any option accepted by the complete
builtin with the exception of -p and -r, and write the matches to the standard output let’s see some examples:
Working with directories
The option for directories is -d so compgen -d
will give as output the list of all the directory in the current path, or you can ask for a specific path such as:
xubuntu-home:~$ compgen -d /etc/s /etc/sudoers.d /etc/sysctl.d /etc/sensors.d /etc/skel /etc/samba /etc/sane.d /etc/ssh /etc/speech-dispatcher /etc/systemd /etc/sgml /etc/ssl /etc/sound /etc/smartmontools /etc/snmp /etc/security |
In this case I’ve asked compgen to show all the directory in /etc/ that start with the letter s, using just /etc/ as parameter it will show all the directories in /etc/.
Working with commands
In the same way compgen can also show all the available commands for an user with the flag -c : compgen -c
if you run this command probably you’ll get a list of some thousands of commands so it’s better to use it with a | less
or to show only some commands use an additional parameter that represents a part of the word you are looking for:
xubuntu-home:~$ compgen -c ss sshd ssgrep ssh-vulnkey ssindex ssconvert ssh-import-id ssh ssh-copy-id ssh-argv0 ssh-keygen ssh-askpass ssh-add ssh-keyscan ssh-agent ss |
In this example I can see all the commands that i can use that start with ss
Other Functions
This is a short list of options that you can use with compgen:
Show all the bash built-ins commands.
compgen -b
Show all the files and directories in current path (or specify a path like in the directory)
compgen -f
Show all the bash keywords
xubuntu-home:~# compgen -k if then else elif fi case esac for select while until do done in function time { } ! [[ ]] coproc |
Show all the bash functions
compgen -A function
Show all the alias available for the current user
xubuntu-home:~$ compgen -a vps egrep fgrep grep ls |
Show all the usernames defined in the server:
xubuntu-home:~# compgen -u s sys sync syslog speech-dispatcher saned sshd sslh |
Show all the groups defined in the server:
xubuntu-home:~# compgen -g p proxy plugdev pulse pulse-access postfix postdrop |
Excellent web site you've got here.. It’s hard to find good quality writing like yours these days.
ReplyDeleteI honestly appreciate people like you! Take care!!
Very good info. Lucky me I found your website by chance (stumbleupon).
ReplyDeleteI've book-marked it for later!
Great site you have here.. It’s difficult to find high quality writing
ReplyDeletelike yours these days. I really appreciate individuals like you!
Take care!!
I'd like to thank you for the efforts you have
ReplyDeleteput in penning this site. I'm hoping to see the same high-grade content by you
in the future as well. In truth, your creative
writing abilities has motivated me to get my own, personal site now ;)
This is the perfect webpage for everyone who wishes to find out about this topic.
ReplyDeleteYou realize so much its almost hard to argue with you
(not that I really will need to…HaHa). You definitely
put a fresh spin on a topic that has been written about for ages.
Excellent stuff, just excellent!
Oh my goodness! Incredible article dude! Thank you so much, However I am encountering issues with your
ReplyDeleteRSS. I don’t understand why I can't subscribe
to it. Is there anybody else getting identical RSS issues?
Anyone who knows the answer will you kindly respond? Thanks!!
I like reading through an article that will make men and women think.
ReplyDeleteAlso, thank you for allowing for me to comment!
Good article. I certainly appreciate this website. Stick with it!
ReplyDeleteGreetings! Very helpful advice within this article!
ReplyDeleteIt is the little changes that will make the most important
changes. Thanks a lot for sharing!
I have to thank you for the efforts you have put in writing this blog.
ReplyDeleteI really hope to check out the same high-grade blog posts from you in the future
as well. In fact, your creative writing abilities
has encouraged me to get my own website now ;)
I really love your blog.. Excellent colors & theme.
ReplyDeleteDid you build this web site yourself? Please reply back as I’m looking to
create my very own site and would love to
learn where you got this from or exactly what the theme is called.
Thank you!
I was able to find good advice from your blog posts.
ReplyDeleteI seriously love your site.. Pleasant colors & theme.
ReplyDeleteDid you develop this site yourself? Please reply back as I’m looking to create my own personal
blog and would like to find out where you got this
from or what the theme is called. Thanks!
I love looking through an article that can make men and
ReplyDeletewomen think. Also, many thanks for permitting me to comment!
I really like reading through a post that will make people
ReplyDeletethink. Also, thanks for permitting me to comment!
Very nice blog post. I definitely appreciate this site.
ReplyDeleteKeep it up!
Spot on with this write-up, I honestly believe this web site needs a lot more attention. I’ll
ReplyDeleteprobably be back again to read more, thanks for the info!
You are so interesting! I don't believe I've read anything
ReplyDeletelike this before. So nice to find someone with original thoughts on this subject.
Seriously.. many thanks for starting this up. This website is something that is required on the internet,
someone with a little originality!
This is a great tip especially to those fresh to the blogosphere.
ReplyDeleteShort but very accurate info… Thank you for sharing this one.
A must read article!
May I simply say what a comfort to find somebody who genuinely knows what they are talking
ReplyDeleteabout online. You definitely understand how to bring an issue to light and make it
important. More people should look at this and understand this side of
your story. I was surprised you're not more popular since
you definitely possess the gift.
Your style is really unique compared to other folks I've read stuff from.
ReplyDeleteThanks for posting when you have the opportunity, Guess I'll just bookmark this site.
Very good post. I am going through many of these issues
ReplyDeleteas well..
bookmarked!!, I like your website!
ReplyDeleteHi, I do believe this is an excellent site. I stumbledupon it ;) I'm going to return yet again since I
ReplyDeletebook-marked it. Money and freedom is the best way to change,
may you be rich and continue to guide others.
This is the right website for anyone who hopes to understand this
ReplyDeletetopic. You realize so much its almost hard to argue with you (not that I personally would want to…HaHa).
You definitely put a fresh spin on a topic that's been discussed for decades.
Wonderful stuff, just wonderful!
There's definately a lot to learn about this topic. I like all the points you made.
ReplyDeleteExcellent post. I absolutely appreciate this site.
ReplyDeleteThanks!
There is certainly a lot to find out about this subject.
ReplyDeleteI like all of the points you've made.
Good article. I will be going through a few of these issues as well..
ReplyDeleteIt’s nearly impossible to find educated people for this topic,
ReplyDeletebut you seem like you know what you’re talking about! Thanks
Wonderful article! We are linking to this great content on our website.
ReplyDeleteKeep up the great writing.
This page truly has all of the info I wanted about this subject and didn’t know who to ask.
ReplyDeleteAw, this was an incredibly good post. Taking the time
ReplyDeleteand actual effort to produce a good article… but what can I say… I
hesitate a lot and don't seem to get anything done.
Oh my goodness! Incredible article dude! Thank you so much, However I am encountering issues with your RSS.
ReplyDeleteI don’t understand the reason why I am unable
to join it. Is there anybody having the same RSS problems?
Anyone that knows the solution will you kindly respond?
Thanx!!
Hello! I simply want to offer you a huge thumbs up for the great info you have here on this post.
ReplyDeleteI am returning to your site for more soon.