[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.
This thread is a great idea. Here's a few I use adapted to use the same PKGDIR variable.
Code:
PKGDIR="/var/lib/pkgtools/packages"
# list kernel pkgs natural sort
kernsort () {
( cd $PKGDIR ; ls -lv kernel-* )
}
# list kernel pkgs grouped by version
kernsortver () {
( cd $PKGDIR ; ls kernel-* ) | sort -t- -k3
}
# get list of installed slackbuilds (_SBo tag) that no longer exist in repo
SLACKBUILDS=/var/lib/sbopkg/SBo/15.0/SLACKBUILDS.TXT
#SLACKBUILDS=/usr/sbo/repo/SLACKBUILDS.TXT
#SLACKBUILDS=/var/lib/slpkg/repository/SLACKBUILDS.TXT
sbremoved () {
for i in `( cd $PKGDIR ; ls *_SBo ) | rev |cut -d- -f4-|rev`
do
echo -n "$i "
grep -c "SLACKBUILD NAME: $i$" $SLACKBUILDS
done | grep 0$
}
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.
It is in general a very good idea.
Quote:
Originally Posted by 0XBF
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.
When you post them and show them (not exactly you, but anyone), it means you want to add something to slackware (but in general to any distro). it's a helpful tool, and it's meant to be useful for more than just you (oh yes, you did make development already). Which is totally fine really.
And now comes the interesting part: do you want to share low quality, slow and resource hungry code just because you don't really care about that 1 sec?
Quote:
Originally Posted by 0XBF
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.
And this is the next step: I [tried to] improve your code by discussing that issue, because I care about it. Is it a common goal, isn't it?
Quote:
Originally Posted by 0XBF
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.
That is just wrong, you can write high and low quality code on any language. Obviously discussing the quality of the scripts is [almost] offtopic here, but a very important question. For my part, I would do my best if I wanted to give (share) anything to others.
That is just wrong, you can write high and low quality code on any language. Obviously discussing the quality of the scripts is [almost] offtopic here, but a very important question. For my part, I would do my best if I wanted to give (share) anything to others.
Code can be written less than optimally if that has a purpose, which is that of understanding and readability. Otherwise, everybody should just write assembler code, right? Including you.
I side up with @pan64 here. Why everyone feels buttfucked when @pan64 clearly just try to give advice on how to further improve the code? Why is that bad?
I side up with @pan64 here. Why everyone feels buttfucked when @pan64 clearly just try to give advice on how to further improve the code? Why is that bad?
I have no issue with his suggestions for improving the example code snippets, they are good. I was triggered by his remark "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".
And now comes the interesting part: do you want to share low quality, slow and resource hungry code just because you don't really care about that 1 sec?
Context matters here. A scenario where I use these generally goes like this: 1. I find a library or file or something and wonder where it came from, so I 'pkggrep' it. If I want to look further at that package I 'pkgcat' into a pager and read it. Execution time doesn't matter for my case and I'm not calling the function with heavy usage from another script or anything. My aim was that it made it quicker than writing out the full "cat <full pathname>" to examine a package, and that was about it.
Quote:
Originally Posted by pan64
Obviously discussing the quality of the scripts is [almost] offtopic here...
Exactly the point I was trying to make. I wasnt trying to just talk about the most efficient way to do my pkgcat function and I didnt want the topic to get "buried in the weeds" over that. I appreciate your suggestions for improvements, but then devolving into back and fourth about wasting resources and doing things "the MS way" kinda derails the topic of getting others to share their own scripts/functions, so I was trying to nip it in the bud. I hope you understand where I'm coming from.
I have a script that checks for update of Slackware or third-party package repositories (I'm using slackpkg+).
The script runs every hour using cron and sends a desktop notification when the Slackware or a third-party repository has any update.
To be able to send a desktop notification (to fluxbox), I need to export the session DBUS_SESSION_BUS_ADDRESS variable before executing notify-send.
The way I get DBUS_SESSION_BUS_ADDRESS variable could probably be improved but as stated by OP, it's not the primary topic to be discussed here.
However, I'm open to suggestion (maybe in PM to avoid being off-topic).
Code:
$ crontab -l
SHELL=/bin/bash
# Check update of Slackware packages
# minute | hour | day of month | month | day of week
0 * * * * sh $HOME/data/script/check_slackware_update.sh
Code:
$ cat $HOME/data/script/check_slackware_update.sh
#!/bin/bash
/usr/sbin/slackpkg &>/dev/null check-updates
username=$(/usr/bin/whoami)
export $(ps -u $username e | grep -m 1 -Eo 'DBUS_SESSION_BUS_ADDRESS=[^ ]* ')
[ -s ~/.slackpkg/updated-repos.txt ] && /usr/bin/notify-send -i dialog-warning "[ S L A C K P K G + ]" "Available updates in repositories: $(cat ~/.slackpkg/updated-repos.txt|tr '\n' ','|sed 's/,$//')"
Distribution: Slackware64 {15.0,-current}, FreeBSD, stuff on QEMU
Posts: 462
Rep:
Here's a script that saves and modifies a list of man pages I want to read later:
Code:
#!/bin/ksh
LISTFILE=$HOME/.man_backlog
BKLTMP=$(mktemp /tmp/XXXX.manbklog)
case $1 in
-h|--help)
echo
echo "Usage: -a: Add a man page to the list"
echo " -d: Remove a man page from the list"
echo "No argument: display the list and read a page"
echo
exit
;;
-a)
for arg in $@; do
[ $arg != $1 ] && echo $arg >> $LISTFILE
done
exit
;;
-d)
for arg in $@; do
GREPCMD="$GREPCMD -e $arg"
done
GREPCMD="grep -vx $GREPCMD"
$GREPCMD $LISTFILE > $BKLTMP
mv $BKLTMP $LISTFILE
exit
;;
esac
if [ $(wc -l $LISTFILE | cut -d' ' -f1) -gt 0 ]; then
echo
echo You\'ve got at least one man page to read...
echo
cat $LISTFILE | sort -u
echo
echo Pick one...
read PICK
man $PICK
echo Delete? \(y/*\)
read YN
if [ $YN == "y" ]; then
grep -vx $PICK $LISTFILE > $BKLTMP
mv $BKLTMP $LISTFILE
fi
echo
exit
fi
And if you don't want to import all variables in environment
This also has the advantage over the earlier suggestion that it isn't position dependent. It will work regardless of what order the variables appear in /etc/os-release.
I've used this little, Slackware-based script for years. It's an easy way to grant a temporary root privilege to a single command, and it uses only $HOME, not /etc or /usr.
Code:
#!/bin/sh
case `id -u` in
0) # privileged script here
;;
*) # wrapper here and ready
exec /bin/su -c "`which $0`" -
;;
esac
Over the years, I used this to adjust the temperature and frequency of the CPU load, as well as reclaim the swap area back into RAM, whenever I wanted. It's so simple to use.
For example, unswap back into RAM:
Code:
#!/bin/sh
# 2019-11-30 MVM : Unswap in sequence, not in toto.
case `id -u` in
0) ( for i in $(awk '/dev/ {print $1;}' < /proc/swaps) ; do
/sbin/swapoff $i ; /sbin/swapon -p 1 $i
done ) ;;
*) exec /bin/su -c "`which $0`" - ;;
esac
It can go anywhere in $HOME (I use ~/bin/), and then I add +x. Then, when I want to run it with privileges, I don't need to add "su" or "sudo" first, do something, and then... type it again because I goofed... now, password... you get it. It's very similar to normal scripting, but with the added fun of $ROOT instead of $HOME.
What about the "exec /bin/su -c..."? That's the step to "restart this, with root." And, if you already have root privileges, you don't need a password.
read -r os version codename < <(. /etc/os-release ; echo $ID $VERSION $VERSION_CODENAME)
I generally avoid shell extensions -- referring here to the process substitution expression <( command ). I prefer anything I write to be portable as possible. Even though my tool is so dependent on Slackware that it makes little difference, I'll still review it for portability before release.
Here's a demonstration. This solution is more readable than my original, but designed to be shell-independent. Per your suggestions, it uses the shell for parsing, employing a subshell to avoid importing unused variables.
Passes Slackware 15's Bash and Korn shells, with and without -oposix. It is likely to pass even a real Bourne shell, because it uses backquotes instead of $() for command substitution.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.