Introduction
Shell编程从入门到放弃学习笔记。
Materials
Variable
-
$i
-
$*
-
$#
-
$?
-
Array
A=(test1 test2 test3) echo ${A[0]} echo ${A[@]} echo ${#A[@]} ${A[@]/test2/test5} unset A[2]
Syntax
-
if
if expression1; then statement1 elif expression2; then statement2 else statement3 fi
-
for
for var in iterobj do statement done for ((i=1; i<=10; i++)) do statement done
-
while
while condition_statement do statement done
-
until
until condtion_statement do statement done
-
case select
PS3="...?" select i in CenOS RedHat Ubuntu do #statement case $arg in pattern1) statement1 ;; pattern2) statement2 ;; *) statement3 ;; done
-
function
function command() { statements; } command
-
awk
cat test.txt |awk '{print $i}' cat test.txt |awk '{print $NF}' cat test.txt |awk -F: {print $i} df -h|grep "/"|awk '{print $5}'|sed 's/%//g'
-
grep
grep -v "^$" test.txt grep "^10" test.txt grep "11$" test.txt grep [-E] "([0-9]{1, 3}\.){3}[0-9]{1,3}" egrep "11|wuguangke" test.txt
-
sed
sed [-i] 's/192.10/192.168/g' test.txt sed 's/^/& /g' test.txt sed '/wuguangke/a test007' test.txt sed '/wuguangke/i test007' test.txt sed -n '/wuguangke/p' test.txt sed -n '1,5p' test.txt sed -n '1p;$p' test.txt
-
find
find / -name "test.txt" find . -name "test.txt" find . -maxdepth 1 -name "test.txt" find . -maxdepth 1 -type f -name "*.txt" find . -maxdepth 1 -type f -name "*.txt" -mtime +30 find . -maxdepth 1 -type f -name "*.txt" -mtime -1 find . -maxdepth 1 -type f -name "*.txt" -mtime -5 -exec rm -rf {} \; find . -maxdepth 1 -type f -name "*.txt" -mtime -5 -exec cp {} /tmp/ \; find . -maxdepth 1 -type f -name "*.txt" -mtime -5 |xargs rm -rf {} \; find . -size +20M find . -maxdepth 1 -size +20M -type f find . -maxdepth 1 -size +20M -type f -exec mv {} /tmp/ \;
Examples
- get current directory
CUR_DIR=$(cd $(dirname $0) && pwd) echo $CUR_DIR