Bash
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
Does you account exist locally?
if grep $USER /etc/passwd;then echo "your user account is managed locally"; fi
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.
Are you root?
if [ "$(whoami)" != 'root' ]; then echo "You have no permission to run $0 as non-root user." exit 1; fi
With Bash, you can shorten this type of construct. The compact equivalent of the above test is as follows:
[ "$(whoami)" != 'root' ] && ( echo you are using a non-privileged account; exit 1 ) Similar to the "&&" expression which indicates what to do if the test proves true, "||" specifies what to do if the test is false.
real programmer...
test "$(whoami)" != 'root' && (echo you are using a non-privileged account; exit 1)
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
Count the size of file in current directory.
find . -maxdepth 1 -type f -ls | awk 'BEGIN { print "Hello\n" } { total=total + $7 } { print "counting " total } END { print "---------------------------------\nTotal revenue: " total }'
printf
define output format.
if
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.
Primary expressions used with if.
The table below contains an overview of the so-called "primaries" that make up the TEST-COMMAND command or list of commands. These primaries are put between square brackets to indicate the test of a conditional expression.
[ -a FILE ] True if FILE exists. [ -b FILE ] True if FILE exists and is a block-special file. [ -c FILE ] True if FILE exists and is a character-special file. [ -d FILE ] True if FILE exists and is a directory. [ -e FILE ] True if FILE exists. [ -f FILE ] True if FILE exists and is a regular file. [ -g FILE ] True if FILE exists and its SGID bit is set. [ -h FILE ] True if FILE exists and is a symbolic link. [ -k FILE ] True if FILE exists and its sticky bit is set. [ -o optname ] True if shell option optname is enabled. [ -p FILE ] True if FILE exists and is a named pipe (FIFO). [ -r FILE ] True if FILE exists and is readable. [ -s FILE ] True if FILE exists and has a size greater than zero. [ -t FD ] True if file descriptor FD is open and refers to a terminal. [ -u FILE ] True if FILE exists and its SUID (set user ID) bit is set. [ -w FILE ] True if FILE True if FILE exists and is writable. [ -x FILE ] True if FILE exists and is executable. [ -O FILE ] True if FILE exists and is owned by the effective user ID. [ -G FILE ] True if FILE exists and is owned by the effective group ID. [ -L FILE ] True if FILE exists and is a symbolic link. [ -N FILE ] True if FILE exists and has been modified since it was last read. [ -S FILE ] True if FILE exists and is a socket. [ FILE1 -nt FILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not. [ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not. [ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers. [ -o OPTIONNAME ] True if shell option "OPTIONNAME" is enabled. [ -z STRING ] True of the length of "STRING" is zero. [ -n STRING ] or [ STRING ] True of the length of "STRING" is non-zero. [ STRING1 == STRING2 ] True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance. [ STRING1 != STRING2 ] True if the strings are not equal. [ STRING1 < STRING2 ] True if "STRING1" sorts before "STRING2" lexicographically in the current locale. [ STRING1 > STRING2 ] True if "STRING1" sorts after "STRING2" lexicographically in the current locale. [ ARG1 OP ARG2 ] "OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers. -eq equal -ne not equal -lt lesser than -gt greater than. -go greater than or equal
Expressions may be combined using the following operators, listed in decreasing order of precedence:
Operation Effect [ ! EXPR ] True if EXPR is false. [ ( EXPR ) ] Returns the value of EXPR. This may be used to override the normal precedence of operators. [ EXPR1 -a EXPR2 ] True if both EXPR1 and EXPR2 are true. [ EXPR1 -o EXPR2 ] True if either EXPR1 or EXPR2 is true.
case statement
case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;; ... CASEN) COMMAND-LIST;; esac Each case is an expression matching a pattern. The commands in the COMMAND-LIST for the first match are executed. The "|" symbol is used for separating multiple patterns, and the ")" operator terminates a pattern list. Each case plus its according commands are called a clause. Each clause must be terminated with ";;". Each case statement is ended with the esac statement.
read
read [options] NAME1 NAME2 ... NAMEN
One line is read from the standard input, or from the file descriptor supplied as an argument to the -u option. The first word of the line is assigned to the first name, NAME1, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name, NAMEN. If there are fewer words read from the input stream than there are names, the remaining names are assigned empty values.
Option Meaning -a ANAME The words are assigned to sequential indexes of the array variable ANAME, starting at 0. All elements are removed from ANAME before the assignment. Other NAME arguments are ignored. -d DELIM The first character of DELIM is used to terminate the input line, rather than newline. -e readline is used to obtain the line. -n NCHARS read returns after reading NCHARS characters rather than waiting for a complete line of input. -p PROMPT Display PROMPT, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal. -r If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation. -s Silent mode. If input is coming from a terminal, characters are not echoed. -t TIMEOUT Cause read to time out and return failure if a complete line of input is not read within TIMEOUT seconds. This option has no effect if read is not reading input from the terminal or from a pipe. -u FD Read input from file descriptor FD.