1. 基本操作
a. export(打印所有环境变量) ,echo $VARIABLE_NAME(打印某个环境变量)
b. whereis(查找可执行命令的源文件位置)
c. which(查找可执行命令的绝对路径)
d. clear(清楚窗口内容)
1.1文件操作
a . cat
1) 显示文本文件内容 cat filename
2) 复制文本文件内容 cat file1 >file2(覆盖写)
3) 结合文本文件内容 cat file1 >>file2(附加写)
4) 创建新文件 cat > test.txt<<EOF
b.chmod
修改文件权限 chmod 755filename
c. cp
复制文本文件内容 cpfile1 file2(覆盖写)
d. diff
比较两个文本文件内容差异diff file1 file2
e. file
查看文件类型 filefilename
f. find
查找某一目录下(子目录下)文件find directoryname –name filename
g. gunzip
gzip压缩文件gzip filename 得到后缀名.gz的文件
gunzip解压文件 gunzip filename得到普通文件
h. zcat
查看gzip已压缩的文件
i. head
查看某个文件的前10行 head filename
j. more
查看某个文件的内容 morefilename
k. mv
移动某个文件到指定目录
l. rm
删除文件 rm –ffilename
删除目录(子目录)下的所有文件及文件夹 rm –rf directory
m. tail
显示文本文件后10行 tail filename
n. touch
创建或者更新文件 touchfilename
1.2文本操作
a. awk 一行一行的处理整个文件
一般格式:awk '/search_pattern/ { action_to_take_if_pattern_matches; }' file_to_parse
基本使用:
以:分割域打印第n个域的内容 awk -F':' '{ print $1 }' /etc/passwd
打印整个文本文件内容 awk '{ print }' /etc/passwd
|表示shell管道,即对于awk的结果进行排序 awk -F':' '{ print $1 }'/etc/passwd | sort
模式匹配awk '$9 == 500 { print $0}' /var/log/httpd/access.log
打印tom or jerry or vivek的所在行 awk'/tom|jerry|vivek/' /etc/passwd
打印第一行退出 awk "NR==1{print;exit}" /etc/resolv.conf
累加第一列的所有值,并打印结果 awk '{total += $1} END {print total}'earnings.txt
打印浮点型值 awk 'BEGIN {printf "%.3f\n", 2005.50 / 3}'
将所有的awk命令放入.awk文件中 awk -f mypgoram.awk input.txt
将shell脚本中的变量传入awk中-v
b. cut
从每一行中删除部分
以空格分隔只显示每一行的第2,7,9列内容 cut -d " " –f 2,7,9 example.txt
c. echo
显示文本的一行信息
d. grep
基本格式:grep pattern filename
查找文本文件中存在关键词的行 grep -E 'Lorem|dolor' example.txt
打印匹配某种模式的行 grep -F 'Lorem|dolor' example.txt
以某种模式搜索目录下的所有文件 grep admin -r /etc/
搜索当前目录下所有包含main的文件 grep –ER ‘main’ *
如果上述需要限定为单词 grep –ERw ‘main’ *
搜索example.txt文件中包含关键词sys或者user的行 grep –Ew ‘sys|user’ example.txt
e. fmt
格式化输出
cat example.txt | fmt -w 20
f. nl
文件的行数 nlfilename
g.sed
流编辑过滤和转化文本
将文本中的所有空格替换成- sed 's/ /-/g' example.txt
将文本中的所有数字替换成d sed 's/[0-9]/d/g' example.txt
h .sort
按照a,b,c,d,e,f…进行排序
I . tr
翻译或者删字符
将文本文件中的所有小写字母变成大写字母cat example.txt | tr 'a-z' 'A-Z'
j. uniq
消除重复行 sort example.txt | uniq
消除重复行但会显示每一行出现的频次 sort example.txt | uniq –c
k.wc
告诉文本文件中有多少行,多少单词,多少字符wc filename
1.3目录操作
a. cd
b.mkdir
c.pwd
1.4 SSH&系统信息&网络操作
a. df
显示磁盘信息
b. dig
得到domain的DNS信息 dig domain
c. du
显示磁盘文件或者目录的使用情况
磁盘文件大小 du –sh filename
d. finger
显示用户信息 finger username
e. kill
杀死进程 kill PID
f. killall
杀死所有名为CMD的进程 killall CMD_NAME
g. man
相关命令的详细使用手册 mancmd
h. passwd
修改密码
i. scp
远程传输文件 scptest1.txt 192.168.0.49:/home/cq
j. ssh
远程登陆机器 ssh192.168.0.49
k. uname
显示内核信息 uname –a
l. wget
下载某个软件 wget http://xxx.com
L.whois
domain的whois信息 whois domain
2. 基本shell编程
2.1 变量
创建变量 str=”helloworld”
创建数组 array[0]=0 array[1]=1array[2]=2
打印变量 echo $str
${varname:-word} # if varname exists and isn't null, return its value; otherwise returnword
${varname:=word} # if varname exists and isn't null, return its value; otherwise set itword and then return its value
${varname:+word} # if varname exists and isn't null, return word; otherwise return null
${varname:offset:length} # performs substring expansion. It returnsthe substring of $varname starting at offset and up to length characters
2.2 字符串替换
${variable#pattern} # if the pattern matches the beginningof the variable's value, delete the shortest part that matches and return therest
${variable##pattern} # if the pattern matches thebeginning of the variable's value, delete the longest part that matches andreturn the rest
${variable%pattern} # if the pattern matches the end ofthe variable's value, delete the shortest part that matches and return the rest
${variable%%pattern} # if the pattern matches the end of thevariable's value, delete the longest part that matches and return the rest
${variable/pattern/string} # the longest match to pattern in variable isreplaced by string. Only the first match is replaced
${variable//pattern/string} # the longest match topattern in variable is replaced by string. All matches are replaced
${#varname} #returns the length of the value of the variable as a character string
2.3 函数
functname() {
shell commands
}
例如:
function hello {
echo world!
}
hello
function say {
echo $1
}
say "hello world!"
function说明后面是一个函数,直接通过函数名可以调用 函数名后面可以接多个参数,函数体中使用$1等来使用传入的参数
2.4 条件
if [expression]; then
will execute only if expression is true
else
will execute ifexpression is false
fi
OR
if [ expression 1 ]
then
Statement(s) tobe executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) tobe executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) tobe executed if expression 3 is true
else
Statement(s) tobe executed if no expression is true
fi
OR
case expression in
pattern1 )
statements;;
pattern2 )
statements;;
...
esac
例如:
if [ $word = $wxc ];then
hello
else
say"2222!" "wqdqdqdq"
fi
OR
if [ $word ==$wxc ]
then
echo "a is equal to b"
elif [ $word -gt$wxc ]
then
echo "a is greater than b"
elif [ $word -lt$wxc ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
OR
case $wxc in
123)
echo "it isa letter"
;;
124)
echo "it isa digits"
;;
*)
echo"erro"
;;
Esac
statement1 && statement2 # both statements are true
statement1 || statement2 #one of the statement is true
str1=str2 # str1 matchesstr2
str1!=str2 # str1 doesnot match str2
str1<str2 # str1 isless than str2
str1>str2 # str1 isgreater than str2
-n str1 # str1 is notnull (has length greater than 0)
-z str1 # str1 is null(has length 0)
-a file # file exists
-d file # file existsand is a directory
-e file # file exists;same -a
-f file # file existsand is a regular file (i.e., not a directory or other special type of file)
-r file # you haveread permission
-s file # file existsand is not empty
-w file # your havewrite permission
-x file # you haveexecute permission on file, or directory search permission if it is a directory
-N file # file wasmodified since it was last read
-O file # you own file
-G file # file's groupID matches yours (or one of yours, if you are in multiple groups)
file1 -nt file2 # file1 isnewer than file2
file1 -ot file2 # file1 isolder than file2
-lt # less than
-le # less than or equal
-eq # equal
-ge # greater than orequal
-gt # greater than
-ne # not equal
2.5 循环
for循环
1)for name [in list]
do
statements that can use $name
done
例如:
for loop in 1 23 4 5
do
echo "The value is: $loop"
done
2) for (( initialisation ; ending condition ; update ))
do
statements...
done
例如:
for (( i=1; i<=5; i++ ))
do
echo "i=$i"
done
while循环(只要condition满足,一直做statements操作)
1)while condition; do
statements
done
例如:
myvar=1
while [ $myvar -le 10 ]
do
echo $myvar
myvar=$(( $myvar + 1 ))
done
until循环(直到condition满足之前,一直做statements的操作)
until condition; do
statements
done
例如:
myvar=1
until [ $myvar -gt 10 ]
do
echo $myvar
myvar=$(( $myvar + 1 ))
done
3. 调试
bash -n scriptname 不运行脚本,但检查语法错误
bash -v scriptname 边打印命令,边执行
bash -x scriptname 命令行执行完后打印命令