Every programmer knows how time consuming it can be to type complex commands. One way you can save time is through scripting, which you can learn more about when you take this Linux course. There are many commands you can use in Unix for shell scripting, which you can also learn along with the rest of the information that this Unix article talks about. The more tools you are familiar with, the better you will become at creating script.. Many of the built-in Unix commands have ‘man’ pages, which contain instructions for use.
To access the main Unix page, type:
Unix Shell Scripting
To accept a command that a user types in, Unix uses shells. There are different available shells you can choose from with the most common being KSH (Korn Shell), CSH (C Shell) and SH (Bounce Shell). Other shells you might come across will share the same syntax as they are variants of the ones mentioned here.
There are built-in functions that various shells have. This allows the creation of shell scripting, which is stringing shell commands together get them to become automated, making life easier for users.
Creating Basic Scripts
Command Line Arguments in Unix
In Unix, command line arguments can be altered with the command ‘shift,’ which you can learn more about in this Unix course. Here is an enumeration of the command line arguments:
Here are some special built-in variables of the command line arguments:
$$ expands the shell’s process id innovated for running the script. This is useful for creating temporary, unique file names that are relative to the script instantiation.
$- expands to the options/flags used for invoking the flags. This is useful for program flow control based on the set flags.
$@ is used for expanding all the parameters that spaces separate. This can be used for passing every parameter to some other program or function.
$# represents the count of the parameter. This can be used for controlling constructs of loops that need to process every one of the parameters.
All variables turn into shell variables when scripting starts. You can instantiate new variables like the way it is shown here with no space on either side of the sign =. The name can only be made up of underscores, numeric characters and alphabetic characters. A numeric character cannot begin a name. You also need to avoid using keywords like ‘for’ as these are reserved words for the compiler:
This echoes ‘Hello There!’ to the display on the console. If you want to assign strings to variables and there are spaces in the string, you will need to enclose the string in double quotation marks. This tells the shell to literally take the contents as it is. To include variables, you can also use a $ sign within the quotation marks:
This echoes ‘one two three’ to the screen. Within a double quoted section, the escape character \ can also be used to output a special character. This immediately outputs the character that follows it so that \\ would output \.
When a newline follows an escape character, the newline character is ignored by the shell, allowing long commands to be spread. You can also use the escape character anywhere else, except within single quotation marks.
When you put single quotation marks in anything, this causes it to be treated as literal text that will be passed on. For sending a sequence of commands to other files, this is a useful tool, since you can create new scripts due to the fact that the string inside the quotes remains untouched.
Pipelines and Command Redirection
A pipeline is a form of redirection used for chaining a command in such a way that the construction of composite, powerful commands can be created. The | symbol for a pipe takes the ‘stdout’ from the command before it as it redirects to the commands after it:
In this example, first a long -1 directory listing is requested from the current directory using the command ‘Is. From this, the output is then piped to grep, which gets all the listings filtered out containing this search word. Finally, this gets piped through to ‘sort’ (-r) which gets the output sorted in reverse. The output is then passed on to stdout:
A command accepts standard input by default, which is abbreviated as ‘stdin.’ To put it another way, the command line in the form of arguments passed to the command is called standard input. A normal command by default points its output to standard output, abbreviated as ‘stout.’ Usually, the console display is standard output. This might be the action desired for some commands. At other times, however, users may want to direct input other than stdout and acquire input from somewhere other than stdin. You can do this using redirection.
Use 2> for standard error (stderr) redirection to files. For example, when you want to redirect the standard error to a file from command A, type:
For appending stdout to files, use >>, like when you want to get a file’s end date appended, redirect the output from date:
Use the < to indicate what you want the command to before the symbol for redirection to get input from the specified source right after the symbol. For example, when you want to redirect input to grep so it becomes a file, type:
To stdout to a file, use the >, like when you want to get a directory listing redirected by the Is, here is what you can do:
Exits in Unix Commands
An exit status is the term used when a Unix command exits. To programmers, an exit status indicates whether the command was successful or not in performing its supposed task. Usually, when a command does what it is supposed to do flawlessly, it terminates with a ‘zero’ exit status. When other values are used in the exit status, this is indicative of possible errors that may have occurred, which you can learn more about in this Unix course that talks about command lines, or in this Linux course.