[SOLVED] A Useful Bash Script for Beginner?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
bensatlantic

[SOLVED] A Useful Bash Script for Beginner?

Post by bensatlantic »

I'm trying to learn how to use and understand bash scripts. While learning how to echo "Hello World" is the usual place to start, it's not useful and doesn't leave me with much understanding or spark further exploration, nor is it ever necessary to execute it. Wondering if someone could furnish a truly useful script that I could use as a reference to learn how and why it's written the way it is, how to make it executable on startup, etc. I've been using Linux for 5 years and am comfortable using the command line, but still unable to take that next step a little further under the hood and try a few bash scripts. Your example would have to be pretty basic but accomplish some simple task on my system that I could observe - maybe listing any new files added to my home directory on a given date? I'm not looking to take your time to teach me scripting here - I'm doing that on my own with books and online tutorials - I just know that Mint users like to share their Linux knowledge and well . . . I'm very comfortable here! Thanks in advance.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 7 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: A Useful Bash Script for Beginner?

Post by Termy »

IMO, you're better off studying it yourself rather than waiting around for a script which just shows you some things which can be done. I learned that stuff by exploring man pages, writing programs to get things I needed done, and by watching YouTube videos by people like myself who want to help with that sort of thing. There are also a lot of awesome books on the topic, such as the Advanced Bash Scripting Guide, (it's not as advanced as the title suggests) which you can find in the Ubuntu repositories:

Code: Select all

$ apt-cache search abs-guide
abs-guide - The Advanced Bash-Scripting Guide
Alternatively, view it here: http://tldp.org/LDP/abs/html/

Another thing is, will you use it? If you'll actually use the terminal and shell, then I'd say go for it, otherwise it might just be a waste of your time.

Best of luck, and if you have any questions, I'm more than welcome to answer them. My inbox is open to you.
Last edited by Termy on Thu Dec 28, 2017 8:04 am, edited 1 time in total.
I'm also Terminalforlife on GitHub.
Mute Ant

Re: A Useful Bash Script for Beginner?

Post by Mute Ant »

Here's both. It looks through the files in your system for the type "ASCII text executable" and makes a symlink to each one...

Code: Select all

#!/bin/bash
find /bin /usr/bin -type f | while read a ; do
	if file -b "$a" | grep "ASCII text executable" ; then
		ln -s "$a"
	fi
done
echo "All these files are scripts used by the system, good bad and ugly..."
exit $?
bensatlantic

Re: A Useful Bash Script for Beginner?

Post by bensatlantic »

[quote="Mute Ant"]Here's both. It looks through the files in your system for the type "ASCII text executable" and makes a symlink to each one...

Excellent - thanks Mute Ant - this is the kind of script I was after - analyzing the command terms written out in an actual script like this and how they're arranged and being able to try substitutions to see what happens - it will keep me busy. This Mint forum is the best!

Here is one I found and am experimenting with since posting - again, I'm at a very elementary level here - still learning terms like 'fi' 'grep' and how to write an 'if - then' script. Also just found that there's a Bash and Scripts section here in the Mint forum.

Code: Select all

#!/bin/sh
# Check if this file exists:
if test -e bzdiff
then
  echo "You have bzdiff"
else
  echo "No bzdiff"
fi
echo "Finished with my 'if' test"
Last edited by bensatlantic on Thu Dec 28, 2017 11:36 am, edited 1 time in total.
User avatar
Flemur
Level 20
Level 20
Posts: 10097
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: A Useful Bash Script for Beginner?

Post by Flemur »

Here's one I use all the time because I don't really use menus to start anything - it lists all the executables in your PATH which contain some string (see below). I call it "W" (capital) after "which".

Code: Select all

#!/bin/bash
#
if [ $# -lt 1 ]
then
  echo Usage: `basename $0` pattern
  exit 1
fi
echo $PATH | sed 's/^/ls -A /' | sed 's/:/ |grep -i '$1'; ls -A /g' | sed 's/$/ |grep -i '$1'/' | bash | sort
exit 0
(Edit: I looked at this recently and couldn't figger out how I came up with the "echo $PATH ..." line - "exercise left to the reader!")

Use: show me all the programs which contain "image" :

Code: Select all

$ W image
btrfs-image
e2image
...
scanimage
xfimage

Code: Select all

$ W dvd
anydvd
convxdvd
...
vlcdvd
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
bensatlantic

Re: A Useful Bash Script for Beginner?

Post by bensatlantic »

Flemur wrote:Here's one I use all the time because I don't really use menus to start anything - it lists all the executables in your PATH which contain some string (see below). I call it "W" (capital) after "which".

Code: Select all

#!/bin/bash
#
if [ $# -lt 1 ]
then
  echo Usage: `basename $0` pattern
  exit 1
fi
echo $PATH | sed 's/^/ls -A /' | sed 's/:/ |grep -i '$1'; ls -A /g' | sed 's/$/ |grep -i '$1'/' | bash | sort
exit 0
(Edit: I looked at this recently and couldn't figger out how I came up with the "echo $PATH ..." line - "exercise left to the reader!")

Use: show me all the programs which contain "image" :

Code: Select all

$ W image
btrfs-image
e2image
...
scanimage
xfimage

Code: Select all

$ W dvd
anydvd
convxdvd
...
vlcdvd
This is great - will forego any further thanks to people, but the help is appreciated. This is what I love about Linux - I'm not able to provide much help in the way of answers here yet, but hope I'm helping the community with some good questions that the gurus can answer.
User avatar
Flemur
Level 20
Level 20
Posts: 10097
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: A Useful Bash Script for Beginner?

Post by Flemur »

Another note -
- put scripts in your $HOME/bin
- and "chmod +x script-name"
Then you can run them like regular commands.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
AndyMH
Level 21
Level 21
Posts: 13563
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: A Useful Bash Script for Beginner?

Post by AndyMH »

Buy a book - I went out and bought 'BASH pocket reference' from Amazon. But... if you have specific need try and then when it fails ask questions on this forum. That was my experience, and there are people here who will solve your arcane problems with grep, awk, sid, etc.

I've written scripts to backup my laptop (using rsync) to either SD card or my NAS, couldn't have done it without the help from people on this site.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
jimallyn
Level 19
Level 19
Posts: 9075
Joined: Thu Jun 05, 2014 7:34 pm
Location: Wenatchee, WA USA

Re: [SOLVED] A Useful Bash Script for Beginner?

Post by jimallyn »

I like "The Linux Command Line" which you can download free here:

http://linuxcommand.org/tlcl.php

I also have a couple of Bash references on my Android phone. Plus I have come across several sites that have lots of useful bash scripts (but I didn't bookmark them).
“If the government were coming for your TVs and cars, then you'd be upset. But, as it is, they're only coming for your sons.” - Daniel Berrigan
trapperjohn

Re: [SOLVED] A Useful Bash Script for Beginner?

Post by trapperjohn »

Hi,

Although marked solved, I want to add:

https://www.gnu.org/software/bash/manual/bash.pdf

https://www.gnu.org/software/gawk/manual/gawk.pdf

https://www.gnu.org/software/sed/manual/sed.pdf

And many others at: https://www.gnu.org/manual/manual.html


BTW: Have a look at the emacs editor for writing your scripts.
StevenC21

Re: [SOLVED] A Useful Bash Script for Beginner?

Post by StevenC21 »

trapperjohn wrote: Fri Dec 29, 2017 4:35 pm BTW: Have a look at the emacs editor for writing your scripts.
Vim for the win!
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: [SOLVED] A Useful Bash Script for Beginner?

Post by Termy »

I agree -- VIM is the only way to edit text. :P
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”