shell sumary

1)shell summary
1@@@@function
#!/bin/sh
set _+x
#local "var" is meaning that local var, do not have an effect program
#out of function, so the process run out from function, the var a still
#is test.

function localizer {
  echo "==> In function localizer, a starts as '$a'"
  local a
  echo "==> After local declaration, a is '$a'"
  a="localizer version"
  echo "==> Leaving localizer, a is '$a'"
}
a="test"
echo "Before calling localizer, a is '$a'"
localizer
echo "After calling localizer, a is '$a'"

2@@@@elementary bash comparison operators
String   Numeric   True if
x = y    x -eq y   x is equal to y
x != y   x -ne y   x is not equal to y
x < y    x -lt y   x is less than y
x <= y   x -le y   x is less than or equal to y
x > y    x -gt y   x is greater than y
x >= y   x -ge y   x is greater than or equal to y
-n x       –       x is not null
-z x       –       x is null


3@@@@bash file evaluation operators
Operator    True if
-d file     file exists and is a directory
-e file     file exists
-f file     file exists and is a regular file
-r file     You have read permission on file
-s file     file exists and is not empty
-w file     You have write permission on file
file1 -nt file2    file1 is newer than file2
file1 -ot file2    file1 is older than file2

4@@@@loop
@@@
@@@<1>for
@@@
@@@it is similar to C language.
[root@station78 ~]# cat test.sh
#!/bin/sh
for ((i=0; i<3; i++)); do echo $i; done
[root@station78 ~]# ./test.sh
0
1
2

@@@
@@@<2>while
@@@
@@@" exec 0<$1 " here 0 is standard input, redefine the standard input.
@@@in below case, /etc/passwd all lines would be treated input, respectively. 
[root@station78 ~]# cat test.sh
#!/bin/bash
exec 0<$1
counter=1
while read line;
do
  echo "$counter: $line"
  counter=$((counter + 1))
done

[root@station78 ~]# ./test.sh /etc/passwd | head -n 4
1: root:x:0:0:root:/root:/bin/bash
2: bin:x:1:1:bin:/bin:/sbin/nologin
3: daemon:x:2:2:daemon:/sbin:/sbin/nologin
4: adm:x:3:4:adm:/var/adm:/sbin/nologin


5@@@@array and arithmetic
@@@
@@@<1>arithmetic
@@@
@@@in below case, a is string, b is arithmetic.
[root@station78 ~]# cat test.sh
#!/bin/bash
a=1
b=$((2))
c=$a+$b
d=$(($a+$b))
echo -e "$a + $b = $c \t(plus sign as string literal)"
echo -e "$a + $b = $d \t(plus sign as arithmetic addition)"

[root@station78 ~]# ./test.sh
1 + 2 = 1+2     (plus sign as string literal)
1 + 2 = 3     (plus sign as arithmetic addition)

@@@
@@@<2>array
@@@
@@@in below case,
@@@" ${example[@]} " traversal all elements in array.
@@@" ${#example[@]} " is the number of array elements
[root@station78 ~]# cat test.sh
#!/bin/bash
example=(aa 'bb cc' dd)
example[3]=ee
echo "example[@] = ${example[@]}"
echo "example array contains ${#example[@]} elements"
for elt in "${example[@]}"
do
  echo " Element = $elt"
done

[root@station78 ~]# ./test.sh
example[@] = aa bb cc dd ee
example array contains 4 elements
 Element = aa
 Element = bb cc
 Element = dd
 Element = ee


5@@@@regular expressions
Special characters in regular expressions (common ones)
Symbol    What it matches or does
.         Matches any character
[chars]   Matches any character from a given set
[^chars]  Matches any character not in a given set
^         Matches the beginning of a line
$         Matches the end of a line
\w        Matches any “word” character (same as [A-Za-z0-9_])
\s        Matches any whitespace character (same as [ \f\t\n\r])a
\d        Matches any digit (same as [0-9])
|         Matches either the element to its left or the one to its right
(expr)    Limits scope, groups elements, allows matches to be captured
?         Allows zero or one match of the preceding element
*         Allows zero, one, or many matches of the preceding element
+         Allows one or more matches of the preceding element
{n}       Matches exactly n instances of the preceding element
{min,}    Matches at least min instances (note the comma)
{min,max} Matches any number of instances from min to max

6@@@@cat and echo
[oracle@station78 ~]$ cat test.sh
#!/bin/sh
cat > tmp01 <<EOF 
echo $ORACLE_HOME;
EOF

cat > tmp02 <<'EOF'  #disable the var
echo $ORACLE_HOME;
EOF

###output:
[oracle@station78 ~]$ cat tmp01
echo /u01/app/oracle/product/10.2.0/db_1;
[oracle@station78 ~]$ cat tmp02
echo $ORACLE_HOME;

你可能感兴趣的:(shell)