Sunday, September 8, 2013

SCRIPTING

http://www.thegeekstuff.com/2010/03/introduction-to-bash-scripting/


journald.conf(5) - Journal service configuration file


hostname(5) - Local hostname configuration file hosts(5) - static table lookup for hostnames hosts.equiv(5) - list of hosts and users that are granted "trusted" r command access to your system


Expansion

Expansion refers to the way a shell modifies the command-line before executing it. The bash shell performs:Brace expansion, e.g., (file_{one,two,three}.txt creates multiple files: file_one.txt, file_two.txt, and file_three.txt)~ : The full path of the users home directory~+ : The current working directory~- : The most recent previous working directory.$ : Parameter (or variable) expansion


Parameter Expansion Manipulations: ${VAR :-default } Use default if VAR is unset ${VAR :=default } Same as previous and default is assigned to VAR. ${VAR :?message } Display error message if VAR is unset ${VAR :offset } or ${VAR :n :l } Produces the nth character of $VAR and then the following l characters. If l is not present, then all characters to the right of the nth character are produced. This is useful for splitting up strings


Example: TEXT=scripting_for_phun echo ${TEXT:10:3} echo ${TEXT:10} ${#VAR } Gives the length of $VAR. ${!PRE *} Gives a list of all variables whose names begin with PRE. ${VAR #pattern } The glob expression pattern removed from the leading part of the string. ${VAR ##pattern } The same as the previous expansion except that pattern may contains wild cards. ${VAR %pattern } The same as ${VAR #pattern } except that characters are removed from the trailing part of the string. ${VAR %%pattern } The same as ${VAR ##pattern } except that characters are removed from the trailing part. ${VAR /search /replace } $VAR is returned with the first occurrence of the string search replaced with replace. ${VAR /#search /replace } Same as ${VAR /search /replace } except that the match is attempted from the leading part of $VAR. ${VAR /%search /replace } Same as ${VAR /search /replace } except that the match is attempted at the trailing part of $VAR. ${VAR //search /replace } Same as ${VAR /search /replace } except that all instances of search are replaced.




Example 3: Get the list of usernames in /etc/passwd file

This sed example displays only the first field from the /etc/passwd file.

$sed 's/\([^:]*\).*/\1/' /etc/passwd root bin daemon adm lp sync shutdown








If Conditionals

The if command executes a number of commands (all the lines between the if and the fi) if a condition is met. test ... or [] ... can be used to express the conditon. if [ $A -le $B ] ; then echo 'A < B' else echo 'B < A' fi Alternative syntax: if [...] then ... else ... fi The if command understands nothing of arithmetic. It executes the command test and tests the exit code. If the exit code is zero, then the command is considered to be successful and if proceeds with the body of the if statement block. If can equally well be used with any command. For example (conditionally add /usr/local/bin if grep does not find it in PATH)

if echo "$PATH" | grep -qwv /usr/local/bin ; then export PATH="$PATH:/usr/local/bin" fi

Example 3: Get the list of usernames in /etc/passwd file

This sed example displays only the first field from the /etc/passwd file.


The until statement is identical to while except that the reverse logic is applied. 

For Loops

"For" loops over a command block between do and done and replaces the loop variable with elements from a list. The elements can be strings (e.g., file names) or numbers. for i in cows sheep chickens pigs do echo "$i is a farm animal" done echo -e "but\nGNUs are not farm animals"

Looping Over Glob Expressions

The shell can expand file names when given wildcards (ls *.txt). This applies equally well in any situation:





$sed 's/\([^:]*\).*/\1/' /etc/passwd root bin daemon adm lp sync shutdown






Unix Shell Scripts

A shell script is like a program for a shell, i.e. a list of shell commands stored in a file. The file must have executable rights. Each shell (ksh, csh, tcsh, sh or bash) has slightly different syntax and different commands. The first line of a script specifies the shell: #!/bin/csh for a C-Shell Script #!/bin/bash for a Bash-Shell Script #!/usr/bin/perl for a Perl Script




Example: TEXT=scripting_for_phun echo ${TEXT:10:3} echo ${TEXT:10} ${#VAR } Gives the length of $VAR. ${!PRE *} Gives a list of all variables whose names begin with PRE. ${VAR #pattern } The glob expression pattern removed from the leading part of the string. ${VAR ##pattern } The same as the previous expansion except that pattern may contains wild cards. ${VAR %pattern } The same as ${VAR #pattern } except that characters are removed from the trailing part of the string. ${VAR %%pattern } The same as ${VAR ##pattern } except that characters are removed from the trailing part. ${VAR /search /replace } $VAR is returned with the first occurrence of the string search replaced with replace. ${VAR /#search /replace } Same as ${VAR /search /replace } except that the match is attempted from the leading part of $VAR. ${VAR /%search /replace } Same as ${VAR /search /replace } except that the match is attempted at the trailing part of $VAR. ${VAR //search /replace } Same as ${VAR /search /replace } except that all instances of search are replaced.















more








more



http://www.felixgers.de/teaching/shells/bash_script.html


Each line is a distinct command --the commands are newline-separated. You can also separate them with a semicolons. Variables have no type. An ordinary variable can be expanded with $VARNAME or ${VARNAME}. Single forward quotes ' protect the enclosed text from shell interpretation. $() are an alternative notation. Double quotes "" allow all shell interpretations inside them. They are used to group text containing whitespace. When a command is inside backward quotes ` its output is substituted in place of the back-quotes.



Bash Script Example

Here is an example for a bash shell script that lists all HTML files "*.html" in a directory and writes the first line of each HTML-file to a file called File_Heads:


#!/bin/sh # This is a comment echo "List of files:" ls -lA FILE_LIST="`ls *.html`" echo FILE_LIST: ${FILE_LIST} RESULT="" for file in ${FILE_LIST} do FIRST_LINE=`head -2 ${file}` RESULT=${RESULT}${FIRST_LINE} done echo ${RESULT} | cat >FILE_HEADS echo "'$RESULT' written Script done. "



more
https://www.kernel.org/doc/man-pages/

No comments:

Post a Comment