How to write a Bash shell script - by example

Forum rules
Before you post please read this

How to write a Bash shell script - by example

Postby Habitual on Tue Jul 19, 2011 11:56 pm

Every Bash shell script needs 2 basic things to do it's work on its own. The shebang (or hashbang) and the executable file attribute.
Since EVERY shell script needs these 2 attributes, it might be a good idea to have a script that can do those 2 things for you.

What this script does is adds the shebang (#!/bin/bash) into a file and makes the file executable (700).
This script will help you code other scripts.

Code: Select all
#!/bin/bash
# Purpose:   Inserts #!/bin/bash into a file and makes it executable
# Usage:    scriptname /path/to/file.sh
# Author:    JJ/Habitual
# Date:    Tue Jul 19, 2011
# Version:    1311133977
# Disclaimer:   Use it, abuse it, just don't lose it.
MINPARAMS=1

if [ -n "$1" ]
then
echo "#!/bin/bash" > $1
chmod 700 "$1"
echo File $1 is now an executable and ready to be edited.
fi

if [ $# -lt "$MINPARAMS" ]
then
  echo Usage: `basename $0` /path/to/script.sh
fi 
exit 0


Save it where ever you like.
Code: Select all
chmod 700 /path/to/saved/script.sh

Then run it, it will let you know what you need to do to use it.

Enjoy! Now let's code something!!!
Last edited by Oscar799 on Thu Jul 21, 2011 6:15 am, edited 1 time in total.
Reason: Stickied
What's a landing but a take off in reverse?
My DorkBlog
shellshock.conf for fail2ban 0.8.10
User avatar
Habitual
Level 8
Level 8
 
Posts: 2182
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Linux Mint is funded by ads and donations.
 

Re: How to write a Bash shell script - by example

Postby richyrich on Wed Jul 20, 2011 11:51 am

+1 vote to make this a bash sticky.

- hey new bashers, a keeper, a must have ! read it, figure it out, understand it ! . . li'l nuggets inside . . :mrgreen:

regards, richy
User avatar
richyrich
Level 13
Level 13
 
Posts: 4635
Joined: Mon May 04, 2009 8:31 pm

Re: How to write a Bash shell script - by example

Postby SimonTS on Thu Jul 21, 2011 8:26 am

Thanks for that Habitual. Looks simple enough - but doesn't mean I'd have a hope in hell of creating it myself from scratch :shock:

I've got to try and put some time aside to sit down and learn some of this stuff.
User avatar
SimonTS
Level 6
Level 6
 
Posts: 1272
Joined: Thu Feb 24, 2011 5:19 pm
Location: Bristol, United Kingdom

Re: How to write a Bash shell script - by example

Postby Habitual on Thu Jul 21, 2011 8:59 am

SimonTS:

You can do it.
PM me if you get in a pinch.

Today or Tomorrow I'll explore functions (),
Oh Boy!

I'm no programmer either, ya know, but I have a avid affection for writing shell scripts. I live for the "A-Ha!" moment when something I crafted/created works.
I am an input junkie that needs visual and immediate feedback during the course of my daily work.
That and I hate typing (after 17 years of it).
What's a landing but a take off in reverse?
My DorkBlog
shellshock.conf for fail2ban 0.8.10
User avatar
Habitual
Level 8
Level 8
 
Posts: 2182
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Re: How to write a Bash shell script - by example

Postby Pilosopong Tasyo on Thu Jul 21, 2011 9:37 am

Another way to accomplish this sans the script-to-create-a-script (ok, that sounded recursively redundant...) is to use the Templates folder to store a boilerplate script. Create a text file, put the shebang line and whatever other code fragment/comment lines/etc. that you want to put, save the file -- e.g. filename = "Shell Script" -- in the ~/Templates folder and apply the execute bit.

Here's a sample boilerplate I use:

/home/administrator/Templates/Shell Script
Code: Select all
#!/bin/sh

##########
#
# Project     :
# Started     :
# Author      :
# Module      :
# Description :
#
##########

# start of code here

# EOF

When you want to create a new script, fire up the file browser, right-click an empty place on the right pane > Create a Document > Shell Script. Rename, open in the text editor, and start coding.

Applies to Gnome.
Last edited by Pilosopong Tasyo on Thu Jul 21, 2011 9:41 am, edited 1 time in total.
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1151
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: How to write a Bash shell script - by example

Postby Habitual on Thu Jul 21, 2011 9:41 am

Pilosopong Tasyo:

Clever alternative! I like that. :wink:
What's a landing but a take off in reverse?
My DorkBlog
shellshock.conf for fail2ban 0.8.10
User avatar
Habitual
Level 8
Level 8
 
Posts: 2182
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Re: How to write a Bash shell script - by example

Postby beer-in-box on Mon Sep 26, 2011 12:30 pm

Please delete this post if this is not the place to ask and let me know so that I can ask the question properly :)

Is BASH a programming language itself or is it based on something and not a complete language?

EDIT: I am trying to learn Java, and this was supposed to be a hobby, but Java proved to be more than I thought, so I want to know what I am dealing with if I start to try to learn this :)
beer-in-box
Level 1
Level 1
 
Posts: 33
Joined: Thu Sep 22, 2011 8:28 am

Re: How to write a Bash shell script - by example

Postby Habitual on Mon Sep 26, 2011 4:45 pm

Bash is not a "programming" "language" in that it does not have an interpreter.
Bash is a command processor.

https://secure.wikimedia.org/wikipedia/ ... d_language and
https://secure.wikimedia.org/wikipedia/ ... gain_shell

HTH,
What's a landing but a take off in reverse?
My DorkBlog
shellshock.conf for fail2ban 0.8.10
User avatar
Habitual
Level 8
Level 8
 
Posts: 2182
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Re: How to write a Bash shell script - by example

Postby Habitual on Mon Sep 26, 2011 4:55 pm

Habitual wrote:... in that it does not have an interpreter.
or compiler. :)
What's a landing but a take off in reverse?
My DorkBlog
shellshock.conf for fail2ban 0.8.10
User avatar
Habitual
Level 8
Level 8
 
Posts: 2182
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Re: How to write a Bash shell script - by example

Postby cfaj on Sat May 11, 2013 7:56 am

Habitual wrote:Bash is not a "programming" "language" in that it does not have an interpreter.
Bash is a command processor.

Bash is an interpreter (that's how it executes scripts).
It is a programming language; "script" and "program" are synonymous.
cfaj
Level 1
Level 1
 
Posts: 3
Joined: Sun May 05, 2013 6:13 pm

Re: How to write a Bash shell script - by example

Postby Babbleshack on Mon Jul 22, 2013 12:29 pm

:P
excellent, I made the script include the extra comments.
great bit of code thanks much! :mrgreen:
Code: Select all
NOW=$(date +"%Y-%m-%d")


if [ -n "$1" ]
then
   echo "#!/bin/bash
# Purpose:
# Usage:
# Author: Babbleshack
# Date: ${NOW}
# Version:
# Disclaimer: " > $1
   chmod 700 "$1"
   echo FILE $1 is now an executable and ready to be edited
fi


-Babble
Babbleshack
Level 1
Level 1
 
Posts: 1
Joined: Mon Jul 22, 2013 12:18 pm
Location: Manchester, England

Re: How to write a Bash shell script - by example

Postby Habitual on Thu Aug 01, 2013 5:36 pm

Of course, if you're into the shell as much as I am, you could use this in your ~/.vimrc
Code: Select all
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod 700 <afile>

Any file.{ext} that has any shebang (perl, python, bash, whatever) will be made 700 upon exiting vi[m].

Enjoy.
What's a landing but a take off in reverse?
My DorkBlog
shellshock.conf for fail2ban 0.8.10
User avatar
Habitual
Level 8
Level 8
 
Posts: 2182
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Re: How to write a Bash shell script - by example

Postby tadaensylvermane on Thu Apr 10, 2014 12:50 am

This script is the total lazy mode. I love it!!! Never occured to me to do something like this before.
tadaensylvermane
Level 2
Level 2
 
Posts: 71
Joined: Tue Jun 25, 2013 8:50 pm

Re: How to write a Bash shell script - by example

Postby Habitual on Thu Apr 10, 2014 11:28 am

tadaensylvermane wrote:This script is the total lazy mode. I love it!!! Never occured to me to do something like this before.

"Avoiding Repetitive Tasks" is the hallmark of any good systems administrator.

If you have to type it more than 3 times, write a script/alias/function.
What's a landing but a take off in reverse?
My DorkBlog
shellshock.conf for fail2ban 0.8.10
User avatar
Habitual
Level 8
Level 8
 
Posts: 2182
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 3 guests