LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 12-24-2022, 12:11 PM   #1
0XBF
Member
 
Registered: Nov 2018
Distribution: Slackware
Posts: 807

Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
Arrow [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:
Code:
###################################
# Package Helper functions:
###################################
# Location of packages:
PKGDIR="/var/lib/pkgtools/packages"
# Bash completion for package names:
complete -W "$(ls $PKGDIR)" pkgcat
pkggrep () {
  grep $@ $PKGDIR/*
}
pkgcat () {
  ( cd $PKGDIR ; cat $@ )
}
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!
 
Old 12-24-2022, 03:11 PM   #2
ethelack
Member
 
Registered: Mar 2021
Location: New Zealand
Distribution: Slackware
Posts: 82

Rep: Reputation: Disabled
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>
 
2 members found this post helpful.
Old 12-24-2022, 03:48 PM   #3
ppr:kut
Slackware Contributor
 
Registered: Aug 2006
Location: Netherlands
Distribution: Slackware
Posts: 631

Rep: Reputation: 463Reputation: 463Reputation: 463Reputation: 463Reputation: 463
-current now automatically loads files from ~/.profile.d, so you could move them there
 
4 members found this post helpful.
Old 12-24-2022, 05:04 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by 0XBF View Post
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

Last edited by keefaz; 12-24-2022 at 05:09 PM.
 
Old 12-24-2022, 07:19 PM   #5
0XBF
Member
 
Registered: Nov 2018
Distribution: Slackware
Posts: 807

Original Poster
Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
Quote:
Originally Posted by keefaz View Post
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.
 
Old 12-25-2022, 02:21 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,131

Rep: Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374
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....
 
Old 12-25-2022, 03:31 AM   #7
SCerovec
Senior Member
 
Registered: Oct 2006
Location: Cp6uja
Distribution: Slackware on x86 and arm
Posts: 2,486
Blog Entries: 2

Rep: Reputation: 991Reputation: 991Reputation: 991Reputation: 991Reputation: 991Reputation: 991Reputation: 991Reputation: 991
Arrow

An excerpt from my ~/bin/:

https://github.com/fire-h0und/tool_chest

Do be warned some are WIP others are mature and one (wg) seems to be trampled over by some recent software, YMMV.

Meanwhile have nice holidays

I do understand that as of recent the proper path is ~/.local/bin but i hate hiding code and am a lazy typist I guess...
 
3 members found this post helpful.
Old 12-25-2022, 04:08 AM   #8
ethelack
Member
 
Registered: Mar 2021
Location: New Zealand
Distribution: Slackware
Posts: 82

Rep: Reputation: Disabled
Quote:
Originally Posted by ppr:kut View Post
-current now automatically loads files from ~/.profile.d, so you could move them there
thanks for the heads up will change to it when 15.0 upgrades the etc-15.0-x86_64-17 package.
 
Old 12-25-2022, 08:41 AM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by 0XBF View Post
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/"
 
2 members found this post helpful.
Old 12-25-2022, 10:53 PM   #10
0XBF
Member
 
Registered: Nov 2018
Distribution: Slackware
Posts: 807

Original Poster
Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
Quote:
Originally Posted by pan64 View Post
Using ( ) is not required at all
Just a habit when I write functions in bash. Doesnt hurt does it?

Quote:
Originally Posted by pan64 View Post
and forking a new shell just for a cat is just wasting resources....
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.
 
Old 12-26-2022, 03:05 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,131

Rep: Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374
Quote:
Originally Posted by 0XBF View Post
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
 
Old 12-26-2022, 05:07 AM   #12
Windu
Member
 
Registered: Aug 2021
Distribution: Arch Linux, Debian, Slackware
Posts: 601

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
2 members found this post helpful.
Old 12-26-2022, 01:52 PM   #13
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by 0XBF View Post

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)

Last edited by keefaz; 12-26-2022 at 02:05 PM.
 
1 members found this post helpful.
Old 12-26-2022, 02:26 PM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,131

Rep: Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374
Quote:
Originally Posted by Windu View Post
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.
 
Old 12-26-2022, 06:21 PM   #15
0XBF
Member
 
Registered: Nov 2018
Distribution: Slackware
Posts: 807

Original Poster
Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
@pan64

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.
 
6 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Google's tips and tricks guide for Android users LXer Syndicated Linux News 0 07-27-2016 03:57 PM
nVidia - No diversion 'diversion of /usr/lib/xorg/modules/extensions/libglx nicolasdiogo Linux - Hardware 2 12-31-2012 08:14 AM
GNOME:(Convenience) Any way to input shell commands without bringing up a terminal? kopatops Linux - Software 6 03-13-2010 04:05 PM
LXer: Tips4Linux.com - Tips and Tricks for Linux Users LXer Syndicated Linux News 0 07-14-2009 03:30 PM
Ubuntu 5.10 -> 6.06: diversion of /usr/bin/ldd to /usr/bin/ldd.amd64 by ia32-libs HellSpawn Linux - Software 2 06-04-2006 09:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration