HOWTO: Scripting - Great reference

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
rarsa
Posts: 3053
Joined: Sun 29 May 2005, 20:30
Location: Kitchener, Ontario, Canada
Contact:

HOWTO: Scripting - Great reference

#1 Post by rarsa »

One of the give aways in my LUG was a very handy Bash pocket reference

Today, by chance, I found the same reference as a PDF.

If you do shell scripting you sure want this reference at your side.

Although it is bash specific, most of it applies to Puppy's default shell.

http://database.sarang.net/study/bash/bash.pdf

And now that you are here. You may also be interested in this Bourne shell tutorial:

http://steve-parker.org/sh/sh.shtml

I've read many, but this one is very well organized and presented.

(have I mentioned that I met Steve Bourne and we chatted for a few minutes while waiting for drinks at a conference?)

kethd
Posts: 451
Joined: Thu 20 Oct 2005, 12:54
Location: Boston MA USA

#2 Post by kethd »

BASH is the default command line shell in current Puppy, at least when you are within Xwin -- and future Puppy is said to be all bash all the time, ash will be deprecated...

puppypilgrim
Posts: 85
Joined: Tue 07 Jun 2005, 06:32
Location: Horsefly, Canada
Contact:

#3 Post by puppypilgrim »

Thank you for posting such a handy reference. Scouring the net only yields bits and pieces.

User avatar
jmarsden
Posts: 265
Joined: Sat 31 Dec 2005, 22:18
Location: California, USA

#4 Post by jmarsden »

I've already mentioned these in threads elsewhere on these forums, I think, but since we have a new thread just for Bash documentation and tutorials, here they are:

The canonical source of info on Bash scripting is the Bash Reference Manual . In the Linux world, a decent Bash scripting tutorial is the Advanced Bash Scripting Guide. This is not actually "advanced", in that it starts from basics, so scripting newcomers should not be put off by its title. It's first paragraph reads:[quote]This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an intermediate/advanced level of instruction . . . all the while sneaking in little snippets of UNIX

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#5 Post by MU »

Image

I wrote a shellscript "mini-calc", that might be helpfull.

It does:
- Open itself in a coloured rxvt when clicked in ROX.
- inputs words
- inputs numbers only
- has a kind of "main-menu".
- runs a external program that is not so easy to use, the commandline-calculator "bc".

"bc" can do floatingpoint-calculations, while the internal bash-calculations are limited to integer.

This program is not "better" than bc, in fact it is "stupid",as it just allows to add 2 numbers.
But it is not inteded to be a good calculator, but just to be a demo how you can realize the points mentioned above.

It requires 2 Dotpups:
bc and nohup
bc also might require libreadline4

nohup runs a command (in the example leafpad or beaver to show the sourcecode), without closing it, when the "calling" program (the rxvt-window) is closed.

http://dotpups.de/dotpups/System_Utilities/nohup.pup (6 kb)
http://dotpups.de/dotpups/System_Utilit ... ulator.pup (48 kb)
http://noforum.de/dotpups/libreadline-so-4.pup (82 kb)

And here is the "mini-calc" -script:

Code: Select all

#!/bin/bash

#------------------------------------------------
#-- start in a new console-window
#------------------------------------------------

if [ "$1" != "rxvt" ];then

  rxvt -title mini-calc +sb -cr green -bg yellow -geometry 40x10+200+200 -e $0 rxvt &
  exit 0

fi

#-------------------------------------
#-- simple "input" -function
#-------------------------------------

readkeys(){

  read keypress
  if [ "$keypress" == "exit" ];then
    xmessage -center goodbye!
    exit 0
  fi

}

#-------------------------------------------------------------------------------
#-- simple "input" -function with check for numbers only
#-------------------------------------------------------------------------------
readnumber(){

  clear
  echo -e "enter \"exit\" to quit.\n"
  echo -e $infotext
  read keypress
  if [ "$keypress" == "exit" ];then
    xmessage -center goodbye!
    exit 0
  fi

  #-- just allow numbers and decimal-point
  numcheck=`echo $keypress | sed "s/[0-9]//g" | sed "s/\.//"`

  if [ $numcheck ]; then
   readnumber
  fi
}

#--------------------------------------------------------------------------
#--- addition of 2 values with external calculator "bc"
#-------------------------------------------------------------------------- 
runbcplus(){

  bc -l<<END-OF-INPUT
  $va+$vb
  quit
END-OF-INPUT
}

#-------------------------
#-- main-script 
#-------------------------

#-------------------------------------------------------
#-- read 2 numbers and print the sum
#-------------------------------------------------------


infotext="welcome to mini-calc\nenter first number"
readnumber
va=$keypress

infotext="enter second number"
readnumber
vb=$keypress

clear
echo Result: $va + $vb = `runbcplus`
echo

#-------------------------------------------------------------------.
#-- endless loop until user exits ("main-menu")
#-------------------------------------------------------------------

while [ "$keypress" != "exit" ];do

  echo enter \"exit\" to quit
  echo enter \"code\" to view the source
  echo enter \"r\" to restart again
  readkeys
  if [ "$keypress" == "code" ];then
    nohup leafpad $0 1>/dev/null || beaver $0 1>/dev/null&
  fi
  if [ "$keypress" == "r" ];then
    $0 rxvt
  fi
  clear
done
Save as /usr/local/bin/minicalc
Make it executable:
chmod 755 /usr/local/bin/minicalc
Click it in ROX.

Germans might like this introduction:
http://www.linuxfibel.de/kapitel2.htm
http://www.linuxfibel.de/kapitel7.htm

Mark

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

Ash is a smaller, simplified Bash

#6 Post by Lobster »

kethd wrote:BASH is the default command line shell in current Puppy, at least when you are within Xwin -- and future Puppy is said to be all bash all the time, ash will be deprecated...
Many thanks to MU for his efforts in educating us about scripting. :)

This is my understanding which needs further clarification:

Puppy uses Ash and Busybox and the full Bash commands where these are required for a more complete implementation.
Bash is a (large) collection of C commands that can be called from the command line. Many of these commands are with their most used functions linked in one C program - Busybox. Thus Ash is a smaller, simplified Bash.

As far as I am aware the intention at one point was to move to the full Bash commands BUT it was considered wasteful of space resources and so this has not been implemented.

Is that right? :?
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
jmarsden
Posts: 265
Joined: Sat 31 Dec 2005, 22:18
Location: California, USA

Re: Ash is a smaller, simplified Bash

#7 Post by jmarsden »

Lobster wrote:Puppy uses Ash and Busybox and the full Bash commands where these are required for a more complete implementation.
Bash is a (large) collection of C commands that can be called from the command line. Many of these commands are with their most used functions linked in one C program - Busybox. Thus Ash is a smaller, simplified Bash.
Well, Bash is just the shell itself. Not the collection of executables the shell can run if you ask it to. The only commands that are part of bash are the "built in" ones. In the MSDOS world these would be the "internal commands". Type the word "help" at a bash shell prompt to see its list of these. You can add aliases and shell functions to these, and they will also run "inside" Bash itself.

All other commands you can execute from Bash (or any other shell, in general) by typing their name are scripts or executable programs that are on disk somewhere and in a directory in your $PATH . In the MSDOS world they are "external commands" Some of those (many of the normal Unix commands) are written in C, but others are not.
As far as I am aware the intention at one point was to move to the full Bash commands BUT it was considered wasteful of space resources and so this has not been implemented.
Since the Bash shell is in Puppy, the full set of (internal) Bash commands is there now. As for replacing all BusyBox commands with their individual "normal Linux" equivalents, that could be wasteful of disk space for small systems. What seems to be happening is more that as a particular Busybox command is found to lack some option or feature that Puppy needs, the "real" equivalent command is added to Puppy instead of the "cut down" Busybox version.

Oh, and strictly speaking, ash is the Almquist shell, which is not a cut-down bash, but rather a free implementation of a shell that tries to be as close as possible to the original Bourne shell. Most Linuxes, I think including Puppy, use a version of ash derived from the NetBSD /bin/sh codebase. In general, coding shell scripts for ash will ensure wide compatibility across multiple Unix and Unix-like systems, coding for Bash is more pleasant (you have more language features!), but often not quite as portable. If you do write a script that uses Bash features, be sure to put #!/bin/bash at the top of it, so that it will work on systems where Bash is installed but /bin/sh is ash or a commercial Bourne shell.

That may be more detail that you really wanted... :-)

Jonathan

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

Re: Ash is a smaller, simplified Bash

#8 Post by Lobster »

jmarsden wrote: That may be more detail that you really wanted... :-)

Jonathan
That is very useful :) thanks for clarifying
Now I just have to read this:
http://www.gnu.org/software/bash/manual/bashref.html

Wot no pictures? :wink:
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

Guest

Re: Ash is a smaller, simplified Bash

#9 Post by Guest »

Lobster wrote:Wot no pictures? :wink:
Maybe you could read http://www.tldp.org/LDP/abs/html/ first, the other document I linked to in my 10 Jan 2006 post in this thread. It may not quite have pictures, but it does have plenty of examples. The Bash Reference Manual really is just that... use it to refer to, when you need to do something you are pretty sure Bash can do, but you need the details on how. Use the LDP tutorial to learn the basics (OK, maybe a bit more than just the basics) of shell scripting first. Few people enjoy reading reference manuals cover to cover!

On many Linux systems (but not Puppy for size reasons) there is also the Bash man page, which perhaps lies somewhere between the tutorial and the ref manual -- and is what I used to learn Bash scripting!

Jonathan


User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

Here is a nice intro to bash

#11 Post by Lobster »

Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#12 Post by technosaurus »

And if you want to write _good_ scripts, there are some tips on this forum:
http://www.murga-linux.com/puppy/viewtopic.php?t=70238
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#13 Post by slavvo67 »

I like the "Do not start Bash scripting as root user" tip. Yes, I've had a bad time after not quoting variables and things. Not a big deal because I just re-install on the usb but it's still a pretty good tip.

The great thing about having a bunch of bash scripts around is that even if you're on a Linux system that's somewhat alien to you, that little bash script will (in most cases) still work perfectly. So, there's always a piece of home you take with you to the next distro (or Puppy).

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#14 Post by technosaurus »

the worst is when you have bad vision and you do something like this:

rm -rf / $HOME/.mozilla/

Guess what gets deleted??

Whatever you guessed was correct, because all of / will be deleted
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply