# Looking to learn bash scripting



## hockey97 (Mar 9, 2012)

Hi, I need to learn how to create my own bash scripts. 

What I plan on doing is that I am starting my hosting services.

I need to write a bash script that will detect NAS hard drives. Then once detected and the script knows how much space is used and if it's not full then if my other NAS hard drives becomes full, the script will make sure there is another NAS drive that is empty. Then they will create folders and then store any new files and images on that new one. I basically want the bash script to monitor my NAS hard drives on the internal network. If it gets close to being full to send me a message sms text messages. Once the NAS hard drives are full then it will search for another empty NAS and make changes to use that NAS drive.

In my website I will need to have this bash script to go in and change paths in my PHP scripts. So the php scripts will save files and images on these NAS hard drives.


----------



## shitson (Mar 9, 2012)

A start would be invest in a good book, *I* would recommend: http://www.amazon.com/Learning-bash...9658/ref=sr_1_1?ie=UTF8&qid=1331261031&sr=8-1


----------



## SirDice (Mar 9, 2012)

Forget bash, learn how to script the standard bourne shell. Keep in mind that bash isn't standard on FreeBSD.

http://www.grymoire.com/Unix/Sh.html


----------



## hockey97 (Mar 9, 2012)

SirDice said:
			
		

> Forget bash, learn how to script the standard bourne shell. Keep in mind that bash isn't standard on FreeBSD.
> 
> http://www.grymoire.com/Unix/Sh.html



So bourne shell is what FreeBSD 8.1 uses? 

*C*an you reference me where exactly I should look to learn how to first detect a NAS hard drive and then how can I use this to manipulate my php files?

I would need some way to open up my php files and then replace all data paths inside the file with the current NAS hard drive directories I newly established.


----------



## UNIXgod (Mar 10, 2012)

hockey97 said:
			
		

> So bourne shell is what FreeBSD 8.1 uses?



FreeBSD uses ash (Almquist Shell). It's a small POSIX compliant bourne shell.

"The UNIX Programming Environment" Kernghan/Pike teaches you how to create shell scripts.

It would be a good idea to read man sh().

If you need a cheat sheet on what is portable; here are those commands:
POSIX Shell And Utilities
~


----------



## shitson (Mar 10, 2012)

For a System Admin would bash not be better? Obviously this is going to limit transportability of code if the machine doesn't have bash installed. Is this a vi vs emacs argument; i.e bloated & not builtin?


----------



## tingo (Mar 10, 2012)

UNIXgod said:
			
		

> FreeBSD uses ash (Almquist Shell).



I didn't know about this, so I had to look it up. From Wikipedia (Ash shell):


> Derivatives of ash are installed as the default shell (/bin/sh) on FreeBSD, NetBSD, DragonFly BSD and Minix.


So it isn't *the* Almquist shell.


----------



## UNIXgod (Mar 10, 2012)

shitson said:
			
		

> For a System Admin would bash not be better? Obviously this is going to limit transportability of code if the machine doesn't have bash installed. Is this a vi vs emacs argument; i.e bloated & not builtin?



A system admin will create their own policy. Some policies are good; some bad. It would be pragmatic for a user coming from linux to use bash if they already are programming bash. Using the system sh provides a very small learning curve. When something more robust is needed there are scripting languages which pick up where it leaves off( i.e. perl,ruby).

It's not really a debate. But if I was to compare it to one I would state it's closer to using Bostic's nvi (the system vi) vs vim. Also the book I referenced actually uses ed() as it's actually a sys5 book.

OP read the man page. Take the time to learn regular expressions as they are the basis of so many utilities on the system as well as third party scripting languages. Aside from that regex will provide you a skill for your lifetime. sed() and awk() are also programs you will likely want to explore. Pipes, redirection and quoting are also important to understand.

Another resource worth noting on shell scripting:
http://www.shelldorado.com/



			
				tingo said:
			
		

> I didn't know about this, so I had to look it up. From Wikipedia (Ash shell):
> 
> So it isn't *the* Almquist shell.



It was the one created after Bostic suggested to removal of any ATT specific code for opening up NET/2 at Berkeley. More information here:
http://rosettacode.org/wiki/Almquist_Shell

Would you expand on what you mean by "So it isn't *the* Almquist shell."


----------



## vand777 (Mar 11, 2012)

"Bash Cookbook: Solutions and Examples for Bash Users" by Carl Albing, JP Vossen and Cameron Newham.

You will find a lot of practical solutions to many problems from real life in this book. It's really good.


----------



## jalla (Mar 11, 2012)

tingo said:
			
		

> I didn't know about this, so I had to look it up. From Wikipedia (Ash shell):
> 
> So it isn't *the* Almquist shell.



It certainly is if the manpage is to be believed


```
AUTHORS
     This version of sh was originally written by Kenneth Almquist.
```


----------



## wblock@ (Mar 11, 2012)

shitson said:
			
		

> For a System Admin would bash not be better? Obviously this is going to limit transportability of code if the machine doesn't have bash installed. Is this a vi vs emacs argument; i.e bloated & not builtin?



It's a judgement call.  bash provides a superset of the basic sh(1) functionality.  Some of us believe that if it can't be done easily in portable sh(1), then you've reached the practical limits of shell scripts anyway, and a more powerful language will pay for itself in reduced programming, debugging, and maintenance time.


----------



## UNIXgod (Mar 11, 2012)

One last note to the OP. test()(1) which can be seen by actually using its alias [ is worth reading. Type *man [* or *man test* to see it.

Initially the original system had every command external and acted like a pure repl. so typing in the conditionals like if/elif/fi or  was actually in /usr/bin/if or /usr/bin/fi and so on. This would also include test which /usr/bin/[ was linked to. (it could have been sbin btw the the concept remains)

Currently the shell provides these now as builtin commands. You can view the list of these via man builtin()(1)


----------



## hockey97 (Jul 14, 2012)

Thanks for the responses.The purpose was to learn shell scripting for the FreeBSD 8.1 OS.
I myself already know batch and do know C and C++ and also web programming languages like PHP,js etc. Well I need to ask you if it's worth learning any shell scripting?

I am trying to figure out what I would like to do to my server. I own a server and had not choice but to use FreeBSD since my server is a monster. Before using FreeBSD 8.1 I was using Ubuntu Linux. I used Ubuntu Linux for about 4 years and learned the commands needed. Anyways I got to a point where I was happy with using Linux. Then I built my own monster server and was excited to use Ubuntu server edition. Then found out it doesn't support it. I then search the internet and found out that I had two choices use FreeBSD 8.1 or use windows server.

So, I would like to know if it's worth learning a shell scripting language? Or should I just focus on making C or C++ programs for my server? Will shell scripting help me with my programming in FreeBSD?


----------



## UNIXgod (Jul 15, 2012)

hockey97 said:
			
		

> Well I need to ask you if it's worth learning any shell scripting?
> 
> So, I would like to know if it's worth learning a shell scripting language? Or should I just focus on making C or C++ programs for my server? Will shell scripting help me with my programming in FreeBSD?



The simple answer is yes it's worth learning. Grokking UNIX automata is just another skill-set to add to your programming repertoire.


----------



## UNIXgod (Jul 15, 2012)

tingo said:
			
		

> I didn't know about this, so I had to look it up. From Wikipedia (Ash shell):
> 
> So it isn't *the* Almquist shell.



FreeBSD source may be a bit more accurate than wikipedia:

`% grep Almquist /usr/src/bin/sh/*`


```
/usr/src/bin/sh/TOUR:               [B]Copyright 1989 by Kenneth Almquist[/B].
/usr/src/bin/sh/alias.c: * Kenneth Almquist.
/usr/src/bin/sh/alias.h: * Kenneth Almquist.
/usr/src/bin/sh/arith_yacc.c: * Kenneth Almquist.
/usr/src/bin/sh/arith_yacc.h: * Kenneth Almquist.
/usr/src/bin/sh/arith_yylex.c: * Kenneth Almquist.
/usr/src/bin/sh/builtins.def:# Kenneth Almquist.
/usr/src/bin/sh/cd.c: * Kenneth Almquist.
/usr/src/bin/sh/error.c: * Kenneth Almquist.
/usr/src/bin/sh/error.h: * Kenneth Almquist.
/usr/src/bin/sh/eval.c: * Kenneth Almquist.
/usr/src/bin/sh/eval.h: * Kenneth Almquist.
/usr/src/bin/sh/exec.c: * Kenneth Almquist.
/usr/src/bin/sh/exec.h: * Kenneth Almquist.
/usr/src/bin/sh/expand.c: * Kenneth Almquist.
/usr/src/bin/sh/expand.h: * Kenneth Almquist.
/usr/src/bin/sh/histedit.c: * Kenneth Almquist.
/usr/src/bin/sh/init.h: * Kenneth Almquist.
/usr/src/bin/sh/input.c: * Kenneth Almquist.
/usr/src/bin/sh/input.h: * Kenneth Almquist.
/usr/src/bin/sh/jobs.c: * Kenneth Almquist.
/usr/src/bin/sh/jobs.h: * Kenneth Almquist.
/usr/src/bin/sh/mail.c: * Kenneth Almquist.
/usr/src/bin/sh/mail.h: * Kenneth Almquist.
/usr/src/bin/sh/main.c: * Kenneth Almquist.
/usr/src/bin/sh/main.h: * Kenneth Almquist.
/usr/src/bin/sh/memalloc.c: * Kenneth Almquist.
/usr/src/bin/sh/memalloc.h: * Kenneth Almquist.
/usr/src/bin/sh/miscbltin.c: * Kenneth Almquist.
/usr/src/bin/sh/mkbuiltins:# Kenneth Almquist.
/usr/src/bin/sh/mkinit.c: * Kenneth Almquist.
/usr/src/bin/sh/mknodes.c: * Kenneth Almquist.
/usr/src/bin/sh/mksyntax.c: * Kenneth Almquist.
/usr/src/bin/sh/mktokens:# Kenneth Almquist.
/usr/src/bin/sh/mystring.c: * Kenneth Almquist.
/usr/src/bin/sh/mystring.h: * Kenneth Almquist.
/usr/src/bin/sh/nodes.c.pat: * Kenneth Almquist.
/usr/src/bin/sh/nodetypes:# Kenneth Almquist.
/usr/src/bin/sh/options.c: * Kenneth Almquist.
/usr/src/bin/sh/options.h: * Kenneth Almquist.
/usr/src/bin/sh/output.c: * Kenneth Almquist.
/usr/src/bin/sh/output.h: * Kenneth Almquist.
/usr/src/bin/sh/parser.c: * Kenneth Almquist.
/usr/src/bin/sh/parser.h: * Kenneth Almquist.
/usr/src/bin/sh/redir.c: * Kenneth Almquist.
/usr/src/bin/sh/redir.h: * Kenneth Almquist.
/usr/src/bin/sh/sh.1:.\" Kenneth Almquist.
/usr/src/bin/sh/sh.1:.An Kenneth Almquist .
/usr/src/bin/sh/shell.h: * Kenneth Almquist.
/usr/src/bin/sh/show.c: * Kenneth Almquist.
/usr/src/bin/sh/trap.c: * Kenneth Almquist.
/usr/src/bin/sh/trap.h: * Kenneth Almquist.
/usr/src/bin/sh/var.c: * Kenneth Almquist.
/usr/src/bin/sh/var.h: * Kenneth Almquist.
```


----------



## mix_room (Jul 16, 2012)

On a side note: are you sure that this is the correct solution for your problem? It seems to me that you would be almost as well off running some form of software-RAID on the drives. This would give you both redundancy and space sharing.


----------



## anomie (Aug 2, 2012)

wblock@ said:
			
		

> Some of us believe that if it can't be done easily in portable sh(1), then you've reached the practical limits of shell scripts anyway, and a more powerful language will pay for itself in reduced programming, debugging, and maintenance time.



That's probably a wise philosophy. bash(1) offers a number of features to make it more like a "real" programming language, but it also comes complete with some scary, unintuitive pitfalls: 


 http://mywiki.wooledge.org/BashPitfalls/
 http://tldp.org/LDP/abs/html/gotchas.html

The (arguably) small feature improvements over sh(1) don't exactly make a compelling argument for spending time on bash(1) rather than something equally ubiquitious (and _way_ more powerful), like perl. 



			
				hockey97 said:
			
		

> Well I need to ask you if it's worth learning any shell scripting?



Yes. Bourne shell and/or bash(1) are fantastic for automating small sets of tasks.



			
				mix_room said:
			
		

> On a side note: are you sure that this is the correct solution for your problem?



Indeed. (And I just wanted to yap about the philosophical side of the discussion for a moment.)


----------

