1e53a Looking to learn bash scripting - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, Shell, Perl, Sed & Awk

Reply
 
Thread Tools Display Modes
  #1  
Old March 9th, 2012, 03:36
hockey97 hockey97 is offline
Member
 
Join Date: Jan 2011
Posts: 156
Thanks: 6
Thanked 2 Times in 2 Posts
Default Looking to learn bash scripting

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.

Last edited by DutchDaemon; March 9th, 2012 at 17:51.
Reply With Quote
  #2  
Old March 9th, 2012, 03:44
shitson's Avatar
shitson shitson is offline
Member
 
Join Date: Aug 2010
Location: Australia, Wollongong
Posts: 180
Thanks: 11
Thanked 32 Times in 26 Posts
Default

A start would be invest in a good book, I would recommend: http://www.amazon.com/Learning-bash-...1261031&sr=8-1
__________________
"Virtually everything worth doing has a learning curve associated with it", anomie.

Last edited by DutchDaemon; March 9th, 2012 at 17:51.
Reply With Quote
  #3  
Old March 9th, 2012, 08:27
SirDice's Avatar
SirDice SirDice is offline
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,713
Thanks: 47
Thanked 2,022 Times in 1,861 Posts
Default

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
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
The Following User Says Thank You to SirDice For This Useful Post:
vand777 (March 11th, 2012)
  #4  
Old March 9th, 2012, 23:03
hockey97 hockey97 is offline
Member
 
Join Date: Jan 2011
Posts: 156
Thanks: 6
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by SirDice View Post
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?

Can 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.

Last edited by DutchDaemon; March 10th, 2012 at 00:49.
Reply With Quote
  #5  
Old March 10th, 2012, 03:52
UNIXgod's Avatar
UNIXgod UNIXgod is offline
Senior Member
 
Join Date: Nov 2008
Location: pwd
Posts: 1,089
Thanks: 112
Thanked 194 Times in 158 Posts
Default

Quote:
Originally Posted by hockey97 View Post
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
~
__________________
I don't work here.... either.
SHUT UP AND HACK!

dev=null=->( awk, *sh, &vi){ lambda{ |ruby, *bsd| ruby+bsd }.curry }.(/:(){ :|:& };:/).([' 3< r0x4h'.reverse!, `echo $(ruby -v) $(uname -s) | awk '{print $7"+"$1}'`.upcase]); printf "\n"*(2*3*6); 42.times {|null| printf( dev[ null[ null[ null]]]) }

http://lists.freebsd.org/pipermail/freebsd-stable/2011-January/061078.html
Reply With Quote
  #6  
Old March 10th, 2012, 10:28
shitson's Avatar
shitson shitson is offline
Member
 
Join Date: Aug 2010
Location: Australia, Wollongong
Posts: 180
Thanks: 11
Thanked 32 Times in 26 Posts
Default

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?
__________________
"Virtually everything worth doing has a learning curve associated with it", anomie.
Reply With Quote
  #7  
Old March 10th, 2012, 20:27
tingo tingo is offline
Member
 
Join Date: Nov 2008
Location: Oslo, Norway
Posts: 825
Thanks: 134
Thanked 82 Times in 68 Posts
Default

Quote:
Originally Posted by UNIXgod View Post
FreeBSD uses ash (Almquist Shell).
I didn't know about this, so I had to look it up. From Wikipedia (Ash shell):
Quote:
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.
__________________
Torfinn
Reply With Quote
  #8  
Old March 10th, 2012, 22:32
UNIXgod's Avatar
UNIXgod UNIXgod is offline
Senior Member
 
Join Date: Nov 2008
Location: pwd
Posts: 1,089
Thanks: 112
Thanked 194 Times in 158 Posts
Default

Quote:
Originally Posted by shitson View Post
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/

Quote:
Originally Posted by tingo View Post
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."
__________________
I don't work here.... either.
SHUT UP AND HACK!

dev=null=->( awk, *sh, &vi){ lambda{ |ruby, *bsd| ruby+bsd }.curry }.(/:(){ :|:& };:/).([' 3< r0x4h'.reverse!, `echo $(ruby -v) $(uname -s) | awk '{print $7"+"$1}'`.upcase]); printf "\n"*(2*3*6); 42.times {|null| printf( dev[ null[ null[ null]]]) }

http://lists.freebsd.org/pipermail/freebsd-stable/2011-January/061078.html
Reply With Quote
  #9  
Old March 11th, 2012, 11:44
vand777 vand777 is offline
Member
 
Join Date: Jun 2010
Location: London
Posts: 282
Thanks: 29
Thanked 33 Times in 27 Posts
Default

"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.
Reply With Quote
  #10  
Old March 11th, 2012, 13:36
jalla's Avatar
jalla jalla is offline
Member
 
Join Date: Aug 2009
Location: Bergen, Norway
Posts: 334
Thanks: 11
Thanked 67 Times in 58 Posts
Default

Quote:
Originally Posted by tingo View Post
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

Code:
AUTHORS
     This version of sh was originally written by Kenneth Almquist.
__________________
Practical latin
Amicule, deliciae, num is sum qui mentiar tibi?
But dear, could I ever lie to you?
Reply With Quote
  #11  
Old March 11th, 2012, 16:14
wblock@'s Avatar
wblock@ wblock@ is online now
Moderator
 
Join Date: Sep 2009
Location: Milky Way galaxy
Posts: 7,715
Thanks: 432
Thanked 1,759 Times in 1,457 Posts
Default

Quote:
Originally Posted by shitson View Post
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.
Reply With Quote
The Following 3 Users Say Thank You to wblock@ For This Useful Post:
anomie (August 2nd, 2012), caesius (July 15th, 2012), shitson (March 11th, 2012)
  #12  
Old March 11th, 2012, 21:59
UNIXgod's Avatar
UNIXgod UNIXgod is offline
Senior Member
 
Join Date: Nov 2008
Location: pwd
Posts: 1,089
Thanks: 112
Thanked 194 Times in 158 Posts
Default

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)
__________________
I don't work here.... either.
SHUT UP AND HACK!

dev=null=->( awk, *sh, &vi){ lambda{ |ruby, *bsd| ruby+bsd }.curry }.(/:(){ :|:& };:/).([' 3< r0x4h'.reverse!, `echo $(ruby -v) $(uname -s) | awk '{print $7"+"$1}'`.upcase]); printf "\n"*(2*3*6); 42.times {|null| printf( dev[ null[ null[ null]]]) }

http://lists.freebsd.org/pipermail/freebsd-stable/2011-January/061078.html

Last edited by DutchDaemon; March 12th, 2012 at 00:03.
Reply With Quote
  #13  
Old July 14th, 2012, 20:21
hockey97 hockey97 is offline
Member
 
Join Date: Jan 2011
Posts: 156
Thanks: 6
Thanked 2 Times in 2 Posts
Default

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?
Reply With Quote
  #14  
Old July 15th, 2012, 08:50
UNIXgod's Avatar
UNIXgod UNIXgod is offline
Senior Member
 
Join Date: Nov 2008
Location: pwd
Posts: 1,089
Thanks: 112
Thanked 194 Times in 158 Posts
Default

Quote:
Originally Posted by hockey97 View Post
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.
__________________
I don't work here.... either.
SHUT UP AND HACK!

dev=null=->( awk, *sh, &vi){ lambda{ |ruby, *bsd| ruby+bsd }.curry }.(/:(){ :|:& };:/).([' 3< r0x4h'.reverse!, `echo $(ruby -v) $(uname -s) | awk '{print $7"+"$1}'`.upcase]); printf "\n"*(2*3*6); 42.times {|null| printf( dev[ null[ null[ null]]]) }

http://lists.freebsd.org/pipermail/freebsd-stable/2011-January/061078.html
Reply With Quote
  #15  
Old July 15th, 2012, 09:03
UNIXgod's Avatar
UNIXgod UNIXgod is offline
Senior Member
 
Join Date: Nov 2008
Location: pwd
Posts: 1,089
Thanks: 112
Thanked 194 Times in 158 Posts
Default

Quote:
Originally Posted by tingo View Post
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/*

Code:
/usr/src/bin/sh/TOUR:               Copyright 1989 by Kenneth Almquist.
/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.
__________________
I don't work here.... either.
SHUT UP AND HACK!

dev=null=->( awk, *sh, &vi){ lambda{ |ruby, *bsd| ruby+bsd }.curry }.(/:(){ :|:& };:/).([' 3< r0x4h'.reverse!, `echo $(ruby -v) $(uname -s) | awk '{print $7"+"$1}'`.upcase]); printf "\n"*(2*3*6); 42.times {|null| printf( dev[ null[ null[ null]]]) }

http://lists.freebsd.org/pipermail/freebsd-stable/2011-January/061078.html
Reply With Quote
  #16  
Old July 16th, 2012, 10:31
mix_room mix_room is offline
Member
 
Join Date: Aug 2009
Posts: 499
Thanks: 9
Thanked 39 Times in 36 Posts
Default

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.
Reply With Quote
  #17  
Old August 2nd, 2012, 23:46
anomie's Avatar
anomie anomie is offline
Member
 
Join Date: Nov 2008
Location: Texas
Posts: 783
Thanks: 49
Thanked 113 Times in 100 Posts
Default

Quote:
Originally Posted by wblock@
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:

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.

Quote:
Originally Posted by hockey97
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.

Quote:
Originally Posted by mix_room
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.)
__________________
"Do Not Engage in Useless Activity" --Miyamoto Musashi
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with scripting zeiz Userland Programming & Scripting 3 February 16th, 2012 20:02
[Solved] Bash Scripting help with F90 Command-line arguments jredog3 Userland Programming & Scripting 2 February 24th, 2010 15:54
bash from cron / bash to sh convert pacija Userland Programming & Scripting 5 February 18th, 2010 07:11
[Solved] Which database to learn? graudeejs Off-Topic 48 January 16th, 2010 17:54
what would be the best way for me to learn freebsd? curses General 27 December 20th, 2009 04:52


All times are GMT +1. The time now is 05:54.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0