UNIX Shell Scripting
Shell Scripting Basics
Command Redirection and Pipelines
By default a normal command accepts input from standard input, which we abbreviate to stdin, standard input is the command line in the form of arguments passed to the command. By default a normal command directs its output to standard output, which we abbreviate to stdout, standard output is usually the console display. For some commands this may be the desired action but other times we may wish to get our input for a command from somewhere other than stdin and direct our output to somewhere other than stdout. This is done by redirection:
• We use > to redirect stdout to a file, for instance, if we wanted to redirect a directory listing generated by the ls we could dothe following:
ls > file
• We use < to specify that we want the command immediately before the redirection symbol to get its input from the source specified immediately after the symbol, for instance, we could redirect the input to grep(which searches for strings within files) so that it comes from a file like this:
grep searchterm < file
• We use >> to append stdout to a file, for instance, if we wanted to append the date to the end of a file we could redirect the output from date like so:
date >> file
• One can redirect standard error (stderr) to a file by using 2>, if we wanted to redirect the standard error from commandA to a file we would use:
commmandA 2>
Pipelines are another form of redirection that are used to chain commands so that powerful composite commands can be constructed, the pipe symbol '|' takes the stdout from the command preceding it and redirects it to the command following it:
ls -l | grep searchword | sort -r
The example above firsts requests a long (-l directory listing of the current directory using the ls command, the output from this is then piped to grep which filters out all the listings containing the searchword and then finally pipes this through to sort which then sorts the output in reverse (-r, sort then passes the output on normally to stdout.
Variables
When a script starts all environment variables are turned into shell variables. New variables can be instantiated like this:
name=value
You must do it exactly like that, with no spaces either side of the equals sign, the name must only be made up of alphabetic characters, numeric characters and underscores, it cannot begin with a numeric character.
Variables are referenced like this: $name, here is an example:
#!/bin/sh
msg1=Hello
msg2=There!
echo $msg1 $msg2
This would echo "Hello There!" to the console display, if you want to assign a string to a variable and the string contains spaces you should enclose the string in double quotes ("), the double quotes tell the shell to take the contents literally and ignore keywords, however, a few keywords are still processed. You can still use $ within a (") quoted string to include variables:
#!/bin/sh
msg1="one"
msg2="$msg1 two"
msg3="$msg2 three"
echo $msg3
Would echo "one two three" to the screen. The escape character can also be used within a double quoted section to output special characters, the escape character is "\", it outputs the character immediately following it literally so \\ would output \. A special case is when the escape character is followed by a newline, the shell ignores the newline character which allows the spreading of long commands that must be executed on a single line in reality over multiple lines within the script. The escape character can be used anywhere else too. Except within single quotes. Surrounding anything within single quotes causes it to be treated as literal text that is it will be passed on exactly as intended, this can be useful for sending command sequences to other files in order to create new scripts because the text between the single quotes will remain untouched. For example:
#!/bin/sh
echo 'msg="Hello World!"' > hello
echo 'echo $msg' >> hello
chmod 700 hello
./hello
This would cause "msg="Hello World!" to be echoed and redirected to the file hello, "echo $msg" would then be echoed and redirected to the file hello but this time appended to the end. The chmod line changes the file permissions of hello so that we can execute it. The final line executes hello causing it output "Hello World". If we had not used literal quotes we never would have had to use escape characters to ensure that ($) and (") were echoed to the file, this makes the code a little clearer.
A variable may be referenced like so ${VARIABLENAME}, this allows one to place characters immediately preceding the variable like ${VARIABLENAME}aaa without the shell interpreting aaa as being part of the variable name.
Services: - UNIX Shell Scripting Homework | UNIX Shell Scripting Homework Help | UNIX Shell Scripting Homework Help Services | Live UNIX Shell Scripting Homework Help | UNIX Shell Scripting Homework Tutors | Online UNIX Shell Scripting Homework Help | UNIX Shell Scripting Tutors | Online UNIX Shell Scripting Tutors | UNIX Shell Scripting Homework Services | UNIX Shell Scripting