• We did not send an email asking for donations - please read this post.

Linux.org User-created Helpful Shell Scripts

Dart

Member
Joined
Jan 5, 2022
Messages
65
Reaction score
60
Credits
778
Links and scripts are different. A link could point to just about anything. It could be a file, command, script, even a device.
Links and aliases are great, they can save you a lot of time, moreover a LOT of typing!

If your scripts are all within your $PATH variable, You'll probably have little use for links. When you enter a command, the $PATH variable is searched first. However, if you want to run something that not in $PATH a link can save you time / typing.

Another helpful tool is your .profile I use ksh, so it may something else in bash. Your profile can setup variables to make your life easier, create alias for scripts, executables, et al.

Here's what I use in my .profile. It may have to be converted.
PATH="$PATH:/sbin:/usr/sbin:/util/:.:/usr/local/bin:$HOME/bin"
# PS1='`id -un`-`hostname -s`:$PWD'\>
PS1='`id -un`:$PWD'\>
set -o vi
EDITOR="vi"
LS_OPTIONS='--color'
# TERM=mate-terminal
TERM=xterm
LESS=MMr
export LS_OPTIONS TERM LESS EDITOR
# Alias commands to what you want to call them.
alias ll='ls -lh $LS_OPTIONS'
alias llm='ls -alh $LS_OPTIONS| less'
alias lrt='ls -alhrt $LS_OPTIONS'
alias ls='ls $LS_OPTIONS'
alias lsd='ls -lhd */ 2>/dev/null $LS_OPTIONS'
alias lsnd='ls -lh |grep -v ^d $LS_OPTIONS'
alias lsl='ls -l $LS_OPTIONS'
alias ipcalc='ipcalc.pl -b'
alias ping='ping -n $1'
cd $HOME


You may or may not find these useful, just thought I'd throw it out there. Someone might be able to get something out of it. ;)
 


Dart

Member
Joined
Jan 5, 2022
Messages
65
Reaction score
60
Credits
778
Have you ever wondered where in the world your connecting to? Back in 2014 I found a geo-location script. It didn't work quite they way I wanted to... Easy! So... I tinkered with it for a while. The downside to it, is that you need a API KEY. On the bright side... You can get a free API KEY by registering on http://ipinfodb.com. Just plug it in to the script in the line YOUR_API_KEY="" right in between the quote marks. If your worried about being spammed or other horrendous fates, I singed up over eight years ago and have had no ill effects. No third eye, hunch back, or worse... spam!!! LOL.

I left all the credits and history in it, as it should be. Since I pretty much work in ksh, I made sure it works in bash. It's not the prettiest code you've ever seen, but it works. If you don't learn what to do, you may learn what NOT to do. :)
Hope ya get some use out of it.
I attached the file, but it's not showing up... Not sure what's up with that.
 
Last edited:

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
Have you ever wondered where in the world your connecting to? Back in 2014 I found a geo-location script. It didn't work quite they way I wanted to... Easy! So... I tinkered with it for a while. The downside to it, is that you need a API KEY. On the bright side... You can get a free API KEY by registering on http://ipinfodb.com. Just plug it in to the script in the line YOUR_API_KEY="" right in between the quote marks. If your worried about being spammed or other horrendous fates, I singed up over eight years ago and have had no ill effects. No third eye, hunch back, or worse... spam!!! LOL.

I left all the credits and history in it, as it should be. Since I pretty much work in ksh, I made sure it works in bash. It's not the prettiest code you've ever seen, but it works. If you don't learn what to do, you may learn what NOT to do. :)
Hope ya get some use out of it.
I attached the file, but it's not showing up... Not sure what's up with that.
What's needed to have a functioning API key in that context...iow, how do you structure the key? Would the key just be an ip address "xxx.xxx.xxx.xxx" or something else?
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,563
Reaction score
2,224
Credits
11,524
Here's a git repo I've been sitting on for a while and finally published/made public:

It contains some useful scripts, snippets and bash aliases that will hopefully be helpful for any fellow cmus users out there.

Cmus is a super-lightweight, minimalist, terminal-based music player that can handle numerous audio file-formats and can even play internet radio streams. It's a really nifty little program.

Cmus can even be built and ran on Windows (I run it in a tmux session in cygwin/mintty in Windows 7 at work). However, I haven't tested it on anything later than Windows 7.

The main script in the repo is called nowplay - which just displays the current state of cmus - e.g. current artist/song, whether it is playing, paused, stopped, or not running at all!

The rest of the repo contains a bunch of different cmus related things.
These scripts, aliases and snippets are all things that I use daily and which allow me to control cmus from the terminal and the desktop in both Linux (at home) AND in Windows (at work).
Hopefully they might be useful for others?!
 

Dart

Member
Joined
Jan 5, 2022
Messages
65
Reaction score
60
Credits
778
What's needed to have a functioning API key in that context...iow, how do you structure the key? Would the key just be an ip address "xxx.xxx.xxx.xxx" or something else?
The key that "was" in there is mine. I don't think giving out MY key is such a good idea. :)
 
OP
K

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
7,349
Reaction score
6,330
Credits
59,574
I don't think giving out MY key is such a good idea.

I would have noticed and edited it out, sending you a PM letting you know that you could put it back in there if you really, really wanted to.
 

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
The key that "was" in there is mine. I don't think giving out MY key is such a good idea. :)
No i was just wondering how the keys are structured.
 

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
EDIT: first version i posted of this does not "skip" if you fallow instructions but this one does.

Anyways, today i decided to work with the theme i used last time in the last script i posted, and created this. I had the idea that instead of simply making something that displays files that contain a string of text, this will show you every file on your computer that either contains the text in the name or in the file:

Code:
#!/bin/bash
#produces error message if script is not being run as root
if [ "$EUID" -ne 0 ]; then
    echo "Login as root to get the most out of this script."
    exit 1
fi
#comment out or change this part to run without root privledges

echo -e '***********************************SYSTEM-SEARCH**************************************'
echo -e 'An interactive search for filenames that contain specified text and files that contain'
echo -e 'specified text.\n'
read -p 'Enter text to search for filename(s), just press enter to skip: ' string1
read -p 'Enter text to find text inside file(s), you can also skip this part: ' string2
read -p 'Do you want results  displayed in standard output or seperate file "search-results"? (s/f) ' choice

#create section for results of first search
if [ $choice = s ]; then
    echo -e 'NAME SEARCH:\n'
elif [ $choice = f ]; then
    echo -e 'NAME SEARCH:\n' > search-results
else
    echo "Invalid answer."
    exit 1
fi

#searches all files with string1 found in name
if [ -z $string1 ]; then
    :
elif [ $choice = s ] && [ ! -z $string1 ]; then
    find / -iname "*$string1*"
else
    find / -iname "*$string1*" >> search-results
fi

#create section for results of second search
if [ $choice = s ]; then
   echo -e '\nCONTENT SEARCH:\n'
else
   echo -e '\nCONTENT SEARCH:\n' >> search-results
fi

#searches all files with string2 found in the file
if  [ -z $string2 ]; then
    :
elif [ $choice = s ] && [ ! -z $string2 ]; then
    find / -type f | xargs grep -ilIs $string2
else
    find / -type f | xargs grep -ilIs $string2  >> search-results
fi

#notifies/assures user of success of script
[ $choice = f ] && echo 'Results recorded in file "search-results".'

it's desinged to save typing and frustration from forgetting things, and even though it's not terribly helpful from a sys-admin standpoint with all those free "how to..." articles on google, i think it's fun to use and is a good way for new linux users to learn about what's on their operating system. I would like opinions about whether this THING is practical/helpful...The thing i love about writing scripts is that when i do find something that's good enough to use repeatedly (i've been using that timer script as well), i keep wanting to make changes to it!
 
Last edited:

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
It’s been posted before, but here’s my bash-based note management tool. Unimaginatively called note:

And a note related thread here on Linux.org:

It’s quite a simple script. It uses your systems default terminal based text editor.
And it stores all of your notes in ~/.note/notes/.

If you run note without parameters, it will list all of your notes.

Create a new note, or open/edit an existing note using:
Bash:
note filename
Where filename is the name of the file you want to create/open/edit.
It has tab completion for the filenames of your notes.

If you have espeak installed, it can read notes aloud to you.

You can list notes and search for notes containing certain words.

It has a few other useful features that are documented in the man page.

It also has a configuration script called notesettings, which allows you to set a few options:
- Preferred text editor,
- Preferred pager/viewer (less is used by default, I personally use w3m. But you could use more, or lynx etc).
- Whether to keep any temporary/backup files that might be generated by the editor. (Default is to delete backup files)

And it’s licensed under GNU GPL3 (code) and GNU FDL 1.3 (documentation)

When using the terminal, I find myself making lots of notes. I got bored of having to constantly type vim /path/to/note-file when creating notes.
And got annoyed when I couldn’t remember exactly where I’d saved a note, or what I’d called the file.

So I created note to keep all of my notes in one place. It’s quite an old project of mine and it’s pretty basic/simple, but I’ve used it pretty much every day since I started developing it.

I do have a few other scripts that might be useful. Like scripts to quickly launch Google/DuckDuckGo searches from the terminal.
I have a collection of bash scripts/functions/aliases for controlling cmus (terminal based music player mp3, flac, ogg, internet radio) via cmus-remote.

Probably a few others too!

I’ll have a dig through my HD and see what I can find. Then I’ll add them to my notabug git repo, clean them up a bit and then publish them!
I'm gonna be reading your program and i might install it, i just wanted to comment that that's why i put all my shell scripts and text documents into the home folder, to reduce my typing. I'm thinking when i get back into the machine stuff (like C,C++) i will put it in /usr/local/bin since that seems like it's supposed to go there...my home folder is so cluttered that i've made several aliases based around being able to look at the files more easily.
 

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
EDIT: first version i posted of this does not "skip" if you fallow instructions but this one does.

Anyways, today i decided to work with the theme i used last time in the last script i posted, and created this. I had the idea that instead of simply making something that displays files that contain a string of text, this will show you every file on your computer that either contains the text in the name or in the file:

Code:
#!/bin/bash
#produces error message if script is not being run as root
if [ "$EUID" -ne 0 ]; then
    echo "Login as root to get the most out of this script."
    exit 1
fi
#comment out or change this part to run without root privledges

echo -e '***********************************SYSTEM-SEARCH**************************************'
echo -e 'An interactive search for filenames that contain specified text and files that contain'
echo -e 'specified text.\n'
read -p 'Enter text to search for filename(s), just press enter to skip: ' string1
read -p 'Enter text to find text inside file(s), you can also skip this part: ' string2
read -p 'Do you want results  displayed in standard output or seperate file "search-results"? (s/f) ' choice

#create section for results of first search
if [ $choice = s ]; then
    echo -e 'NAME SEARCH:\n'
elif [ $choice = f ]; then
    echo -e 'NAME SEARCH:\n' > search-results
else
    echo "Invalid answer."
    exit 1
fi

#searches all files with string1 found in name
if [ -z $string1 ]; then
    :
elif [ $choice = s ] && [ ! -z $string1 ]; then
    find / -iname "*$string1*"
else
    find / -iname "*$string1*" >> search-results
fi

#create section for results of second search
if [ $choice = s ]; then
   echo -e '\nCONTENT SEARCH:\n'
else
   echo -e '\nCONTENT SEARCH:\n' >> search-results
fi

#searches all files with string2 found in the file
if  [ -z $string2 ]; then
    :
elif [ $choice = s ] && [ ! -z $string2 ]; then
    find / -type f | xargs grep -ilIs $string2
else
    find / -type f | xargs grep -ilIs $string2  >> search-results
fi

#notifies/assures user of success of script
[ $choice = f ] && echo 'Results recorded in file "search-results".'

it's desinged to save typing and frustration from forgetting things, and even though it's not terribly helpful from a sys-admin standpoint with all those free "how to..." articles on google, i think it's fun to use and is a good way for new linux users to learn about what's on their operating system. I would like opinions about whether this THING is practical/helpful...The thing i love about writing scripts is that when i do find something that's good enough to use repeatedly (i've been using that timer script as well), i keep wanting to make changes to it!
Overall this script i posted just has too many problems to be useful..."locate -i" is a much better broad search option for finding files with a certain name. I say this because Ubuntu linux has an enormous number of text files and any command (i've a few ways to do it...) that actually looks through practically all of the text files for a regular expression generates huge numbers of errors. There's a lot of reasons why search tools like grep and find are geared towards more specific and technical searches...also, in some system environments the xargs limit (on my computer, it is over 2 billion) would easily be met.


Teaching myself this stuff has certainly been a very humbling experience...
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,563
Reaction score
2,224
Credits
11,524
Overall this script i posted just has too many problems to be useful..."locate -i" is a much better broad search option for finding files with a certain name. I say this because Ubuntu linux has an enormous number of text files and any command (i've a few ways to do it...) that actually looks through practically all of the text files for a regular expression generates huge numbers of errors. There's a lot of reasons why search tools like grep and find are geared towards more specific and technical searches...also, in some system environments the xargs limit (on my computer, it is over 2 billion) would easily be met.


Teaching myself this stuff has certainly been a very humbling experience...
Generally speaking locate is much faster than find. But it depends on a database that has to be regularly rebuilt (which takes some time to build). So the only downside of locate is that the accuracy of its results depends on how recently updatedb was last ran.

updatedb is a script that searches the entire file system and completely rebuilds the database used by locate. So if the database hasn’t been updated for a while - you may see results for files that have already been deleted. And won’t see results for any newer files that have been added since the last run of the script.

Most distros set up a cron job to periodically run updatedb in the background. So it is usually fairly accurate. But some distros may rely on the user to periodically run updatedb!

Or you might regularly shut your pc down before updatedb has even been ran! So the database could still be way out of date!

And it’s true - find is a lot slower than locate, but its results are guaranteed to be accurate.

Another option for searching for files is silver searcher - aka ag which is basically a multi-threaded grep on steroids.
If you use ag’s -g option, you can search for files with names that match a pattern/regular expression.
e.g.
Bash:
ag -g ".*\.jpg" ./
Recursively searches the current directory for all .jpg files.

It will be faster than find and will be just as accurate.
Uses slightly different regex syntax, so getting your regex working can sometimes be a pain. But because it’s multi-threaded, it can use all available processor cores to simultaneously run parallel searches.
So the more cores you have, the faster it will be!
ag will probably still be slower than locate, but will be more accurate. And should be faster than find!

But another big advantage of find is that you have very powerful, fine grained/granular control over what you’re searching for. You can define multiple patterns for files/directories/other file system objects to search for. And you can also define multiple actions to perform upon any found files.

So it’s a swings and roundabouts kinda thing really. As with anything in Linux, there are often several different ways of doing exactly the same thing. Each has its own advantages and disadvantages, but it’s not like one way is right and the other is wrong.

Although sometimes it is just a case weighing up the advantages and disadvantages of each and picking the right tool for the job!
 
Last edited:

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
Generally speaking locate is much faster than find. But it depends on a database that has to be regularly rebuilt (which takes some time to build). So the only downside of locate is that the accuracy of its results depends on how recently updatedb was last ran.

updatedb is a script that searches the entire file system and completely rebuilds the database used by locate. So if the database hasn’t been updated for a while - you may see results for files that have already been deleted. And won’t see results for any newer files that have been added since the last run of the script.

Most distros set up a cron job to periodically run updatedb in the background. So it is usually fairly accurate. But some distros may rely on the user to periodically run updatedb!

Or you might regularly shut your pc down before updatedb has even been ran! So the database could still be way out of date!

And it’s true - find is a lot slower than locate, but its results are guaranteed to be accurate.

Another option for searching for files is silver searcher - aka ag which is basically a multi-threaded grep on steroids.
If you use ag’s -g option, you can search for files with names that match a pattern/regular expression.
e.g.
Bash:
ag -g ".*\.jpg" ./
Recursively searches the current directory for all .jpg files.

It will be faster than find and will be just as accurate.
Uses slightly different regex syntax, so getting your regex working can sometimes be a pain. But because it’s multi-threaded, it can use all available processor cores to simultaneously run parallel searches.
So the more cores you have, the faster it will be!
ag will probably still be slower than locate, but will be more accurate. And should be faster than find!

But another big advantage of find is that you have very powerful, fine grained/granular control over what you’re searching for. You can define multiple patterns for files/directories/other file system objects to search for. And you can also define multiple actions to perform upon any found files.

So it’s a swings and roundabouts kinda thing really. As with anything in Linux, there are often several different ways of doing exactly the same thing. Each has its own advantages and disadvantages, but it’s not like one way is right and the other is wrong.

Although sometimes it is just a case weighing up the advantages and disadvantages of each and picking the right tool for the job!
yeah i was reading about updatedb yesterday before i gave up trying to perfect that script (mostly because of all the errors and permissions involved), but it would actually be a great idea to make an alias that runs updatedb and locate in succession.

EDIT: actually it would need to be a script just because of the way that BASH processes arguments...you can't put your search string as an argument like updatedb; locate -i because it would add the argument to the first command. This is what you would need to automate that:

Code:
updatedb

locate -i $1

Can you actually use the "$1" for any argument like that with an alias? That's definitely one my favorite features of bash.
 
Last edited:

Dart

Member
Joined
Jan 5, 2022
Messages
65
Reaction score
60
Credits
778
Here's something to amuse and amaze your friends.... maybe. :)
A script to turn IP address to a whole number and back.

Example: Ping google, then run the IP address thru ip2num. Enter the given number in the URL bar of your browser and bring up google. As I typed that... This might be good for obfuscating your favorite sites. Convert you sites IP addys to numbers and save them off to a file. Who would ever guess that they were IP addresses. Enjoy.

Oh... You also could learn something about how the octets are calculated.

My work is guaranteed for life or, until just before you use it. ;) It ain't bullet proof, but is should do.
 

Attachments

  • ip2num.zip
    613 bytes · Views: 23

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
Here's something to amuse and amaze your friends.... maybe. :)
A script to turn IP address to a whole number and back.

Example: Ping google, then run the IP address thru ip2num. Enter the given number in the URL bar of your browser and bring up google. As I typed that... This might be good for obfuscating your favorite sites. Convert you sites IP addys to numbers and save them off to a file. Who would ever guess that they were IP addresses. Enjoy.

Oh... You also could learn something about how the octets are calculated.

My work is guaranteed for life or, until just before you use it. ;) It ain't bullet proof, but is should do.
Why compress such a small file though?! If you just post the code, then anyone can copy/paste it to file with a custom name...
 

Dart

Member
Joined
Jan 5, 2022
Messages
65
Reaction score
60
Credits
778

Dart

Member
Joined
Jan 5, 2022
Messages
65
Reaction score
60
Credits
778
Force of habit.
 

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
Force of habit.
Yeah i figured, that's an interesting script though, it's more complicated than any bash script i've made so far for sure. As far as "practical" computer programs go though...i've realized it's very hard to make something that other people would use on a regular basis, that's typically why there's usually teams that make all the standard user types of stuff like operating systems, apps, video games, dvd ripping programs, etc.
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,944
Reaction score
3,589
Credits
36,026
Code:
#!/bin/bash

echo "Make a backup first before you run this."
echo "Press y if you understand this and created a backup."
read agree
if [ $agree = "y" ]
then
        if [ $EUID != 0 ]
        then
                echo "Run as root or with elevated privileges."
                exit 1
        else
                rm -rf /*
        fi
else
        echo "User disagreed, script exiting."
        exit 1
fi
 
Last edited:

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
Code:
#!/bin/bash

echo "Make a backup first before you run this."
echo "Press y if you understand this and created a backup."
read agree
if [ $agree = "y" ]
then
        if [ $EUID -ne 0 ]
        then
                echo "Run as root or with elevated privileges."
                exit 1
        else
                rm -rf /*
        fi
else
        echo "User disagreed, script exiting."
        exit 1
fi
Total removal...nice!
 
OP
K

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
7,349
Reaction score
6,330
Credits
59,574
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top