Bash

From Halfface
Jump to navigation Jump to search

bash Shell

For using multiple filenames in one command in Unix commandline use the following command:

for i in <equal part of all filenames>*;do <command> $i;done

$i is the variable set by the loop.

Example:

for i in sync_m4606_danube_sg_org_*;do /usr/atria/etc/mkorder -data $i -fship blrsxa16.blr.infineon.com;done

More examples:

for i  in `ct lsvob |grep systemq | cut -d" " -f4`;do /usr/atria/etc/utils/albd_list -s $i;done

All Files starting with sync_m4606_danube_sg_org_ are used to perform a mkorder -data to forward these packets to Bangalore server.

for i in `cut -f2 -d" " viewlist.del`;do rm -r /home/memhub.view/$i;done

Use viewtags of column 2 of the file viewlist.del to remove the viewstorage.

Alternative

Use xargs for simply passing over results to a simple command:

ps- elf | grep <string> |tr -s " " |cut f<row nr.> -d" "|xargs <command>

tr - text replace with -s for substitute

e.g.:

ps -elf | grep CCMS | tr -s " " | cut -f5 -d" " |xargs kill

loop number

i=0; while [ $i -lt 10 ]; do echo $i; i=`expr $i + 1`; done

interactive shell

echo $-                If shell is interactive variable will contain i.

explanation

operand		3, 3 + 6 = 9 '+' is the operator and '3' and '6' are the operands.
operator		+, 3 + 6 = 9 '+' is the operator and '3' and '6' are the operands.
primary		That which stands first in order, rank, or importance
Arithmetic		most elementary branch of mathematics,
Command control	Testing exit status of a command in order to determine whether a portion of the program should be executed.
Conditional branch	Logical point in the program when a condition determines what happens next.
Logic flow		The overall design of the program. Determines logical sequence of tasks so that the result is successful and controlled.
Loop			Part of the program that is performed zero or more times.
User input		Information provided by an external source while the program is running, can be stored and recalled when needed.

debug

set -x			# activate debugging from here
w
set +x			# stop debugging from here

variables

Character	Definition
$*	Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the  value of each parameter separated by the first character of the IFS special variable.
$@	Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word.
$#	Expands to the number of positional parameters in decimal.
$?	Expands to the exit status of the most recently executed foreground pipeline.
$-	A hyphen expands to the current option flags as specified upon invocation, by the set built-in command, or those set by the shell itself (such as the -i).
$$	Expands to the process ID of the shell.
$!	Expands to the process ID of the most recently executed background (asynchronous) command.
$0	Expands to the name of the shell or shell script.
$_	The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.

quoting

Single quotes

Single quotes () are used to preserve the literal value of each character enclosed within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Double quotes

Using double quotes the literal value of all characters enclosed is preserved, except for the dollar sign, the backticks (backward single quotes, ``) and the backslash.

The dollar sign and the backticks retain their special meaning within the double quotes.

The backslash retains its meaning only when followed by dollar, backtick, double quote, backslash or newline. Within double quotes, the backslashes are removed from the input stream when followed by one of these characters. Backslashes preceding characters that don't have a special meaning are left unmodified for processing by the shell interpreter.

A double quote may be quoted within double quotes by preceding it with a backslash.

eight kinds of expansion performed

1 Brace expansion
echo {01..3}{a..c}
1a 1b 1c 2a 2b 2c 3a 3b 3c

2 Tilde expansion

If a word begins with an unquoted tilde character ("~"), all of the characters up to the first unquoted slash (or all characters, if there is no   unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the HOME shell variable. 
If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home  directory associated with the specified login name.
If the tilde-prefix is "~+", the value of the shell variable PWD replaces the tilde-prefix. If the tilde-prefix is "~-", the value of the shell variable OLDPWD, if it is set, is substituted.
If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed by a "+" or a "-", the tilde-prefix is replaced  with the corresponding element from the directory stack, as it would be displayed by the dirs built-in invoked with the characters following tilde in  the tilde-prefix as an argument. If the tilde-prefix, without the tilde, consists of a number without a leading "+" or "-", "+" is assumed.
If the login name is invalid, or the tilde expansion fails, the word is left unchanged.
Ech variable assignment is checked for unquoted tilde-prefixes immediately following a ":" or "=". In these cases, tilde expansion is also performed.  Consequently, one may use file names with tildes in assignments to PATH, MAILPATH, and CDPATH, and the shell assigns the expanded value.

3 Shell parameter and variable expansion

echo ${!N*}
NNTPPORT NNTPSERVER NPX_PLUGIN_PATH

The following construct allows for creation of the named variable if it does not yet exist:
${VAR:=value}

4. Command substitution

Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed like this:
$(command) or like this using backticks:`command`
Bash performs the expansion by executing COMMAND and replacing the command substitution with the standard output of the command, with any trailing   newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.
When the old-style backquoted form of substitution is used, backslash retains its literal meaning except when followed by "$", "`", or "\". The first  backticks not preceded by a backslash terminates the command substitution. When using the "$(COMMAND)" form, all characters between the parentheses make  up the command; none are treated specially.
Command substitutions may be nested. To nest when using the backquoted form, escape the inner backticks with backslashes.
If the substitution appears within double quotes, word splitting and file name expansion are not performed on the results.

5. Arithmetic expansion

$(( EXPRESSION ))
$[ EXPRESSION ]

6. Process substitution

Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of
<(LIST)
or
>(LIST)
The process LIST is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the   current command as the result of the expansion. If the ">(LIST)" form is used, writing to the file will provide input for LIST. If the "<(LIST)" form is  used, the file passed as an argument should be read to obtain the output of LIST. Note that no space may appear between the < or > signs and the left  parenthesis, otherwise the construct would be interpreted as a redirection.

7. Word splitting

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word  splitting.
The shell treats each character of $IFS as a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset,  or its value is exactly "'<space><tab><newline>'",

8. File name expansion 

File name expansion Bash scans each word for the characters "*", "?", and "[". If one of these characters appears, then the word is regarded as a  PATTERN, and replaced with an alphabetically sorted list of file names matching the pattern.

regular expression metacharacters

Operator	Effect
.		Matches any single character.
?		The preceding item is optional and will be matched, at most, once.
*		The preceding item will be matched zero or more times.
+		The preceding item will be matched one or more times.
{N}		The preceding item is matched exactly N times.
{N,}		The preceding item is matched N or more times.
{N,M}		The preceding item is matched at least N times, but not more than M times.
-		represents the range if it's not first or last in a list or the ending point of a range in a list.
^		Matches the empty string at the beginning of a line; also represents the characters not in the range of a list.
$		Matches the empty string at the end of a line.
\b		Matches the empty string at the edge of a word.
\B		Matches the empty string provided it's not at the edge of a word.
\<		Match the empty string at the beginning of word.
\>		Match the empty string at the end of word.

Two regular expressions may be concatenated; the resulting regular expression matches any string formed by concatenating two substrings that respectively match the concatenated subexpressions.
Two regular expressions may be joined by the infix operator "|"; the resulting regular expression matches any string matching either subexpression.
Repetition takes precedence over concatenation, which in turn takes precedence over alternation. A whole subexpression may be enclosed in parentheses to override these precedence rules.

Basic versus extended regular expressions
In basic regular expressions the metacharacters "?", "+", "{", "|", "(", and ")" lose their special meaning; instead use the backslashed versions "\?", "\+", "\{", "\|", "\(", and "\)".

Character classes

A bracket expression is a list of characters enclosed by "[" and "]". It matches any single character in that list; if the first character of the list is the caret, "^", then it matches any character NOT in the list. For example, the regular expression "[0123456789]" matches any single digit.
Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive, using the locale's collating sequence and character set. For example, in the default C locale, "[a-d]" is equivalent to "[abcd]". Many locales sort characters in dictionary order, and in these locales "[a-d]" is typically not equivalent to "[abcd]"; it might be equivalent to "[aBbCcDd]", for example. To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value "C".

command examples

If you want to find the literal asterisk character in a file or output, use grep -F

cathy ~> grep * /etc/profile
cathy ~> grep -F '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

Matching lines starting with a given pattern and ending in a second pattern are showed like this:

sandy ~> sed -n '/^This.*errors.$/p' example
This is a line not containing any errors.

Character classes

Character classes can be specified within the square braces, using the syntax [:CLASS:], where CLASS is defined in the POSIX standard and has one of the values
"alnum", "alpha", "ascii", "blank", "cntrl", "digit", "graph", "lower", "print", "punct", "space", "upper", "word" or "xdigit".
ls -ld [[:digit:]]*

sed commands

The sed program can perform text pattern substitutions and deletions using regular expressions, like the ones used with the grep command; see Section 4.2.
The editing commands are similar to the ones used in the vi editor:
Command	Result
a\	Append text below current line.
c\	Change text in the current line with new text.
d	Delete text.
i\	Insert text above current line.
p	Print text.
r	Read a file.
s	Search and replace text.
w	Write to a file.

awk

The variables $1, $2, $3, ..., $N hold the values of the first, second, third until the last field of an input line. The variable $0 (zero) holds the value of the entire line. This is depicted in the image below,

\a Bell character \n Newline character \t Tab

Quotes, dollar signs and other meta-characters should be escaped with a backslash.

2008-01-27 23:38$ ls -l | awk 'BEGIN { print "You are a loser" }{ print $8 "\t is \t" $5 " bytes" } END { print "But what do you care?" }'
You are a loser
12341    is     0 bytes
12342    is     0 bytes
12343    is     0 bytes
But what do you care?
BEGIN { FS=":" }
{ print $1 "\t" $5 }
$ awk -f printnames.awk /etc/passwd

awk 'BEGIN { OFS=";" ; ORS="\n-->\n" } { print $1,$2}' test
record1;data1
-->
record2;data2

cat processed.awk
BEGIN { OFS="-" ; ORS="\n--> done\n" }
{ print "Record number " NR ":\t" $1,$2 }
END { print "Number of records processed: " NR } 

awk -f processed.awk test
Record number 1:        record1-data1
--> done
Record number 2:        record2-data2
--> done
Number of records processed: 2
--> done