Bash shell scripting

Booting, installing, newbie
Message
Author
User avatar
une
Posts: 56
Joined: Fri 26 Jan 2007, 12:49
Location: Australia

Bash shell scripting

#1 Post by une »

I want to teach myself shell scripting in BASH.
How do I find out which shell I am currently in (ASH or BASH) from the command line?
How do I swap shells?
What is the difference between ASH and BASH?
Are these the only two shells on Puppy LInux 2.13?
Is there a command that will list all available shells?

User avatar
Gn2
Posts: 943
Joined: Mon 16 Oct 2006, 05:33
Location: virtual - Veni vidi, nihil est adpulerit

#2 Post by Gn2 »

These are all generic Linux queries - use GOOGLE !



echo $ SHELL

bash - ash - sh , etc etc etc

Yes

As default included, Only two in Puppy

User avatar
une
Posts: 56
Joined: Fri 26 Jan 2007, 12:49
Location: Australia

#3 Post by une »

I tried to change shell using
# chsh
but received the message
sh: chsh: command not found

why did this not work?

I also tried
# echo $SHELL
and was told
/bin/sh
so I guess the sh is default shell in use.

When I look at the contents of /bin I see ash and sh are shown in light blue text and bash in lime green text. What do these colour differences represent?

User avatar
trapster
Posts: 2117
Joined: Mon 28 Nov 2005, 23:14
Location: Maine, USA
Contact:

#4 Post by trapster »

ash is a symlink to busybox
sh is a symlink to bash
trapster
Maine, USA

Asus eeepc 1005HA PU1X-BK
Frugal install: Slacko
Currently using full install: DebianDog

User avatar
une
Posts: 56
Joined: Fri 26 Jan 2007, 12:49
Location: Australia

#5 Post by une »

Thanks. Does this mean symbolic links are shown in light blue text and actual applications shown in lime green text?
I don't know why chsh command did not work.

User avatar
rarsa
Posts: 3053
Joined: Sun 29 May 2005, 20:30
Location: Kitchener, Ontario, Canada
Contact:

#6 Post by rarsa »

une wrote:I don't know why chsh command did not work.
First, I read that you said that you wanted to learn to script in bash, well, stick to bash, I don't know why you want to open another shell.

chsh didn't work because there isn't a chsh in Puppy. Really, when a command is not found its because it is not there.

Second:
Here is the best Bash tutorial I've ever seen. It may not be the most complete but after reading it you will feel quite comfortable writing and reading Bash scripts:

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

Third:
Consider that bash scripting is just a way to execute commands in a programmed sequence. So when scripting in bash you will most likelly need to learn those commands. Here is a list of commands that you will need to understand:

grep
sed
awk
cut
tr

Fourth:
Learn regular expressions. Puppy comes with a regular expresions evaluator under the Utilities menu so you can experiment with them.

I hope this helps
[url]http://rarsa.blogspot.com[/url] Covering my eclectic thoughts
[url]http://www.kwlug.org/blog/48[/url] Covering my Linux How-to

User avatar
une
Posts: 56
Joined: Fri 26 Jan 2007, 12:49
Location: Australia

#7 Post by une »

Thanks for your detailed help. My understanding is minimal but I thought that every bash shell was the same, containing many commands and that every bash shell would have. When I type chsh at the command line on my Mandrake 10 Linux desktop system (using bash), the command is recognized. But on my Puppy system the same command is not found within bash. I now assume then that even within the same shell, the commands and functionality varies between systems as they may contain different versions of the same shell. Is this correct?

I don't particluarly want to change shell, but my experiments led to this discovery that does not make sense to me yet.

Is it also the case that sh and bash are different shells (but very similar) and that Puppy only has bash? Thus the symbolic link from sh to bash is present so that scripts that reference sh will work in Puppy's bash shell.

User avatar
Gn2
Posts: 943
Joined: Mon 16 Oct 2006, 05:33
Location: virtual - Veni vidi, nihil est adpulerit

#8 Post by Gn2 »

"sh" Is not a distinct Linux shell variant - & in Puppy - is a symlink to Bash !
Google for different Linux shells available , how they vary.
Bash is the default Linux command parser - it is backwards compatible in most cases - NOT all

As was explained, (by default) inclusion in Puppy has limited ash and Bash CLI parameters.

Temp shelve interest in other shells -concentrate on learning Linux basics & as need dictates, time permits, how to script using Bash.

Puppy forum is for distribution specific variations.

The best way to learn is by backing up (running in RAM mode obviates most full_install_to_hard drive concerns)
Then experiment - asking should be considered - as a last resort when unable to find resources, documentations.

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

#9 Post by MU »

ash is a small inbuilt shell of busybox.
Busybox is used when Puppy boots, it allows to keep the initial ramdisk small.
Busybox is a binary, that includes several commands, that else are only available as seperate programs.
But this small size has a price: not the full set of options of these programs are available.

So when Puppy booted, it additionally offers the bash, that is loaded from the big file pup_2xx.sfs.
Bash compatibility is required by several programs.

Other shells like ksh or csh have specialized capabilities like support for mathematic functions.
You will find those on universitary systems.
We had one program once, that required csh, so you can extract it from this archive:
http://dotpups.de/files/unsorted-not-pa ... sd-csh.tgz

The original limited sh is used no longer, as GN2 said, it usually is just a symlink to one of the others like the Bash (Bourne again shell), that has become quasi standard in Linux, as it is part of the GNU Utilities from the Free Software Foundation.
The FSF has rewritten several Unix-tools under a free license.
What we commonly know as "GNU/Linux" usually is the Kernel (the "real" Linux) plus the GNU Utilities like bash.
Mark

PaulBx1
Posts: 2312
Joined: Sat 17 Jun 2006, 03:11
Location: Wyoming, USA

#10 Post by PaulBx1 »

Mark, thanks for your very clear synopsis here. I have seen the situation with Puppy's shells described here and there before, but usually in an unsatisfactory way that raised even more questions.

User avatar
rarsa
Posts: 3053
Joined: Sun 29 May 2005, 20:30
Location: Kitchener, Ontario, Canada
Contact:

#11 Post by rarsa »

une wrote:I thought that every bash shell was the same, containing many commands and that every bash shell would have.
There is only one bash, there is no such thing as "every bash".

Now I understand your confusion a little bit better:

First
There are different shells, cshell, kornshell, bourne shell, bash. This link may clarify it
http://www.freeos.com/guides/lsst/ch01sec07.html

Second
Not every command that you execute in Bash is a bash command, some are external comands. For example, if you open a bash window and execute "geany" it will open, that does not mean that geany is a bash command, it means that bash executed that command.

chsh is an external command that changes shells,
http://www.die.net/doc/linux/man/man1/chsh.1.html

As puppy only comes with one shell, there is no need for that command.
I now assume then that even within the same shell, the commands and functionality varies between systems as they may contain different versions of the same shell. Is this correct?
No, it is not correct.

As an example, every car has the same functionality, but if I put fuzzydice in one car, does not mean that all cars will have them, does it?
Is it also the case that sh and bash are different shells
No, sh is not "a shell", it is a link to the default shell. So in puppy it is pointing to bash, in other systems it may be pointing to some other shell.

I hope this clarifies the issue.
[url]http://rarsa.blogspot.com[/url] Covering my eclectic thoughts
[url]http://www.kwlug.org/blog/48[/url] Covering my Linux How-to

User avatar
une
Posts: 56
Joined: Fri 26 Jan 2007, 12:49
Location: Australia

#12 Post by une »

Thanks for such detailed responses. You have cleared up a lot of issues for me. It is much appreciated.

User avatar
Gn2
Posts: 943
Joined: Mon 16 Oct 2006, 05:33
Location: virtual - Veni vidi, nihil est adpulerit

#13 Post by Gn2 »

Just a note RE the supplied link Alternate shells:
Please be aware command {chsh} is used to alter default O/System LOGIN shell

That may not be advisable > ESP in Puppy

User avatar
une
Posts: 56
Joined: Fri 26 Jan 2007, 12:49
Location: Australia

#14 Post by une »

So it seems that the command chsh executes an application (if that application has been installed on the system) that changes the shell. This command does not execute a function built into the shell itself, unlike commands such as rm and grep. Do I have this much correct?
Sorry for not being distro specific, but I figure I may as well finish this thing now.

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#15 Post by Flash »

No, don't stop now. This is a subject which has needed clarification for some time. I'm adding this thread to the Beginners sticky index, under Educational discussions. Thanks to everyone who contributed helpful explanations. :)

User avatar
rarsa
Posts: 3053
Joined: Sun 29 May 2005, 20:30
Location: Kitchener, Ontario, Canada
Contact:

#16 Post by rarsa »

une wrote:This command does not execute a function built into the shell itself, unlike commands such as rm and grep. Do I have this much correct?.
The concept is correct: there are built in commands that are part of bash. There are other commands that are sand alone programs.

FYI...rm and grep are external commands, not a shell commands.

Here is the list of built in commands

http://linux.about.com/library/cmd/blcmdl1_builtin.htm

Whenever you are in doubt you can execute "which" with a valid command

Code: Select all

which <command>
e.g.
which grep
If it returns you the file path, then it is an external program. If it complains, then it is a shell command.

Note that I said "valid command", if you use "which" with an invalid command or a program that is not in your path, then it will complain too.
[url]http://rarsa.blogspot.com[/url] Covering my eclectic thoughts
[url]http://www.kwlug.org/blog/48[/url] Covering my Linux How-to

User avatar
Gn2
Posts: 943
Joined: Mon 16 Oct 2006, 05:33
Location: virtual - Veni vidi, nihil est adpulerit

#17 Post by Gn2 »

Bash is a very powerful command shell - a "shell" because it may stand alone - without need for most GUI functions.

As in nature a "shell" may contain most anything inside it.
Bash has many desirable features,>
Auto command line completion_
"Wildcard" letter substitution
"Built-in" Internal & external functions
A "prompt" that is user configurable & may enfold many functions
"Reserved" character functions - (Bash internal use only)
Built as an execute only - or await user input {interactively}
The login shell (in the INITIAL = initrd boot image) usually ASH is an example of "interactive" use
Once control is passed to the kernel. file system mounted, which CLI used has been determined by the distribution

Part of the confusion arises in ability to make a script executable.

An executable may include - functions as desired to call other internal/external commands,
take those results and perform other actions.
The script may also embed other languages/functions that are invoked If/when the dependencies
for them are available as dynamic libraries -already installed on System.

What you now have is a executable - coded partially in binary and partially in text.

If some binary contains their dependencies in the code - it is a STATIC binary
= Self contained, the dependencies are included

Apps built requiring dynamic libraries - are compiled with specific dependencies.
If those dependencies are not met (not available), versions don't match -
or the command executable path environment is incorrect-

The output is intercepted by the error handling procedures of the kernel -
In most cases, an error Msg is output to screen (I.E. segment fault) and process halted to limit further system damage.
In extreme error cases, a kernel "dump" is output (contents of memory locations of events)
In others - the system may "lock" (kernel panic) > a kernel freeze - unable to continue ~ a power reset is Req'd.

Please read Bash manuals and for Linux basics, see such as the YoLinux (URL)
It will be a big aid to purchase such as the O'Reilly manual "Linux in a Nutshell"
It is much the same as the built-in system help (man or info pages), but w/explanations.
Their prime use however is as an "cheat-sheet" to recall parameter syntax.

They are not suitable to LEARN Bash... any more than a dictionary may teach a new language.
One of best Bash manuals is downloadable, BASH - on-line

Any data in this forum thread was available on line.

User avatar
une
Posts: 56
Joined: Fri 26 Jan 2007, 12:49
Location: Australia

#18 Post by une »

Thanks again. I think I need to do some generic Linux research.
I am using a book called;
"Linux & Unix Programming Tools" by Sarwar
to get started on my Linux programming quest. It is actually part of a computer science course I am taking via the Internet.

I am starting to work my way into the book and am finding other commands that my Puppy Linux system does not recognize. The latest command I tried to use that Puppy Linux did not recognize was whereis.

My Linux system at home (Mandrake 10) recognizes commands such as chsh and whereis. It seems there is a whole other set of commands that one commonly uses from the Linux command line that are not a built in within BASH or located elsewhere in Puppy Linux.

My apologies Gn2 but I did not understand all of what you detailed in your last post, as I said I will do more research elsewehere.
However, is there a package I can download and install (then include in a remastered CD for constant use) that will make available from the command line the entire range of Linux commands that are commonly used?
I looked at the the built in commands listing suggested in this thread and found some examples that have confused me a little. Stick with me here if you have time, following are my experiments;

In this thread it has been mentioned that grep is not a built in BASH command, thus as expected to me when I execute the following I get a response as follows from the system;
$which grep
/bin/grep

This indicates to me that the command grep is not built in within BASH and the application that is grep is located in /bin.

break is a built in command within BASH apparently, so when I execute
$which break
$

I get no response at all, just another prompt as shown above. Does this lack of system response indicate that break is a BASH built in command?

printf is also an built in command within BASH it seems, however when I execute the following I get a response as follows;
$which printf
/usr/bin/printf

This confuses me as this indicates to me that to execute the printf command the system uses an external application stored in /usr/bin.

whereis is not available at all, however when I execute the following I get a response as follows;
$which whereis
$

This confuses me as this lack of system response is the same as for break, which I deduced meant it was a BASH built in command.

There are some other "non BASH built in" commands available in my Puppy Linux system such as date
$which date
/bin/date


and time

$which time
/usr/bin/time


which are included in my Puppy Linux.

Thus I am confused as to why what I think are supposed to be BASH built in commands, seem to execute from external (to BASH) locations (eg printf)
I am also confused why some external commands such as grep are included in Puppy Linux and why others are not (eg whereis)

Perhaps there may be other commands I will be asked to use when working through my textbook that are not either built in within BASH nor available from elsewhere in the Puppy Linux OS.

I want to be able to work through this textbook using Puppy Linux.

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#19 Post by GuestToo »

My Linux system at home (Mandrake 10) recognizes commands such as chsh and whereis
the free version of Mandriva is 4,438MB (commercial version is larger) ... Puppy is less than 80MB
is there a package I can download
i'm not sure, but i think there is a dotpup package of the GNU utilities somewhere
This indicates to me that the command grep is not built in within BASH and the application that is grep is located in /bin
which grep shows that there is an executable file named grep in one of the directories in the PATH (type echo $PATH) ... whether grep is a bash builtin function, or not ... if there is no executable file named grep in a dir in PATH, it will not display a result ... whether grep is a bash builtin function, or not

which does not tell you about bash builtin functions
Does this lack of system response indicate that break is a BASH built in command?
no, it tells you that there is no executable file named break in the PATH dirs
This confuses me as this indicates to me that to execute the printf command the system uses an external application stored in /usr/bin
which tells you that there is an executable file named printf in /usr/bin ... this does not tell you whether there is a bash builtin function named printf

if you type ash, you will run Busybox's ash shell ... if you then type help, it will show you a list of builtin functions in the ash shell ... printf is not an ash builtin function, so ash will run an external executable file if you run the printf command

if you type help in a bash shell, you will see that printf is a bash builtin function ... if you run the printf command in a bash/sh shell, you will be running the internal bash function, not the external executable file
This confuses me as this lack of system response is the same as for break, which I deduced meant it was a BASH built in command
this means there is no executable file named whereis in PATH

there is a similar and useful bash script called where ... dotpup package here: http://www.murga-linux.com/puppy/viewtopic.php?&t=10469
I am also confused why some external commands such as grep are included in Puppy Linux and why others are not (eg whereis)
grep is extremely useful (essential) ... whereis is not essential at all (obviously, since Puppy runs without it)

if you delete /bin/grep, you will find that Puppy will be severely broken ... many programs will no longer work ... Puppy may no longer be able to finish booting
I want to be able to work through this textbook using Puppy Linux
Puppy is 80 megs, not 4 gigs ... Puppy is different, it is designed to be small and fast and to run completely in ram ... it is not possible to small and fast and to run completely in ram and to have installed by default every program and utility and library that a distro like Mandriva has installed

User avatar
une
Posts: 56
Joined: Fri 26 Jan 2007, 12:49
Location: Australia

#20 Post by une »

Thanks a lot for your clear answer. I will contemplate further.

Post Reply