How to write a Bash shell script - by example

Forum rules
Before you post please read how to get help
User avatar
Habitual
Level 12
Level 12
Posts: 4007
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

How to write a Bash shell script - by example

Postby Habitual » 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: `pwd`/`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 Habitual on Mon Jul 06, 2015 5:27 pm, edited 2 times in total.
Do you realize how hard it is for me to explain to people that I know you?

User avatar
richyrich
Level 16
Level 16
Posts: 6795
Joined: Mon May 04, 2009 8:31 pm

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

Postby richyrich » 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
Image

User avatar
SimonTS
Level 6
Level 6
Posts: 1271
Joined: Thu Feb 24, 2011 5:19 pm
Location: Bristol, United Kingdom

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

Postby SimonTS » 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
Habitual
Level 12
Level 12
Posts: 4007
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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

Postby Habitual » 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).
Do you realize how hard it is for me to explain to people that I know you?

User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1366
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

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

Postby Pilosopong Tasyo » 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
Habitual
Level 12
Level 12
Posts: 4007
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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

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

Pilosopong Tasyo:

Clever alternative! I like that. :wink:
Do you realize how hard it is for me to explain to people that I know you?

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 beer-in-box » 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 :)

User avatar
Habitual
Level 12
Level 12
Posts: 4007
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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

Postby Habitual » 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,
Do you realize how hard it is for me to explain to people that I know you?

User avatar
Habitual
Level 12
Level 12
Posts: 4007
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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

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

Habitual wrote:... in that it does not have an interpreter.
or compiler. :)
Do you realize how hard it is for me to explain to people that I know you?

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 cfaj » 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.

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 Babbleshack » 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

User avatar
Habitual
Level 12
Level 12
Posts: 4007
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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

Postby Habitual » 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.
Do you realize how hard it is for me to explain to people that I know you?

tadaensylvermane
Level 2
Level 2
Posts: 78
Joined: Tue Jun 25, 2013 8:50 pm

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

Postby tadaensylvermane » 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.

User avatar
Habitual
Level 12
Level 12
Posts: 4007
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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

Postby Habitual » 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.
Do you realize how hard it is for me to explain to people that I know you?

Tim_Olaguna
Level 1
Level 1
Posts: 17
Joined: Mon Nov 03, 2008 2:58 pm
Location: Sacramento, California, USA

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

Postby Tim_Olaguna » Thu May 28, 2015 5:08 pm

Habitual wrote: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.


Hum... I tried creating a /.vimrc file with the specified code [I copied and pasted the one entire line into a new text file created with 'pluma']. But upon trying to use 'vi' to create a new executable file I immediately received a message to the effect that coding was not use-able in the current bash environment. I am running Linux Mint 17.1. Do you have any thoughts as to why I might be getting such a message and how it might be mitigated?

Thank you in advance for your time and wisdom.
Cheerily Yours,
Tim O'Laguna

User avatar
Habitual
Level 12
Level 12
Posts: 4007
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

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

Postby Habitual » Thu May 28, 2015 7:21 pm

Tim_Olaguna wrote:Hum... I tried creating a /.vimrc file with the specified code [I copied and pasted the one entire line into a new text file created with 'pluma']. But upon trying to use 'vi' to create a new executable file I immediately received a message to the effect that coding was not use-able in the current bash environment. I am running Linux Mint 17.1. Do you have any thoughts as to why I might be getting such a message and how it might be mitigated?

Tim:
Sure, if you create a ~/.vimrc file and you are certain that you have

Code: Select all

au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod 700 <afile>

then next time, try this:

vim x.sh # and the first line has to have a

Code: Select all

#!/bin/bash

# for .vimrc to work, this script has to have a shebang, be it bash, perl, python...

See this vidclip for an actual walk through.
Do you realize how hard it is for me to explain to people that I know you?

tolkien
Level 2
Level 2
Posts: 73
Joined: Sat Nov 14, 2015 9:50 am

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

Postby tolkien » Thu Dec 17, 2015 3:52 am

You can also give your script as an input to the bash interpreter:
bash /path/to/script
This way you don't need the shebang line and also not the chmod permission.
Works for other interpreter languages too, such as perl and python.

User avatar
xenopeek
Level 23
Level 23
Posts: 19023
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Postby xenopeek » Sat Dec 19, 2015 3:09 am

Here's how I do it. In your home directory you'll find the subdirectory called Templates. Files that you put here will become available in your file manager's Create New Document submenu that you find in the File menu and in the context menu (right-click). So create a file in the Templates directory called "Bash" for example, put the shebang line and any other default lines you want in there, and change its permissions to be executable. Now any time you need a new bash file you can select it from the Create New Document menu.

You can organize your templates in subdirectories. I have a subdirectory called Scripts where I have a template for Bash and for Python for example.
Image

User avatar
Fred Barclay
Level 9
Level 9
Posts: 2980
Joined: Sat Sep 13, 2014 11:12 am
Location: Bumping around in the bush

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

Postby Fred Barclay » Sat Dec 19, 2015 9:25 pm

Hi xeno. Great idea! Just one incredibly minor question.
Is it necessary to change the permissions of the templates to executable? None of mine are, and they show up just fine under "Create New Document."

Code: Select all

fred@aussie! ~/Templates $ ls -al
total 24
drwxr-xr-x  2 fred fred 4096 Dec 20 12:23 .
drwxr-xr-x 62 fred fred 4096 Dec 20 02:15 ..
-rw-r--r--  1 fred fred   65 Dec 20 12:16 bash.sh
-rw-r--r--  1 fred fred   54 Dec 20 12:22 c.c
-rw-r--r--  1 fred fred   77 Dec 20 12:18 python2.py
-rw-r--r--  1 fred fred   76 Dec 20 12:19 python3.py


Thanks to Habitual for starting this and to the others too! :)
Image
Visit https://forums.linuxmint.com/viewtopic.php?f=60&t=182272 to thank others who helped you!

User avatar
xenopeek
Level 23
Level 23
Posts: 19023
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Postby xenopeek » Sun Dec 20, 2015 3:09 am

If you change script files templates to executable, the script files created with "create new document" will be executable as well. Saves you having to change permissions on each newly created script file.
Image


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 1 guest