Tuesday, September 21, 2010

Shell Tips - Would be growing continuously

Every time looking for a small useful but seldom used tips all over the internet I hate it.


How to output backslash escapes useing echo?


From echo(1):

-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)

So by default there is no backslash escapes! If you want, you have to do like this:

echo -e "\b\c"


What does exec do?


From bash(1):

exec [-cl] [-a name] [command [arguments]]
If command is specified, it replaces the shell. No new process is created. The
arguments become the arguments to command. If the -l option is supplied, the
shell places a dash at the beginning of the zeroth arg passed to command.
This is what login(1) does. The -c option causes command to be executed with
an empty environment. If -a is supplied, the shell passes name as the zeroth
argument to the executed command. If command cannot be executed for some reason,
a non-interactive shell exits, unless the shell option execfail is en-
abled, in which case it returns failure. An interactive shell returns failure
if the file can not be executed. If command is not specified, any redirections
take effect in the current shell, and the return status is 0. If there is a
redirection error, the return status is 1.

exec is useful when you want to set script redirection for all your scripts in current shell, otherwise you will have to set redirection for each.

exec 1>STDOUT_GOES_TO_HERE 2>STDERR_GOES_TO_THERE

After this, all following scripts running from this shell will redirect their STDOUT to STDOUT_GOES_TO_HERE and STDERR to STDERR_GOES_TO_THERE


Where to find ASCII?



man ascii


Using a Control Character in a Script


From UNIX POWER TOOLS
There are times when you need to use non-printing control characters in a script file. If you type them directly into the file, they can be invisible to printers and on your screen - or, worse, they can cause trouble when you print or display the file.

One time you might need to store control characters in a script is when you're writing sed substitution commands; you don't know what delimiters to use because the strings you're substituting could contain almost any text:

sed "s/$something/$whoknows/"

Because sed can use almost any character as the delimiter, you can use a control character like CTRL-A instead of the slash (/). Another time you might also need to use non-printable strings of characters is for controlling a terminal; you won't want to type an Escape character directly into the file.

The answer is to use a command that will create the control characters as the script runs - and store them in shell variables.

If your version of echo interprets an octal number in a string like \001 as its ASCII value, the job is easy. An octal-to-ASCII chart shows you that 001 is CTRL-A. You can store the output of echo in a shell variable, and use the variable wherever you need a CTRL-A character:

ca=`echo '\001'` # control-A character
...
sed "s${ca}$something${ca}$whoknows${ca}"


Remove ^M characters at end of lines in vi


UNIX treats the end of line differently than other operating systems. Sometimes when editing files in both Windows and UNIX environments, a CTRL-M character is visibly displayed at the end of each line as ^M in vi.

To remove the ^M characters at the end of all lines in vi, use:

:%s/^V^M//g


The ^v is a CONTROL-V character and ^m is a CONTROL-M. When you type this, it will look like this:

:%s/^M//g


In UNIX, you can escape a control character by preceeding it with a CONTROL-V. The :%s is a basic search and replace command in vi. It tells vi to replace the regular expression between the first and second slashes (^M) with the text between the second and third slashes (nothing in this case). The g at the end directs vi to search and replace globally (all occurrences).

__CONTINUED__

No comments:

Post a Comment