[Discussion/Diversion] Convenience shell tips and tricks for Slackware users.
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[Discussion/Diversion] Convenience shell tips and tricks for Slackware users.
There was a post by allend in the "requests for current" thread not too long ago where they had asked about adding some Slackware specific convenience shell functions to the distro.
That idea may be difficult to implement as a standard for all users, but I thought it would make an interesting discussion thread in any case. I'm always interested to learn new things and see what tricks other Slackware users have cooked up. I'd be curious to see what some candidates for such a list might look like anyways!
In the spirit of the holiday cheer and sharing , I thought it might be nice to have a thread where we can share bash tips/tricks, convenience functions, or other neat .bashrc and profile script tricks that Slackers use to make life easier. (other shell users are welcome of course, but my experience is with bash)
I can start with a couple Slackware package functions I've been using in my .bashrc:
The 'pkggrep' function just greps from all packages in the PKGDIR. It passes options to grep as well so you can do things like 'pkggrep -i <foo>' for case insensitive searches for example. I use this when I want to find what package something on my system belongs to.
The 'pkgcat' function just 'cats' packages from the PKGDIR. I find I commonly do something like 'cat /var/lib/pkgtools/packages/<some package>' when I want to examine some package's contents. This function does that, just a little lazier. It also uses bash completion so that you can <tab> complete package names.
Share em' if you have em' and happy holidays to all!
My ~/.bashrc was getting a bit muddled so I split it into separate files that I source and edit with vim tabs.
~/.bashrc
Code:
# source individual .config/bash.d files
if [ -d ~/.config/bash.d ]; then
for file in ~/.config/bash.d/*.sh; do
[[ -r $file ]] && . $file;
unset file
done
else
echo -e "\t\t\tYou do not have a ~/config/bash.d directory"
fi
example list of files in ~/.config/bash.d
alias.sh
backups.sh
bash-config.sh
colors.sh
export.sh
functions.sh
packages.sh
vim-shortcuts.sh
in vim-shortcuts.sh I added an alias so that the sourced files would be read in vim tabs
Code:
alias vmb="cd ~/.config/bash.d && vim -p *.sh"
In ~/.vimrc I mapped keys to make using tabs a bit easier
Code:
" shortcuts for tabs, previous tab, next tab, close current, close all,
" remove highlight of search patterns
nnoremap <F8> :tabprevious<CR>
nnoremap <F9> :tabnext<CR>
nnoremap <F10> :tabc<CR>
nnoremap <F11> :qall<CR>
nnoremap ,<space> :nohlsearch<CR>
The 'pkgcat' function just 'cats' packages from the PKGDIR. I find I commonly do something like 'cat /var/lib/pkgtools/packages/<some package>' when I want to examine some package's contents. This function does that, just a little lazier. It also uses bash completion so that you can <tab> complete package names.
Nice idea! (my .bashrc is just filled with aliases and shopt -s extglob)
For pkgcat, why not just use cat directly?
Code:
pkgcat () {
cat $PKGDIR/$@
}
Edit: oh I see, for keeping it working when package full path is given as argument
Nice idea! (my .bashrc is just filled with aliases and shopt -s extglob)
For pkgcat, why not just use cat directly?
Code:
pkgcat () {
cat $PKGDIR/$@
}
Edit: oh I see, for keeping it working when package full path is given as argument
I did it that way so that I could tab complete multiple packages to pkgcat and they would all be spit out. With the way you posted, only the first one gets 'cat' properly when the $@ is expanded.
shellcheck reported 2 errors in this small script.
Using ( ) is not required at all and forking a new shell just for a cat is just wasting resources....
Code:
pkgcat() {
pushd $PKGDIR
cat "$@"
popd
}
would be much better, but it is still not perfect....
I did it that way so that I could tab complete multiple packages to pkgcat and they would all be spit out. With the way you posted, only the first one gets 'cat' properly when the $@ is expanded.
Code:
pkgcat () {
cat "${@/#/$PKGDIR/}"
}
this works with bash string manipulation
Code:
${string/#substring/replacement}
If $substring matches front end of $string, substitute $replacement for $substring.
so here it replaces start of each $@ element with "$PKGDIR/"
Just a habit when I write functions in bash. Doesnt hurt does it?
True, but its not a function that is getting used intensively and it spits out the packages in < 1 second so I was fine with the inefficiency
However, I like the alternative keefaz offered and it has no shellcheck issues so I updated my .bashrc with that.
Wasting resources is a bad habit. It does not hurt as long as your box has no real task to do. You can do what you want for yourself, but for such a community, this is not an example to follow
Wasting resources is a bad habit. It does not hurt as long as your box has no real task to do. You can do what you want for yourself, but for such a community, this is not an example to follow
Surely you are now just trolling. This is not the 1950's where every bit was needed. This thread's topic mentions "convenience" not "I have a 256 MB RAM computer and still want to run Slackware-current". Give us all a break.
And don't be a mouth for "the community" please. You don't get to talk for me.
However, I like the alternative keefaz offered and it has no shellcheck issues so I updated my .bashrc with that.
It's not bullet proof though, some care is needed for the case when a full path is given as argument as it blindly adds $PKGDIR path to anything.
Also I think the double quotes are not needed and they actually block the asterisk expansion if you would run something like: pkgcat ht* (but then ShellCheck complains about missing quotes to avoid re-splitting elements, not relevant with this usage though)
Surely you are now just trolling. This is not the 1950's where every bit was needed. This thread's topic mentions "convenience" not "I have a 256 MB RAM computer and still want to run Slackware-current". Give us all a break.
And don't be a mouth for "the community" please. You don't get to talk for me.
That is a quite interesting approach. This is the MS way, to put more and more hardware into the box to make the software acceptable. And I still don't like it. You can't write high quality software with that. And don't distribute low quality software for any community. I don't talk for you, but against this approach. This makes the distribution unstable and unreliable in the long run (and will require more and more hardware to run smoothly).
And there is an additional issue: others may use these scripts as examples for their own tasks and only learn and spread bad habits (such as wasting resources) instead of doing something useful
Mainly because it doesn't cost more to write better code, and also using better solutions are always much more convenient.
The idea behind this thread was to generate some discussion around convenience functions or scripts that users have cooked up over the years to help manage, troubleshoot, or speed up any other menial tasks on their Slackware system. These ideas are not going into the distribution, they were just talking points that I was trying to use to spark some ideas or conversation. I'm just another Slackware user and not making development decisions for anyone.
I got your point about inefficiencies of using sub-shells so lets not beat a dead horse about perfect coding practices because that's not what I wanted to discuss.
If there's no interest in the topic as I originally posed in post #1 then I'm fine letting it die off without the arguing over how people should write their bash scripts. If I wanted better efficiency I would write something in C, not bash, but that's not the point here.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.