Bourne shell source file


















Bourne shell script need help please? RedHat Commands. OpenSolaris Commands. Linux Commands. SunOS Commands. FreeBSD Commands. Full Man Repository. Advanced Search. Contact Us. Forum Rules.

Mark Forums Read. Thread Tools. View Public Profile for dordrix. Find all posts by dordrix. View Public Profile for zaxxon. Do I have a wrong version of sh? Improve this question. Milad Milad 3, 5 5 gold badges 27 27 silver badges 38 38 bronze badges. One could reasonably argue that a shell which supports source is a "wrong version".

Also, the error message source: not found means that the source command was evaluated properly, but the file it should have read does not exist.

It's not possible to "install" source because it is a feature of the shell. It cannot be implemented as an external command. Add a comment. Active Oldest Votes. Improve this answer. I'm curious why though, does it mean on Ubuntu bash is sh?

Milad I think it does not work on ubuntu What is your OS version? Thank you. It wasn't immediately intuitive to me how to fix this issue unfortunately but it set me on the right path. In Bourne shell sh , use the. Hawkeye Parker 6, 7 7 gold badges 38 38 silver badges 44 44 bronze badges.

Guru Guru I am not really allowed to change the script, it runs fine on a redhat server. But on that server, source seems to be included in sh — Milad. Summary [ edit ] Description Bourne Shell Scripting. Book is in the English language. You cannot overwrite this file.

The following other wikis use this file: Usage on en. Software used cairo 1. Structured data Items portrayed in this file depicts. Bash reads input from the terminal when interactive, and from the script file specified as an argument otherwise. When interactive, bash allows the user to edit command lines as they are typed in, using familiar key sequences and editing commands similar to the Unix emacs and vi editors. Bash uses the readline library to implement command line editing.

This provides a set of functions allowing users to edit command lines, functions to save command lines as they are entered, to recall previous commands, and to perform csh-like history expansion. Bash is readline's primary client, and they are developed together, but there is no bash-specific code in readline.

Many other projects have adopted readline to provide a terminal-based line editing interface. Readline also allows users to bind key sequences of unlimited length to any of a large number of readline commands. Readline has commands to move the cursor around the line, insert and remove text, retrieve previous lines, and complete partially-typed words. On top of this, users may define macros, which are strings of characters that are inserted into the line in response to a key sequence, using the same syntax as key bindings.

Macros afford readline users a simple string substitution and shorthand facility. It reads characters from the keyboard using read or equivalent, or obtains input from a macro. Each character is used as an index into a keymap, or dispatch table. Though indexed by a single eight-bit character, the contents of each element of the keymap can be several things. The characters can resolve to additional keymaps, which is how multiple-character key sequences are possible. Resolving to a readline command, such as beginning-of-line , causes that command to be executed.

A character bound to the self-insert command is stored into the editing buffer. It's also possible to bind a key sequence to a command while simultaneously binding subsequences to different commands a relatively recently-added feature ; there is a special index into a keymap to indicate that this is done.

Binding a key sequence to a macro provides a great deal of flexibility, from inserting arbitrary strings into a command line to creating keyboard shortcuts for complex editing sequences. Readline stores each character bound to self-insert in the editing buffer, which when displayed may occupy one or more lines on the screen.

Readline manages only character buffers and strings using C char s, and builds multibyte characters out of them if necessary. When in a locale that supports multibyte characters, readline automatically reads an entire multibyte character and inserts it into the editing buffer.

It's possible to bind multibyte characters to editing commands, but one has to bind such a character as a key sequence; this is possible, but difficult and usually not wanted. The existing emacs and vi command sets do not use multibyte characters, for instance. Once a key sequence finally resolves to an editing command, readline updates the terminal display to reflect the results.

This happens regardless of whether the command results in characters being inserted into the buffer, the editing position being moved, or the line being partially or completely replaced.

Some bindable editing commands, such as those that modify the history file, do not cause any change to the contents of the editing buffer. Updating the terminal display, while seemingly simple, is quite involved.

Readline has to keep track of three things: the current contents of the buffer of characters displayed on the screen, the updated contents of that display buffer, and the actual characters displayed. In the presence of multibyte characters, the characters displayed do not exactly match the buffer, and the redisplay engine must take that into account. When redisplaying, readline must compare the current display buffer's contents with the updated buffer, figure out the differences, and decide how to most efficiently modify the display to reflect the updated buffer.

This problem has been the subject of considerable research through the years the string-to-string correction problem. Readline's approach is to identify the beginning and end of the portion of the buffer that differs, compute the cost of updating just that portion, including moving the cursor backward and forward e. The redisplay engine is without question the one piece of readline that has been modified most heavily. Most of the changes have been to add functionality—most significantly, the ability to have non-displaying characters in the prompt to change colors, for instance and to cope with characters that take up more than a single byte.

Readline returns the contents of the editing buffer to the calling application, which is then responsible for saving the possibly-modified results in the history list. Just as readline offers users a variety of ways to customize and extend readline's default behavior, it provides a number of mechanisms for applications to extend its default feature set. First, bindable readline functions accept a standard set of arguments and return a specified set of results, making it easy for applications to extend readline with application-specific functions.

Bash, for instance, adds more than thirty bindable commands, from bash-specific word completions to interfaces to shell built-in commands. The second way readline allows applications to modify its behavior is through the pervasive use of pointers to hook functions with well-known names and calling interfaces.

Applications can replace some portions of readline's internals, interpose functionality in front of readline, and perform application-specific transformations. When the shell is not using readline, it uses either stdio or its own buffered input routines to obtain input. The bash buffered input package is preferable to stdio when the shell is not interactive because of the somewhat peculiar restrictions Posix imposes on input consumption: the shell must consume only the input necessary to parse a command and leave the rest for executed programs.

This is particularly important when the shell is reading a script from the standard input. The shell is allowed to buffer input as much as it wants, as long as it is able to roll the file offset back to just after the last character the parser consumes.

As a practical matter, this means that the shell must read scripts a character at a time when reading from non-seekable devices such as pipes, but may buffer as many characters as it likes when reading from files. These idiosyncrasies aside, the output of the non-interactive input portion of shell processing is the same as readline: a buffer of characters terminated by a newline.

Multibyte character processing was added to the shell a long time after its initial implementation, and it was done in a way designed to minimize its impact on the existing code. When in a locale that supports multibyte characters, the shell stores its input in a buffer of bytes C char s , but treats these bytes as potentially multibyte characters.

Readline understands how to display multibyte characters the key is knowing how many screen positions a multibyte character occupies, and how many bytes to consume from a buffer when displaying a character on the screen , how to move forward and backward in the line a character at a time, as opposed to a byte at a time, and so on.

Other than that, multibyte characters don't have much effect on shell input processing. Other parts of the shell, described later, need to be aware of multibyte characters and take them into account when processing their input.

The initial job of the parsing engine is lexical analysis: to separate the stream of characters into words and apply meaning to the result. The word is the basic unit on which the parser operates. Words are sequences of characters separated by metacharacters, which include simple separators like spaces and tabs, or characters that are special to the shell language, like semicolons and ampersands.

One historical problem with the shell, as Tom Duff said in his paper about rc , the Plan 9 shell, is that nobody really knows what the Bourne shell grammar is. The Posix shell committee deserves significant credit for finally publishing a definitive grammar for a Unix shell, albeit one that has plenty of context dependencies.

That grammar isn't without its problems—it disallows some constructs that historical Bourne shell parsers have accepted without error—but it's the best we have. The bash parser is derived from an early version of the Posix grammar, and is, as far as I know, the only Bourne-style shell parser implemented using Yacc or Bison.

This has presented its own set of difficulties—the shell grammar isn't really well-suited to yacc-style parsing and requires some complicated lexical analysis and a lot of cooperation between the parser and the lexical analyzer. In any event, the lexical analyzer takes lines of input from readline or another source, breaks them into tokens at metacharacters, identifies the tokens based on context, and passes them on to the parser to be assembled into statements and commands.

There is a lot of context involved—for instance, the word for can be a reserved word, an identifier, part of an assignment statement, or other word, and the following is a perfectly valid command:. At this point, a short digression about aliasing is in order. Bash allows the first word of a simple command to be replaced with arbitrary text using aliases.

Since they're completely lexical, aliases can even be used or abused to change the shell grammar: it's possible to write an alias that implements a compound command that bash doesn't provide. The bash parser implements aliasing completely in the lexical phase, though the parser has to inform the analyzer when alias expansion is permitted.

There are three types of quoting, each of which is slightly different and permits slightly different interpretations of the quoted text: the backslash, which escapes the next character; single quotes, which prevent interpretation of all enclosed characters; and double quotes, which prevent some interpretation but allow certain word expansions and treats backslashes differently.

The lexical analyzer interprets quoted characters and strings and prevents them from being recognized by the parser as reserved words or metacharacters. The former is widely used; the latter, perhaps because there are few good examples or use cases, less so.

The rest of the interface between the parser and lexical analyzer is straightforward. The parser encodes a certain amount of state and shares it with the analyzer to allow the sort of context-dependent analysis the grammar requires. For example, the lexical analyzer categorizes words according to the token type: reserved word in the appropriate context , word, assignment statement, and so on.

In order to do this, the parser has to tell it something about how far it has progressed parsing a command, whether it is processing a multiline string sometimes called a "here-document" , whether it's in a case statement or a conditional command, or whether it is processing an extended shell pattern or compound assignment statement. This function has to know about here documents, shell comments, metacharacters and word boundaries, quoting, and when reserved words are acceptable so it knows when it's in a case statement ; it took a while to get that right.

When expanding a command substitution during word expansion, bash uses the parser to find the correct end of the construct. This is similar to turning a string into a command for eval , but in this case the command isn't terminated by the end of the string. In order to make this work, the parser must recognize a right parenthesis as a valid command terminator, which leads to special cases in a number of grammar productions and requires the lexical analyzer to flag a right parenthesis in the appropriate context as denoting EOF.

The parser also has to save and restore parser state before recursively invoking yyparse , since a command substitution can be parsed and executed as part of expanding a prompt string in the middle of reading a command. Since the input functions implement read-ahead, this function must finally take care of rewinding the bash input pointer to the right spot, whether bash is reading input from a string, a file, or the terminal using readline.

This is important not only so that input is not lost, but so the command substitution expansion functions construct the correct string for execution. Similar problems are posed by programmable word completion, which allows arbitrary commands to be executed while parsing another command, and solved by saving and restoring parser state around invocations.

Quoting is also a source of incompatibility and debate. Twenty years after the publication of the first Posix shell standard, members of the standards working group are still debating the proper behavior of obscure quoting.



0コメント

  • 1000 / 1000