~ $ ls /bin/*sh
/bin/bash /bin/csh /bin/ksh /bin/sh /bin/tcsh /bin/zsh
wget uc.cn -O uc.html 2>/dev/null || echo "Failed to get url"
面向字符串
space tab | < > & ( ) ;
|| & && ; ;; ( ) | |& <newline>
' "
$ `` \
var=3+5
echo '$$ `uname` $var $((var))'
echo "$$ \$ `uname` $var $((var))"
字符串展开
echo {a,b}cd{e,f} ~ ~-/ `uname` $(ls /bin/[kcz]sh) $$ `ls /bin/e*` /bin/?sh ${var} $(($var)) $#
字符串使用
命令
ls -l /home
break continue : . echo eval exec exit export expr printf return set shift trap unset
((3+5))、[[ 4 > 5]]
赋值 var=value
常见语法
cd ~
uname |grep linux
; & || &&
() {} (()) [[]]
流程控制选择
if condition1; then
statements
elif condition2; then
statements
else
statements
fi
for var in one two three four five; do
echo '$var is '$var
done
while
until
case
select
cat in.txt |while read url; do
echo "got $url"
done > out.txt
tar -zcvf update.`date '+%Y%m%d_%H%M%S'`.tar.gz somedir
cat <<EO > index.html
<html>
<head>
<title></title>
</head>
hello $USER
your computer's name `hostname`
</html>
EO
/usr/bin/find /home/dir -type f ! \( -name "*.bz2" -o -name "*.gz" \) -mmin +120 -exec bzip2 {} \; >/dev/null 2>&1
awk '!a[$0]++' in.txt > out.txt
grep -Phio "(?<=\`uri=)[^\`]+" *.log |sort |uniq
grep -Pio "(?<='customizable': ')[^',]+" *.log
`uri=http://13aaaa.com/html/PIC01/332190.html`wait=0.09`slow...
sed -i 's/dispatch_rule/dispatch_rule = /g' in.txt
awk -F, '{sum+=$2} END{print sum}
ps -ef |grep start_all.py |grep -v grep && exit 2 || python start_all.py
url='http://www_sina_com_cn/path/index.html'
echo ${url//_/.}
echo ${url#http://}
echo ${url%%/*}
for i in $(seq -w 0 23); do
echo "log.8???_2014-10-15_$i"
done
find . -type f |xargs -n 1000 grep -Pio -m 1 $key
find /nfs -size +10000k –xdev –exec ls –lh {} \;
find ~ -mtime +7 -name "*.log*" -exec rm -rf {} \;
r4 w2 x1
u(ser) g(roup) o(ther) a(ll)
chmod o+r filename
chmod 775 filename
chown user:user_group filename
if uname |grep -i linux; then
echo 'you are using linux'
fi
通配符扩展(和正则区别)*
?
[set]
[^set]
{ba,c,k,z,tc}sh
字符串比较,=
!=
;数字比较 -eq -ne -lt -le -gt -ge
if [ "$t1" != "$t2" -a $n1 -eq $n2 ] || [ $n3 -eq 100 ]; then
...
fi
30 * * * * source /home/nemo/.bashrc; /home/nemo/somescript > /dev/null
0-59 1-23 1-31 1-12 0-6 somescript
sort in.txt > in.txt
func () {
local var=hello;
}
echo echo ${BASH}_VERSION
echo ${10}
[ "$day" = yes ]
mkdir -p some/conf/{first,second/{third,forth}}
vi some/domains.conf
find . -name "dom*.conf" -exec vi {} \;
tar xvf -C ~ /nfs/some.tar.gz # 减少移动文档
cd ~ && tar xvf -C ~ /nfs/some.tar.gz # 鼓励使用列表
使用 \ 进行长命令折行;
() {} 对命令分组执行
rm *.xml; rm some -r #避免 rm * -rf
字符 | 通用含义 | 示例及说明 |
---|---|---|
* | 位置变量列表,使用IFS第一个字符分开 | fun 'hello world' |
@ | 位置变量列表 | |
# | 位置参数数目 | |
? | 最近一个前台任务的返回状态 | $? |
- | 所有发送给shell的标志 | |
$ | 进程ID | |
! | 最近被放到后台执行的命令的进程ID | |
_ | 前一个命令最后一个参数 | |
0 | shell、脚本名、函数名 | |
1-9 | shell、脚本、函数的位置参数 | $2 ${10} |
已知
ls /tmp/*
=/tmp/a /tmp/b /tmp/c
,VAR=/tmp/*
,求以下命令的输出:
ls '$VAR'
ls "$VAR"
ls $VAR
ls $VARa
ls ${VAR}a
eval ls "$VAR"
已知:/etc/hosts的内容为下:
192.168.1.11 oldboy11.etiantian.org
192.168.1.21 oldboy21.etiantian.org
192.168.1.31 oldboy31.etiantian.org
#192.168.1.111 oldboy111.etiantian.org
怎么才能在输入IP后找到/etc/hosts里对应的唯一的hostname?
列出你最常用到的linux命令
grep awk sed find vi git
cd ls echo cat cp mv wl tar