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
User avatar
Habitual
Level 7
Level 7
 
Posts: 1609
Joined: Sun Nov 21, 2010 8:31 pm
Location: uid=0(root) gid=0(root) groups=0(root)

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: 4644
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).
User avatar
Habitual
Level 7
Level 7
 
Posts: 1609
Joined: Sun Nov 21, 2010 8:31 pm
Location: uid=0(root) gid=0(root) groups=0(root)

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.
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 5
Level 5
 
Posts: 800
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:
User avatar
Habitual
Level 7
Level 7
 
Posts: 1609
Joined: Sun Nov 21, 2010 8:31 pm
Location: uid=0(root) gid=0(root) groups=0(root)

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,
User avatar
Habitual
Level 7
Level 7
 
Posts: 1609
Joined: Sun Nov 21, 2010 8:31 pm
Location: uid=0(root) gid=0(root) groups=0(root)

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. :)
User avatar
Habitual
Level 7
Level 7
 
Posts: 1609
Joined: Sun Nov 21, 2010 8:31 pm
Location: uid=0(root) gid=0(root) groups=0(root)

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

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