Linux.org User-created Helpful Shell Scripts

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
6,436
Reaction score
5,711
Credits
51,793
Did you write a new script and want to share it with fellow users, site browsers and searchers alike? Well, then add it to this thread!

Be sure to specify if you're not using Bash and maybe name the script something useful.

Be sure to use the CODE tags, to keep things legible for future readers.

If you see a script and think you can improve on it, feel free to do so. If the author likes the edit, they can edit their own posts to include it. Hmm... Kinda like a pull request, I guess.

Other than specific questions regarding existing scripts, let's *try* to keep things on-topic.
 


SlowCoder

Gold Member
Gold Supporter
Joined
May 2, 2022
Messages
456
Reaction score
313
Credits
3,611
Alrighty then, I'll start this thread off with my little contributions.

For more details on the following scripts, follow the links.

bat.sh - Configurable battery stats in the terminal
ls-octal.sh - Display octal permissions for files in your current directory.
readlink-all.sh - Follow a symlink, displaying all links to the destination.
 

Fanboi

Well-Known Member
Joined
Apr 16, 2021
Messages
499
Reaction score
406
Credits
6,114
That really neat code in @SlowCoder's battery script will make something I posted earlier look messy, but anyway...

Delete/move/archive files older than a week
Barebones. Use it to build on. Sorry about style, my best mate burned it into me. I just neatened it up a little (on my phone, hope I didn't break it).
 
Last edited:

gvisoc

Well-Known Member
Joined
May 29, 2020
Messages
412
Reaction score
479
Credits
4,221
This one is a just-works work in progress. It will end up in GitHub when I feel it's OK, but worth sharing.

It is based on this Gist by Reilly Tucker Siemens.

Signs kernel modules with your Machine Owner Key. If you use Secure Boot with your Linux installation, you will need to do this for any module not included in the official update repositories every time you update the Kernel. Examples are plenty: NVIDIA and VirtualBox modules, v4l2loopback, and many other.

It works in Fedora, and some changes needed for Debian, as per the under-the-hood fragmentation. Details in the comments.

The usage is sudo ./signmodule.sh [path-to]/MOK.priv [path-to]/MOK.der module

In case of modules coming with dependencies, it will sign all modules in the directory of the module to sign. This is the case, for example, of VirtualBox; vboxdrv is the sort of main module, but comes with a handful more of them.

Before finishing:
  • It will restart the service systemd-modules-load, which may be failing when this script needs to be run, and this should insert the newly signed modules for the current session and all reboots until the next kernel update.
  • It will tell you which else systemd services you need to restart, in case of any of them depending on the modules you just signed and loaded. This is an obvious example of what can be improved: parse the list in the output of the systemctl list-unit --failed and restart them before exiting.
It doesn't check parameters or observe any extra decency other than the necessary to do the work.
Bash:
#!/bin/sh

readonly hash_algo='sha256'
readonly key="$1"                       # 'MOK.priv'
readonly x509="$2"                      # 'MOK.der'
readonly module="$3"                    # The name of the module (no extensions)

readonly name="$(basename $0)"

readonly fullver=$(uname -r)

log() { local string="${1}"; echo "[$name] ${string}"; }

# The exact location of `sign-file` might vary depending on your platform.
# This is for Fedora
alias sign-file="/usr/src/kernels/${fullver}/scripts/sign-file"

[ -z "${KBUILD_SIGN_PIN}" ] && read -p "Passphrase for ${key}: " KBUILD_SIGN_PIN
export KBUILD_SIGN_PIN

# In Fedora, modinfo is in the PATH for root users. In other
# distributions it may not, and may be located at either /sbin
# or /usr/sbin
# Another difference is whether your distribution compresses the
# modules (.ko.xz) or not (just .ko).
for mod in $(dirname $(modinfo -n $module))/*.ko.xz; do
    log "Unpacking ${mod}..."
    unxz ${mod}
    mod_basename=${mod:0:-3}
    log "Signing ${mod_basename}..."
    sign-file "${hash_algo}" "${key}" "${x509}" "${mod_basename}"
    log "Recompressing ${mod_basename}"
    xz -f ${mod_basename}
done

systemctl restart systemd-modules-load
log "-------------------------------------------------------------------------------"
log "Restart the following units (if any shown) with 'systemctl restart <unitname>':"
systemctl list-units --state failed
log "------------------------------------DONE---------------------------------------"
 
Last edited:

dancaer69

New Member
Joined
Mar 10, 2020
Messages
14
Reaction score
6
Credits
109
Recenty I created an autoshutdown script using dialog, to use remotely to a server pc.


Code:
#!/bin/bash
choise=$(dialog --clear --fb --title "Auto shutdown/reboot/suspend" --menu "Please choose an action:" 0 0 0 \
1 "Suspend " \
2 "Reboot " \
3 "Power off " 3>&1 1>&2 2>&3)

option="$choise"

time=$(dialog --stdout  --timebox "Input the desired time:" 0 0 00 00 00)

echo $time
IFS=":"
read -a strarr <<< "$time"
hours="${strarr[0]}"
mins="${strarr[1]}"
sec=10

htosec=$(($hours * 60 * 60))
#echo $htosec
mtosec=$(($mins * 60))
#echo $mtosec
newdt=$(($htosec + $mtosec + $sec))

newdt=$(date -d  "+$addtime seconds" "+%H:%M:%S")


while [ $hours -ge 0 ]; do
                 while [ $mins -ge 0 ]; do
                         while [ $sec -ge 0 ]; do
                                 dialog --cr-wrap --colors --infobox "The action \n will launch \nat \Z2$newdt\ZN \n\n\Zb   \Z1$hours:$mins:$sec" 8 18
                                 let "sec=sec-1"
                                 sleep 1
                         done
                         sec=59
                         let "mins=mins-1"
                 done
                 mins=59
                 let "hours=hours-1"
               
done

if [ "$option" = "Suspend" ]; then
    #echo "Suspend..."
    sudo systemctl suspend
elif [ "$option" = "Reboot" ]; then
    #echo "Reboot..."
    sudo systemctl reboot
elif [ "$option" = "Power off" ]; then
    #echo "Power off..."
    sudo systemctl poweroff
fi
 

CrazedNerd

Gold Member
Gold Supporter
Joined
Mar 31, 2021
Messages
514
Reaction score
211
Credits
4,413
Here's something I made given the default lack of a timer on Ubuntu, would be even more practical if I could turn this into a clickable desktop shortcut:

Code:
#! /bin/bash
 
 firefox https://www.timeanddate.com/timer/ 2>/dev/null

the redirection at the end is to free up the terminal for other uses and get rid of the worthless error message. Given the auto-complete of firefox (after a couple of visits, entering the URL would only mean entering "t"), you can still win interms of efficiency if you know how to make this into a clickable shortcut!

EDIT: removed line numbers from script to keep any coding novice from getting confused.
 
Last edited:

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,453
Reaction score
1,176
Credits
10,548
Here's something I made given the default lack of a timer on Ubuntu, would be even more practical if I could turn this into a clickable desktop shortcut:

Code:
#! /bin/bash
 2
 3 firefox https://www.timeanddate.com/timer/ 2>/dev/null

the redirection at the end is to free up the terminal for other uses and get rid of the worthless error message. Given the auto-complete of firefox (after a couple of visits, entering the URL would only mean entering "t"), you can still win interms of efficiency if you know how to make this into a clickable shortcut!
You might want to check this: https://linuxconfig.org/time-countdown-bash-script-example
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,453
Reaction score
1,176
Credits
10,548
Here's one I use to back up my dot/config files to a USB drive:
Bash:
#!/bin/bash
DIRLIST=(~/.config ~/.local ~/.mozilla ~/.zoom ~/.claws-mail ~/.makagiga ~/.fonts ~/.gnome ~/.thunderbird)
DIRLIST1=(~/Documents ~/Downloads ~/Pictures ~/Videos ~/Appimages ~/bin ~/txts)
TARGET="/media/$USER/USB-Drive/Bullseye/dot_files"
TARGET1="/media/$USER/USB-Drive/Bullseye/"

cp -r -v -u "${DIRLIST[@]}" "$TARGET" && cp -r -v -u "${DIRLIST1[@]}" "$TARGET1"
 

CrazedNerd

Gold Member
Gold Supporter
Joined
Mar 31, 2021
Messages
514
Reaction score
211
Credits
4,413
Here's one I use to back up my dot/config files to a USB drive:
Bash:
#!/bin/bash
DIRLIST=(~/.config ~/.local ~/.mozilla ~/.zoom ~/.claws-mail ~/.makagiga ~/.fonts ~/.gnome ~/.thunderbird)
DIRLIST1=(~/Documents ~/Downloads ~/Pictures ~/Videos ~/Appimages ~/bin ~/txts)
TARGET="/media/$USER/USB-Drive/Bullseye/dot_files"
TARGET1="/media/$USER/USB-Drive/Bullseye/"

cp -r -v -u "${DIRLIST[@]}" "$TARGET" && cp -r -v -u "${DIRLIST1[@]}" "$TARGET1"
that's real neat, how do you change your flash drive label through the command line? It's pretty straightforward with Disks, a GUI Program, i've wondered how you could change the label from the command line.
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,453
Reaction score
1,176
Credits
10,548
how do you change your flash drive label through the command line?
Hmmm ... not sure really, since I don't use (too scared to try lol) CLI- tools for that task, the couple of times I did try, just ruined an install lol That being said, maybe parted, mkfs or cfdisk might just do it. However, why bother when you have so many handy GUIs around? I use partition manager from KDE.

EDIT: Well, I found this: https://help.ubuntu.com/community/RenameUSBDrive
:)
 
Last edited:

CrazedNerd

Gold Member
Gold Supporter
Joined
Mar 31, 2021
Messages
514
Reaction score
211
Credits
4,413
Hmmm ... not sure really, since I don't use (too scared to try lol) CLI- tools for that task, the couple of times I did try, just ruined an install lol That being said, maybe parted, mkfs or cfdisk might just do it. However, why bother when you have so many handy GUIs around? I use partition manager from KDE.

EDIT: Well, I found this: https://help.ubuntu.com/community/RenameUSBDrive
:)
each filesystem type has a different tool, how confusing!

I usually just do it after formatting through either those GUI tools or the file manager, but it would be interesting if you could change the label without erasing the drive.
 

gvisoc

Well-Known Member
Joined
May 29, 2020
Messages
412
Reaction score
479
Credits
4,221
Here's one I use to back up my dot/config files to a USB drive:
Bash:
#!/bin/bash
DIRLIST=(~/.config ~/.local ~/.mozilla ~/.zoom ~/.claws-mail ~/.makagiga ~/.fonts ~/.gnome ~/.thunderbird)
DIRLIST1=(~/Documents ~/Downloads ~/Pictures ~/Videos ~/Appimages ~/bin ~/txts)
TARGET="/media/$USER/USB-Drive/Bullseye/dot_files"
TARGET1="/media/$USER/USB-Drive/Bullseye/"

cp -r -v -u "${DIRLIST[@]}" "$TARGET" && cp -r -v -u "${DIRLIST1[@]}" "$TARGET1"
Why not with rsync?

Even if you don't have more machines to keep in sync with the thumbdrive, off the top of my head I think I remember (or maybe it's a fabricated memory) that had some extra functionality and better performance.
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,453
Reaction score
1,176
Credits
10,548

Fanboi

Well-Known Member
Joined
Apr 16, 2021
Messages
499
Reaction score
406
Credits
6,114
Not to put too fine a point on it, but..
Other than specific questions regarding existing scripts, let's *try* to keep things on-topic.
I made a discussion/chat thread here.
Guys, let's please use that for discussion. If you follow the OP there, it's pretty easy to keep everything in context and cohesive.
David, if you want to move these last few posts in there (this one, too -- or just delete this one) and edit your OP to reflect the new setup if you approve. You can chown that thread, too, if you like.

Hope that saves a lot of housekeeping.
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,453
Reaction score
1,176
Credits
10,548
python is technically a scripted language can we mention those ?
what about php ?
I'm no moderator, but the title does read: "User-created Helpful Shell Scripts", and the shell is just the interpreter, it's not limited to bash only. So, I think yes, scripts written in any scripting language that the shell can read and run, should be fine to post. Speaking of python, this is a nice post you might want to check:

 

captain-sensible

Well-Known Member
Joined
Jun 14, 2019
Messages
2,855
Reaction score
1,931
Credits
17,653
OP
K

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
6,436
Reaction score
5,711
Credits
51,793
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Latest posts

Top