The Original ash Shell - Shell Scripting

Before looking at the newer, more advanced ash and dash shells, it’s a good idea to understand what the original ash shell looked like. Even though you may not run into it in desktop or server Linux distributions, it still lives on today in some embedded Linux distributions, so it’s good to know the limitations of the shell.

The Original ash command line parameters

The original ash shell uses command line parameters to control the behavior of the shell. Table below lists the command line parameters available in the original ash shell.

The Original ash command line parameters

You can specify the command line parameters in any order, either individually, or together after a single dash.

The -e command line option allows you to force the ash shell to exit immediately if a command produces an error. This is a handy feature when trying to troubleshoot your shell scripts:

$ cat test1
#!/bin/ash -e
# exiting on an error
cp /home/badfile test
echo "Does this display?"
$ ./test1
cp: /home/badfile: No such file or directory
$

The cp command attempts to copy a file that doesn’t exist, so it generates an error. When the ash shell detects the error, it immediately exits, causing the script to stop.

The -f command line option allows you to disable wildcard expansion in the shell. The shell treats the standard wildcard characters as normal characters:

$ ash -f
$ ls -al *.txt
ls: *.txt: No such file or directory
$

In this example, the shell is looking for a file called *.txt, instead of matching any file that ends with .txt.

The -n command line option is not used in interactive shells, but if you use it in your shell script , the ash shell will scan the script, but not actually run it. Here’s a quick example:

$ cat test2
#!/bin/ash -n
# test run a script
echo "This is a test"
echo "This is a bad line.
$ ./test2
./test2: 5:Syntax error: Unterminated quoted string
$

The shell doesn’t execute the good echo statement, since the bad echo statement contained an error. However, the shell does indicate that there’s an error in the bad echo statement line, so at least you know where to look for the problem (the error message even tells you the line number). This is a great way to quickly troubleshoot coding problems in your ash shell scripts without having to actually run through the entire script line by line.

The original ash built-in commands

Since the original ash shell was designed to be lightweight, it contains just a subset of the Bourne shell built-in commands. The trick is knowing which commands you can use and which ones you can’t. Table below shows the built-in commands available in the original ash shell.

As you can see from Table below, the ash shell contains some of the bash commands that you’re used to, but there are quite a few things missing. The original ash shell doesn’t provide many bells and whistles in its command environment. It just provides a bare-bones environment for running programs. Of course, this feature is what makes it so popular in low-memory operations, such as embedded Linux devices. Table The Original ash Shell Built-In Commands

Let’s take a look at a few of the basic ash shell built-in commands that we haven’t already explored in the bash shell.

The bltin command

You can use the bltin command when you want to run a built-in command in situations when there’s another command available with the same name. When you’re running shell scripts, if there’s more than one script with the same name, you can just specify the full pathname to select the proper script. With a built-in command, you can’t do that.

To solve this problem, the ash shell provides the bltin command (its name is changed to builtin in the bash shell). This forces the ash shell to look for a built-in command using the specified name instead of a shell script.

The Original ash built in command

The hash command

The ash shell maintains a table of all commands you enter while in the shell process. The ash shell then uses this table (called the hash table) to quickly retrieve the location of recently used commands, instead of having to traverse all of the directories in the PATH environment variable looking for them.

You can display the current hash table just by using the hash command by itself:

$ hash
builtin hash
builtin pwd
/bin/ls
$

The hash table contains the commands starting with the most recently used command. As you can see from the example, it also remembers the built-in shell commands that you execute.If you use the cd command to change your directory, the shell places an asterisk next to any commands in the hash table that use a pathname:

$ cd
$ hash
builtin hash
builtin pwd
builtin cd
/bin/ls*
$

The asterisk means that the pathname may no longer be valid due to the change of directory since the last time you executed the command.You can use the -r parameter to remove individual items from the hash table:

$ hash -r /bin/ls
$ hash
builtin hash
builtin pwd
builtin cd
$

Sometimes you’ll run into the problem of a command that’s been moved, but the shell doesn’t realize it and attempts to run the command from the location specified in the hash table. By removing the hash table entry for the command you force the shell to re-find the command.

The -v parameter tells the hash command to display the full location where it finds a command that you specify:

$ hash -v cd
builtin cd
$ hash -v ls
/bin/ls
$ hash -v fortune
/usr/games/fortune
$

This example also demonstrates that, if the command is a built-in shell command, the hash command will identify it as such.

The lc command

The lc command is a cute little utility that can save you some typing on the command line. It stands for last command, and its job is to execute the last command that you ran on the command line, including any command line arguments. Here’s a few examples of using the lc command:

$ pwd
/home/rich/test
$ lc
/home/rich/test
$ ./test1
Your HOME directory exists
$ lc
Your HOME directory exists
$ ls -al
total 148
drwxr-xr-x 2 rich rich 4096 Jan 7 08:15 .
drwxr-xr-x 17 rich rich 4096 Jan 3 10:03 ..
-rwxr--r-- 1 rich rich 97 Jan 3 10:04 test1
-rwxr--r-- 1 rich rich 84 Jan 7 08:01 test2
$ lc
total 148
drwxr-xr-x 2 rich rich 4096 Jan 7 08:15 .
drwxr-xr-x 17 rich rich 4096 Jan 3 10:03 ..
-rwxr--r-- 1 rich rich 97 Jan 3 10:04 test1
-rwxr--r-- 1 rich rich 84 Jan 7 08:01 test2
$

The lc command works no matter what the last command was, whether it was a built-in shell command, an application program, or a shell script. As you can see from the example, the lc command also duplicates any command line arguments you supplied with the original command.

The ash shell files

Just like the bash shell, the ash shell uses default files to control its start environment. The default files allow you to define shell environment variables and settings when using the ash shell in an interactive mode.

There are three files that the ash shell uses:

  • The /etc/profile file
  • The $HOME/.profile file
  • A file defined in the ENV environment variable

If the ash shell detects that it’s used as a login shell, it first executes the contents of the /etc/ profile file. This is the place to define system-wide environment variables that are set for all interactive users.

Next, the ash shell searches the contents of the user’s HOME directory for a file named .profile. If it exists, the ash shell executes it. Finally, the ash shell checks if the ENV environment variable has been set (either before the ash shell is run or as part of the login script). If this variable is set, the ash shell attempts to execute the contents of the filename specified by the variable.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Shell Scripting Topics