Page 1 of 1

[SOLVED] A Useful Bash Script for Beginner?

Posted: Thu Dec 28, 2017 7:49 am
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.

Re: A Useful Bash Script for Beginner?

Posted: Thu Dec 28, 2017 8:02 am
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.

Re: A Useful Bash Script for Beginner?

Posted: Thu Dec 28, 2017 8:04 am
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 $?

Re: A Useful Bash Script for Beginner?

Posted: Thu Dec 28, 2017 10:29 am
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"

Re: A Useful Bash Script for Beginner?

Posted: Thu Dec 28, 2017 11:34 am
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

Re: A Useful Bash Script for Beginner?

Posted: Thu Dec 28, 2017 11:39 am
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.

Re: A Useful Bash Script for Beginner?

Posted: Thu Dec 28, 2017 12:12 pm
by Flemur
Another note -
- put scripts in your $HOME/bin
- and "chmod +x script-name"
Then you can run them like regular commands.

Re: A Useful Bash Script for Beginner?

Posted: Thu Dec 28, 2017 1:52 pm
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.

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

Posted: Fri Dec 29, 2017 1:20 am
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).

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

Posted: Fri Dec 29, 2017 4:35 pm
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.

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

Posted: Tue Apr 10, 2018 11:55 am
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!

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

Posted: Wed Apr 11, 2018 3:36 pm
by Termy
I agree -- VIM is the only way to edit text. :P