[SOLVED] A Useful Bash Script for Beginner?

Forum rules
Before you post please read how to get help
Post Reply
User avatar
bensatlantic
Level 1
Level 1
Posts: 26
Joined: Thu Nov 30, 2017 1:53 pm

[SOLVED] A Useful Bash Script for Beginner?

Post by bensatlantic » Thu Dec 28, 2017 7:49 am

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 bensatlantic on Thu Dec 28, 2017 9:20 pm, edited 6 times in total.

User avatar
Termy
Level 5
Level 5
Posts: 746
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: A Useful Bash Script for Beginner?

Post by Termy » Thu Dec 28, 2017 8:02 am

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.
Here to help.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

Mute Ant
Level 13
Level 13
Posts: 4721
Joined: Tue Sep 03, 2013 7:45 pm

Re: A Useful Bash Script for Beginner?

Post by Mute Ant » Thu Dec 28, 2017 8:04 am

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 $?
Right at the bottom, all alone in the centre of the bright cold floor, was a lemon drop.

User avatar
bensatlantic
Level 1
Level 1
Posts: 26
Joined: Thu Nov 30, 2017 1:53 pm

Re: A Useful Bash Script for Beginner?

Post by bensatlantic » Thu Dec 28, 2017 10:29 am

[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 14
Level 14
Posts: 5493
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: A Useful Bash Script for Beginner?

Post by Flemur » Thu Dec 28, 2017 11:34 am

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?
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less

User avatar
bensatlantic
Level 1
Level 1
Posts: 26
Joined: Thu Nov 30, 2017 1:53 pm

Re: A Useful Bash Script for Beginner?

Post by bensatlantic » Thu Dec 28, 2017 11:39 am

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 14
Level 14
Posts: 5493
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: A Useful Bash Script for Beginner?

Post by Flemur » Thu Dec 28, 2017 12:12 pm

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?
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less

User avatar
AndyMH
Level 4
Level 4
Posts: 302
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: A Useful Bash Script for Beginner?

Post by AndyMH » Thu Dec 28, 2017 1:52 pm

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.
Thinkpad T430 i7-3632 Cinnamon 18.3, Thinkpad T410 Cinnamon 17.3, Thinkpad T60 18.0 Mate

User avatar
jimallyn
Level 17
Level 17
Posts: 7889
Joined: Thu Jun 05, 2014 7:34 pm
Location: Wenatchee, WA USA

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

Post by jimallyn » Fri Dec 29, 2017 1:20 am

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).
Image

“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
Level 5
Level 5
Posts: 814
Joined: Wed Jul 11, 2012 4:10 pm
Contact:

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

Post by trapperjohn » Fri Dec 29, 2017 4:35 pm

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
Level 1
Level 1
Posts: 39
Joined: Mon Mar 19, 2018 11:02 pm

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

Post by StevenC21 » Tue Apr 10, 2018 11:55 am

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!
"A computer is like air conditioning. It becomes useless when you open Windows."
- Linus Torvalds

User avatar
Termy
Level 5
Level 5
Posts: 746
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

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

Post by Termy » Wed Apr 11, 2018 3:36 pm

I agree -- VIM is the only way to edit text. :P
Here to help.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

Post Reply

Return to “Scripts & Bash”