• Welcome to Jose's Read Only Forum 2023.
 

Scripting Under Linux

Started by Donald Darden, April 29, 2008, 11:27:44 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Donald Darden

DOS users recognize batch files, with the extension .BAT, as being a type of script file.
Windows programmers may have encountered script languages and scripts as part of certain applications.  Both DOS and Windows depend upon file associations based on the last group of characters after the last period in a file name, to identify the type of file and the program that it is associated with.  In Windows, many file associations are linked to specific applications by their inclusion in the Registry.  The idea is that you identify a file, and its extension identifies the application required, and the application is started automatically, which in turn processes the file for you.

DOS and Windows depend on file extensions to know what to do with a file.  But in Linux, file extensions are optional.  So how does Linux know which application or scripting process to associate a file with, or what to do with it?

Well, DOS, Windows, and Linux are all capable of looking internally at the content of a file, and often there are clues that help identify what the file type is.  Script files are after all, text files, not binary files.  Binary files can (and usually do) have any character value in them, but text files are limited to a subset of those characters.
If you find any excluded characters in a file, then it must be a binary file.  If it only contains valid text characters, then it is a text file.

But more than that, certain types of files can be idenfitied by their structure, which is expressed as a unique arrangement of characters at the beginning of the file.  DOS only recognizes three types of executable files based on their extensions, which are .BAT, .COM, and .EXE files.  Each is internally different.  The .BAT file is a text file (or script file if you prefer), and the .COM and .EXE files are binary files with computer instructions, data, and storage areas defined in them.  The differences between .COM and .EXE are significant, however.  And to make sure that the .EXE file is correctly recognized, the letters "MZ" are the first two bytes found in every executable (.EXE) file.  The .COM file (short for command), will not have these tweo letters in those first two bytes.

That tells you something about how the internal structure and content of files can be used to identify what types of files they are.  So without a requirement for certain types of file extensions in Linux, the judgment of what to do with a file depends upon its internal structure or how it is envoked.

Essentially, files are either binary or text in nature.  If you assume that binary files are executed as found, and text files are script files that need to be interpreted by the default command processor, then you have a working arrangement.  But to go further, the command processor, what Linux refers to as a "Shell", may be able to recognize the internal structure of either binary or text files to break their types down even further.  Thus, different command processors (or shells) may be set up to handle specific types of files in certain ways.

In DOS you have only one built-in command processor, which is invoked by COMMAND or CMD.  Windows itself uses a different command processor that creates the Windows environment, but still allows you access to the DOS command processor when you so desire.  Start/Run allows you to type CMD, which opens what a Linux user would call a "Terminal Console" Window and gives you access to what some call the " C Prompt".  This is the command processor access point, what others call the command line mode, since you end up typing a line at a time here.

On the other hand, Linux gives you a choice of multiple Shells and GUIs (Graphics User Interfaces) to choose from.  Ubuntu uses the Gnome GUI by default, and new user accounts are set up generally to use the bash (Borne Again Shell) when in non-graphics mode, such as working though a Terminal Console Window.

You can use a Terminal Console Window on your Linux system and enter cat /etc/shells to get a list of the available shells on your system.  There are only three commonly used, which are bash, csh (c shell), and ksh (korn shell), but at least 90 percent of users stick with bash.  The c shell is notable because it is very similar to the C language, and may be preferred by programmers.  The Korn shell went a step farther, by retaining the feature set of bash with enhancements from csh so that you have a blend of the two.

There are other shells available as well, but either less popular or more difficult to get information on how best to use them.

Every user has a default shell that will kick in when you enter a command via the Terminal Console Window.  You can change from one shell to another as you choose, and you can temporarily invoke a different shell when it is needed

Now there is a trick to executing a program or script in Linux that is not needed in the DOS world, and this is going to bother some people coming from either DOS or Windows to Linux.  In that other world, when you type in a command or file name for an executable file, the first place where DOS or Windows will look for it is in the current folder.  But Linux does not follow that rule, and in fact it might not be able to find the file if the current folder is not included in its search path, even though it is right there under your nose.  The "fix" is a simple one:  Just enter ./ right in front of the file name, and it will only search the current folder for it.  This is because the use of period and slash together specify the current folder.  A period also signifies the current folder, but put in front of a file name would read as though it were part of the file name.  It is perfectly legal to have periods to start the names of folders and files, and by convention, these will be "hidden" from casual use of the ls (list) or dir command.  You can still see hidden folders and files if you use ls -a.

Now this works for executing a file in the current folder.  But suppose it is a script file, and expected to operate under a different shell?  You can switch to a different shell for the duration of this session by simply typing in the shell name, such as ksh.   Bash will recognize the name, but if it is not yet installed, it will advise you how to get  it.  If it is installed, you will be switched to it.  To get back, you just have to enter bash by itself on a later line.

To invoke a shell for a single command or file, you enter the shell name, followed by
the command or file name on the same line.  It will revert to the same shell you were using before the line was executed.

Son not so hard.  Now there are many books on writing bash scripts or using bash commands, and a lot of very helpful scripting examples posted on the internet.  This is a good subject to read up on.  Another topic of interest might be using start up scripts to preconfigure commands or add some capabilities that you want when your system or session begins.  These can be very helpful as well.